File size: 5,587 Bytes
551cc83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from __future__ import annotations

import math
from dataclasses import dataclass


@dataclass(frozen=True)
class Box:
    x_min: float
    y_min: float
    x_max: float
    y_max: float

    @property
    def width(self) -> float:
        return max(0.0, self.x_max - self.x_min)

    @property
    def height(self) -> float:
        return max(0.0, self.y_max - self.y_min)

    @property
    def area(self) -> float:
        return self.width * self.height

    def intersection(self, other: "Box") -> "Box | None":
        x_min = max(self.x_min, other.x_min)
        y_min = max(self.y_min, other.y_min)
        x_max = min(self.x_max, other.x_max)
        y_max = min(self.y_max, other.y_max)
        if x_max <= x_min or y_max <= y_min:
            return None
        return Box(x_min=x_min, y_min=y_min, x_max=x_max, y_max=y_max)

    def contains_point(self, x: float, y: float) -> bool:
        return self.x_min <= x <= self.x_max and self.y_min <= y <= self.y_max

    def to_polygon(self) -> tuple[tuple[float, float], ...]:
        return (
            (self.x_min, self.y_min),
            (self.x_max, self.y_min),
            (self.x_max, self.y_max),
            (self.x_min, self.y_max),
        )

    def to_list(self) -> list[float]:
        return [self.x_min, self.y_min, self.x_max, self.y_max]


def signed_polygon_area(points: list[tuple[float, float]] | tuple[tuple[float, float], ...]) -> float:
    if len(points) < 3:
        return 0.0

    area = 0.0
    for index, point in enumerate(points):
        next_point = points[(index + 1) % len(points)]
        area += point[0] * next_point[1] - next_point[0] * point[1]
    return area / 2.0


def polygon_area(points: list[tuple[float, float]] | tuple[tuple[float, float], ...]) -> float:
    return abs(signed_polygon_area(points))


def polygon_bounds(points: list[tuple[float, float]] | tuple[tuple[float, float], ...]) -> Box:
    return Box(
        x_min=min(point[0] for point in points),
        y_min=min(point[1] for point in points),
        x_max=max(point[0] for point in points),
        y_max=max(point[1] for point in points),
    )


def rotated_rectangle_points(
    x: float,
    y: float,
    width: float,
    height: float,
    rotation_degrees: float,
) -> tuple[tuple[float, float], ...]:
    rotation_radians = math.radians(rotation_degrees)
    cos_theta = math.cos(rotation_radians)
    sin_theta = math.sin(rotation_radians)
    return (
        (x, y),
        (x + width * cos_theta, y + width * sin_theta),
        (x + width * cos_theta - height * sin_theta, y + width * sin_theta + height * cos_theta),
        (x - height * sin_theta, y + height * cos_theta),
    )


def point_in_convex_polygon(
    point: tuple[float, float],
    polygon: tuple[tuple[float, float], ...],
) -> bool:
    if len(polygon) < 3:
        return False

    orientation = 1 if signed_polygon_area(polygon) >= 0 else -1
    point_x, point_y = point

    for index, start in enumerate(polygon):
        end = polygon[(index + 1) % len(polygon)]
        cross = ((end[0] - start[0]) * (point_y - start[1])) - (
            (end[1] - start[1]) * (point_x - start[0])
        )
        if orientation * cross < -1e-9:
            return False
    return True


def line_intersection(
    line1_start: tuple[float, float],
    line1_end: tuple[float, float],
    line2_start: tuple[float, float],
    line2_end: tuple[float, float],
) -> tuple[float, float]:
    x1, y1 = line1_start
    x2, y2 = line1_end
    x3, y3 = line2_start
    x4, y4 = line2_end

    denominator = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))
    if abs(denominator) < 1e-12:
        return line1_end

    determinant1 = (x1 * y2) - (y1 * x2)
    determinant2 = (x3 * y4) - (y3 * x4)
    x = ((determinant1 * (x3 - x4)) - ((x1 - x2) * determinant2)) / denominator
    y = ((determinant1 * (y3 - y4)) - ((y1 - y2) * determinant2)) / denominator
    return (x, y)


def polygon_intersection(
    subject_polygon: tuple[tuple[float, float], ...] | list[tuple[float, float]],
    clip_polygon: tuple[tuple[float, float], ...],
) -> list[tuple[float, float]]:
    output = list(subject_polygon)
    if len(output) < 3 or len(clip_polygon) < 3:
        return []

    orientation = 1 if signed_polygon_area(clip_polygon) >= 0 else -1

    def is_inside(point: tuple[float, float], edge_start: tuple[float, float], edge_end: tuple[float, float]) -> bool:
        cross = ((edge_end[0] - edge_start[0]) * (point[1] - edge_start[1])) - (
            (edge_end[1] - edge_start[1]) * (point[0] - edge_start[0])
        )
        return orientation * cross >= -1e-9

    for index, clip_start in enumerate(clip_polygon):
        clip_end = clip_polygon[(index + 1) % len(clip_polygon)]
        input_points = output
        output = []
        if not input_points:
            break

        previous_point = input_points[-1]
        for current_point in input_points:
            current_inside = is_inside(current_point, clip_start, clip_end)
            previous_inside = is_inside(previous_point, clip_start, clip_end)

            if current_inside:
                if not previous_inside:
                    output.append(
                        line_intersection(previous_point, current_point, clip_start, clip_end)
                    )
                output.append(current_point)
            elif previous_inside:
                output.append(
                    line_intersection(previous_point, current_point, clip_start, clip_end)
                )

            previous_point = current_point

    return output