79630381

Date: 2025-05-20 12:01:35
Score: 1.5
Natty:
Report link

To check if Polygon A is fully inside Polygon B, follow two steps:

  1. Edge Intersection Check

    • Loop through all edges of both polygons.

    • If any edge of A intersects any edge of B → A is not inside B.

  2. Vertex Inside Check

    • If no intersections, check if all vertices of A are inside B using ray casting.

If both conditions pass, Polygon A is fully inside Polygon B.

def do_edges_intersect(p1, p2, q1, q2):
    def orientation(a, b, c):
        val = (b[1]-a[1])*(c[0]-b[0]) - (b[0]-a[0])*(c[1]-b[1])
        return 0 if val == 0 else (1 if val > 0 else 2)

    def on_segment(a, b, c):
        return min(a[0],c[0]) <= b[0] <= max(a[0],c[0]) and min(a[1],c[1]) <= b[1] <= max(a[1],c[1])

    o1 = orientation(p1, p2, q1)
    o2 = orientation(p1, p2, q2)
    o3 = orientation(q1, q2, p1)
    o4 = orientation(q1, q2, p2)

    if o1 != o2 and o3 != o4: return True
    if o1 == 0 and on_segment(p1, q1, p2): return True
    if o2 == 0 and on_segment(p1, q2, p2): return True
    if o3 == 0 and on_segment(q1, p1, q2): return True
    if o4 == 0 and on_segment(q1, p2, q2): return True
    return False

def is_point_inside_polygon(point, polygon):
    x, y = point
    count = 0
    for i in range(len(polygon)):
        x1, y1 = polygon[i]
        x2, y2 = polygon[(i+1)%len(polygon)]
        if y1 > y2: x1, y1, x2, y2 = x2, y2, x1, y1
        if y == y1 or y == y2: y += 1e-10
        if y > y1 and y < y2 and x < (x2 - x1)*(y - y1)/(y2 - y1) + x1:
            count += 1
    return count % 2 == 1

def is_polygon_inside(polygon_a, polygon_b):
    for i in range(len(polygon_a)):
        a1 = polygon_a[i]
        a2 = polygon_a[(i+1)%len(polygon_a)]
        for j in range(len(polygon_b)):
            b1 = polygon_b[j]
            b2 = polygon_b[(j+1)%len(polygon_b)]
            if do_edges_intersect(a1, a2, b1, b2):
                return False
    return all(is_point_inside_polygon(p, polygon_b) for p in polygon_a)

a = [(2, 2), (3, 2), (3, 3), (2, 3)]
b = [(0, 0), (5, 0), (5, 5), (0, 5)]
print(is_polygon_inside(a, b))  # True

Does this approach have any edge cases or loopholes I missed? Would appreciate suggestions or improvements!

Reasons:
  • Blacklisted phrase (1.5): Would appreciate
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Sriteja Chinthala