1

In Asymptote, how can I check if a point is on a line within a tolerance?

Also, how can I check if a point is in a circle?

  • 3
    Hi and welcome to the site. Can you clarify the following in your question? Are you looking for a function to check whether a given point is inside a circular path? Are you also looking for a function to check whether a given point is on a linear path of finite length? Must the point be exactly on the line, or on the line within a tolerance? Thank you. – James Sep 25 '19 at 1:34
  • 3
    There is a function called "inside" in the asymptote manual (in the section about paths). You give the function a pair and a cyclic path. It returns a boolean. – James Sep 25 '19 at 1:40
  • 1
    Tks James, I write function to check POL: bool g2POL(point A, line m) { real min=1/10^15; point H=midpoint(A--reflect(m)*A); if (abs(A-H)<min) return true; else return false; } – Tran Quan Sep 26 '19 at 3:08
  • 1
    Looks good! You should answer your own question with that function. You should include in your answer that you are using the geometry package. You might consider making the tolerance "min" value an argument of your function. For example, bool g2POL(point A, line m, real tolerance=1e-15) – James Sep 26 '19 at 11:18
  • I now see that there is a function already in the geometry package for distance from a point to a line. You could use it to make your function simpler. real distance(point M, line l) – James Sep 26 '19 at 11:34
1

The following code demonstrates the solution to your two questions using the geometry package in asymptote.

import geometry;
unitsize(1inch);

point p1 = (0.0,0.0);
point p2 = (1.0,1.0);

path c = circle((0,0), 1);
write(inside(c, (pair) p1));
write(inside(c, (pair) p2));

line l = line(p1, (1,-1));
write(distance(p1, l));
write(distance(p2, l));

bool pointOnLine(line l, point p, real tolerance=1e-8) {
    return distance(p, l) < tolerance;
}

write(pointOnLine(l, p2));
write(pointOnLine(l, p2, 2.0));

draw(c);
dot(Label("p1 (0,0)"), p1, red);
dot(Label("p2 (1,1)"), p2, red);
draw(l, blue);

The output of the 6 write(); commands are as follows.

true
false
0
1.41421356237309
false
true

Notes:

  1. The inside function is a built-in command for a cyclic path and a pair. A point must be cast to a pair as shown to use this function.

  2. I have created a function to test if a point is on a line. It incorporates the distance function. A tolerance may be given. If not given, the tolerance defaults to 1e-8. In the second demonstration of this function, I give a large tolerance of 2.0.

enter image description here

  • 1
    There is a operator @: (A @ m) return true if and only if the point A belongs to the line (or circle, ...) m. – Tran Quan Sep 28 '19 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.