PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ circle_raycast_intersections()

static int circle_raycast_intersections ( const POINT2D center,
double  radius,
double  ray,
POINT2D i0,
POINT2D i1 
)
static

Calculates the intersection points of a circle and a horizontal line.

The equation of a circle is (x - cx)^2 + (y - cy)^2 = r^2. The equation of the horizontal line is y = y_line. Substituting y_line into the circle equation gives: (x - cx)^2 = r^2 - (y_line - cy)^2 This function solves for x.

Parameters
centerA pointer to the center point of the circle.
radiusThe radius of the circle.
rayThe y-coordinate of the horizontal line.
i0A pointer to a POINT2D to store the first intersection point.
i1A pointer to a POINT2D to store the second intersection point.
Returns
The number of intersection points found (0, 1, or 2).

Definition at line 903 of file ptarray.c.

904{
905 // Calculate the vertical distance from the circle's center to the horizontal line.
906 double dy = ray - center->y;
907
908 // If the absolute vertical distance is greater than the radius, there are no intersections.
909 if (fabs(dy) > radius)
910 return 0;
911
912 // Use the Pythagorean theorem to find the horizontal distance (dx) from the
913 // center's x-coordinate to the intersection points.
914 // dx^2 + dy^2 = radius^2 => dx^2 = radius^2 - dy^2
915 double dx_squared = radius * radius - dy * dy;
916
917 // Case 1: One intersection (tangent)
918 // This occurs when the line just touches the top or bottom of the circle.
919 // dx_squared will be zero. We check against a small epsilon for floating-point safety.
920 if (FP_EQUALS(dx_squared, 0.0))
921 {
922 i0->x = center->x;
923 i0->y = ray;
924 return 1;
925 }
926
927 // Case 2: Two intersections
928 // The line cuts through the circle.
929 double dx = sqrt(dx_squared);
930
931 // The first intersection point has the smaller x-value.
932 i0->x = center->x - dx;
933 i0->y = ray;
934
935 // The second intersection point has the larger x-value.
936 i1->x = center->x + dx;
937 i1->y = ray;
938
939 return 2;
940}
#define FP_EQUALS(A, B)
double y
Definition liblwgeom.h:390
double x
Definition liblwgeom.h:390

References FP_EQUALS, POINT2D::x, and POINT2D::y.

Referenced by ptarrayarc_raycast_intersections().

Here is the caller graph for this function: