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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThe 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:
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.
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
.
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)
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)