Computational Geometry Questions Long
The algorithm for computing the intersection of two polygons in Computational Geometry can be described as follows:
1. Input: Two polygons P and Q, each represented by a list of vertices in counterclockwise order.
2. Initialize an empty list, intersection_points, to store the intersection points.
3. For each edge (u, v) in P, do the following steps:
a. Calculate the line equation of the edge (u, v) as Ax + By + C = 0, where A, B, and C are the coefficients of the line equation.
b. For each edge (x, y) in Q, calculate the line equation of the edge (x, y) as Dx + Ey + F = 0.
c. Calculate the determinant D = AE - BD.
d. If D is zero, the two lines are parallel or coincident. Skip to the next edge in P.
e. Calculate the intersection point (x_int, y_int) of the two lines using Cramer's rule:
x_int = (BE - CD) / D
y_int = (CD - AF) / D
f. Check if the intersection point (x_int, y_int) lies within the range of the edge (u, v) and (x, y) by checking if it is between the minimum and maximum x and y coordinates of the two edges. If it is, add the intersection point to the intersection_points list.
4. Repeat steps 3 for each edge in Q, considering each edge in Q against each edge in P.
5. If the intersection_points list is empty, the two polygons do not intersect. Return an empty list.
6. If the intersection_points list is not empty, sort the intersection points in counterclockwise order around the centroid of the intersection_points.
7. Return the intersection_points list, which represents the intersection of the two polygons.
This algorithm works by iterating through each edge of one polygon and checking for intersections with each edge of the other polygon. The intersection points are calculated using line equations and Cramer's rule. The resulting intersection points are then sorted in counterclockwise order around the centroid of the intersection points to ensure a consistent representation of the intersection.