PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ 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 894 of file ptarray.c.

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

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

Referenced by ptarrayarc_raycast_intersections().

Here is the caller graph for this function: