The intersection of a circle and two line segments does not detect all intersections
I’m using SymPy’s geometry module intersecting segments and circles. It seems that only a few intersections are calculated and many others are ignored.
Here are some test codes for finding the intersection:
from sympy.geometry import Point2D, Segment2D, Circle
# Point A, B and C
A = Point2D(1, 1)
B = Point2D(3, -1)
C = Point2D(-2, -2)
# Segment from A to B
f_0 = Segment2D(A, B)
# Segment from A to C
f_1 = Segment2D(A, C)
# Circle with center A and radius 0.8
c = Circle(A, .8)
i_0 = c.intersection(f_0)
i_1 = c.intersection(f_1)
print(i_0)
print(i_1)
This should work, and does capture all intersection points when doing line-circle intersections or circle-circle intersections, but not segment-circle or ray-circle intersections. Here is the output:
[]
[Point2D(217157287525381/500000000000000, 217157287525381/500000000000000)]
It clearly did not work as expected. I don’t know what caused it, I would like to know how to fix it or find any alternative (preferably still using SymPy).
Solution
I
still don’t know why my previous method didn’t work, but I know there’s a way to do it. | Wolfram After wandering around in Alpha, I realized that the coordinates of the intersections were all unreasonable. Seeing that the output of the program is a score, it is clear that something is wrong. It turns out that the radius of the circle of 0.8 causes all the trouble.
You need to symbolize it first, not float as a parameter. Two things are important to remember:
- The parameter must be a string and not float.
- The sign of “reason” must be true.
With this in mind, the new code becomes:
from sympy import sympify
from sympy.geometry import Point2D, Segment2D, Circle
# Point A, B and C
A = Point2D(1, 1)
B = Point2D(3, -1)
C = Point2D(-2, -2)
# Segment from A to B
f_0 = Segment2D(A, B)
# Segment from A to C
f_1 = Segment2D(A, C)
# Circle with center A and radius 0.8
c = Circle(A, sympify('.8', rational=True))
i_0 = c.intersection(f_0)
i_1 = c.intersection(f_1)
print(i_0)
print(i_1)
Then the output becomes:
[Point2D(2*sqrt(2)/5 + 1, -2*sqrt(2)/5 + 1)]
[Point2D(-2*sqrt(2)/5 + 1, -2*sqrt(2)/5 + 1)]