banner
NEWS LETTER

Python计算几何模板

Scroll down

代码

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
from dataclasses import dataclass
from decimal import Decimal, getcontext
from enum import Enum, auto
import math
from typing import Self

getcontext().prec = 100
PI = Decimal(314159265_358979323846264_338327950288_419716939937510) / Decimal(10**50)


_epsilon_exponent = max(1, getcontext().prec - 20)
EPSILON = Decimal("1e-" + str(_epsilon_exponent))


@dataclass
class Vector:
x: Decimal
y: Decimal

def __add__(self, other):
if not isinstance(other, Vector):
return NotImplemented
return Vector(self.x + other.x, self.y + other.y)

def __sub__(self, other):
if not isinstance(other, Vector):
return NotImplemented
return Vector(self.x - other.x, self.y - other.y)

def __mul__(self, other):
if isinstance(other, (int, float, Decimal)):
return Vector(self.x * other, self.y * other)
raise TypeError("Multiplication is only supported with a scalar.")

def __truediv__(self, other):
if isinstance(other, (int, float, Decimal)):
if other == 0:
raise ZeroDivisionError("Cannot divide vector by zero.")
return Vector(self.x / other, self.y / other)
raise TypeError("Division is only supported by a scalar.")

def __neg__(self):
return Vector(-self.x, -self.y)

def __str__(self):
return f"({self.x}, {self.y})"

def __repr__(self):
return f"Vector({self.x}, {self.y})"

def __eq__(self, other: Self):
if not isinstance(other, Vector):
return NotImplemented
return self.x == other.x and self.y == other.y

def cross_product(self, other: Self) -> Decimal:
"""计算二维叉积 (self x other)"""
if not isinstance(other, Vector):
raise TypeError("Cross product is only supported between two Vectors.")
return self.x * other.y - self.y * other.x

def dot_product(self, other: Self) -> Decimal:
"""计算二维点积 (self . other)"""
if not isinstance(other, Vector):
raise TypeError("Dot product is only supported between two Vectors.")
return self.x * other.x + self.y * other.y

def magnitude(self) -> Decimal:
"""计算向量的模长"""
return (self.x**2 + self.y**2).sqrt()

def normalized(self) -> Self:
"""返回单位向量"""
magnitude = self.magnitude()
if magnitude == 0:
raise ZeroDivisionError("Cannot normalize a zero vector.")
return Vector(self.x / magnitude, self.y / magnitude)

def angle(self, other: Self) -> Decimal:
"""计算两个向量之间的夹角 (弧度)"""
if not isinstance(other, Vector):
raise TypeError("Angle calculation is only supported between two Vectors.")

mag_self = self.magnitude()
mag_other = other.magnitude()

if mag_self == 0 or mag_other == 0:
raise ValueError("Cannot calculate angle with a zero vector.")

dot_prod = self.dot_product(other)
cos_theta = dot_prod / (mag_self * mag_other)

cos_theta = max(Decimal("-1"), min(Decimal("1"), cos_theta))

return Decimal(math.acos(cos_theta))

def angle_signed(self, other: Self) -> Decimal:
"""
计算从 self 到 other 的有向夹角 (弧度)
"""
if not isinstance(other, Vector):
raise TypeError(
"Signed angle calculation is only supported between two Vectors."
)

mag_self = self.magnitude()
mag_other = other.magnitude()

if mag_self == 0 or mag_other == 0:
raise ValueError("Cannot calculate signed angle with a zero vector.")

cross_prod = self.cross_product(other)
dot_prod = self.dot_product(other)

if abs(dot_prod) < EPSILON and abs(cross_prod) < EPSILON:
raise ValueError("Cannot calculate angle with a zero vector.")

if abs(dot_prod) < EPSILON:
if cross_prod > 0:
return PI / 2
elif cross_prod < 0:
return -PI / 2
else:
return Decimal(0)
else:
angle_val = Decimal(math.atan(cross_prod / dot_prod))
if dot_prod < 0:
if cross_prod >= 0:
angle_val += PI
else:
angle_val -= PI
return angle_val

def __abs__(self) -> Decimal:
return self.Magnitude()

def rotate(self, angle: Decimal) -> Self:
"""
将向量绕原点旋转指定角度 (弧度)
"""
cos_a = Decimal(math.cos(angle))
sin_a = Decimal(math.sin(angle))
new_x = self.x * cos_a - self.y * sin_a
new_y = self.x * sin_a + self.y * cos_a
return Vector(new_x, new_y)


@dataclass
class Point:
x: Decimal
y: Decimal

def __sub__(self, other: "Point") -> Vector:
"""两点相减得到一个向量。"""
if not isinstance(other, Point):
return NotImplemented
return Vector(self.x - other.x, self.y - other.y)

def __add__(self, other: Vector) -> "Point":
"""点与向量相加得到一个新点。"""
if not isinstance(other, Vector):
return NotImplemented
return Point(self.x + other.x, self.y + other.y)

def __eq__(self, other: Self) -> bool:
if not isinstance(other, Point):
return NotImplemented
return self.x == other.x and self.y == other.y

def __str__(self) -> str:
return f"({self.x}, {self.y})"

def __repr__(self) -> str:
return f"Point({self.x}, {self.y})"


@dataclass
class Line:
p: Point
v: Vector

def is_point_on_line(self, q: Point) -> bool:
"""
检查点 Q 是否在直线上
"""
if not isinstance(q, Point):
raise TypeError("Input q must be a Point.")

pq_vector = q - self.p

cross_prod_val = pq_vector.cross_product(self.v)

return abs(cross_prod_val) < EPSILON

def distance_to_point(self, q: Point) -> Decimal:
"""
计算点 Q 到直线的距离
"""
if not isinstance(q, Point):
raise TypeError("Input q must be a Point.")

pq_vector = q - self.p
v_magnitude_squared = self.v.x**2 + self.v.y**2

if v_magnitude_squared == 0:
return ((q.x - self.p.x) ** 2 + (q.y - self.p.y) ** 2).sqrt()

distance = abs(pq_vector.cross_product(self.v)) / v_magnitude_squared.sqrt()
return distance

def intersection(self, other: Self) -> Point | None:
"""
计算两条直线的交点
"""
if not isinstance(other, Line):
raise TypeError("Input other must be a Line.")

cross_prod_v = self.v.cross_product(other.v)

if abs(cross_prod_v) < EPSILON:
if abs((other.p - self.p).cross_product(self.v)) < EPSILON:
return None
else:
return None

t_numerator = (other.p - self.p).cross_product(other.v)
t = t_numerator / cross_prod_v

intersection_point = self.p + self.v * t

return intersection_point

def is_parallel_to(self, other: Self) -> bool:
"""
检查两条直线是否平行
"""
if not isinstance(other, Line):
raise TypeError("Input other must be a Line.")

cross_prod_v = self.v.cross_product(other.v)

return abs(cross_prod_v) < EPSILON

def get_perpendicular_line_through_point(self, q: Point) -> Self:
"""
获取通过点 Q 且垂直于当前直线的直线
"""
if not isinstance(q, Point):
raise TypeError("Input q must be a Point.")

v_perp = Vector(-self.v.y, self.v.x)

if self.v.x == 0 and self.v.y == 0:
raise ValueError("Cannot get perpendicular line from a zero vector line.")

return Line(q, v_perp)

def is_coincident_with(self, other: Self) -> bool:
"""
检查两条直线是否重合
"""
if not isinstance(other, Line):
raise TypeError("Input other must be a Line.")

if not self.is_parallel_to(other):
return False

return abs((other.p - self.p).cross_product(self.v)) < EPSILON

def __contains__(self, point: Point) -> bool:
return self.is_point_on_line(point)

def __eq__(self, value: Self):
if not isinstance(value, Line):
return NotImplemented
return self.p == value.p and self.v == value.v


@dataclass
class Polygon:
points: list[Point]

def area(self) -> Decimal:
"""计算多边形的面积"""
n = len(self.points)
if n < 3:
return Decimal(0)

area = Decimal(0)

for i in range(n):
p1 = self.points[i]
p2 = self.points[(i + 1) % n]

area += p1.x * p2.y
area -= p1.y * p2.x

return abs(area) / Decimal(2)

def is_convex(self) -> bool:
"""
检查多边形是否为凸多边形
"""
n = len(self.points)
if n < 3:
return True

v1 = self.points[1] - self.points[0]
v2 = self.points[2] - self.points[1]
first_cross_product = v1.cross_product(v2)

for i in range(n):
p1 = self.points[i]
p2 = self.points[(i + 1) % n]
p3 = self.points[(i + 2) % n]

vec1 = p2 - p1
vec2 = p3 - p2

current_cross_product = vec1.cross_product(vec2)

if first_cross_product > 0 and current_cross_product < 0:
return False
if first_cross_product < 0 and current_cross_product > 0:
return False

if first_cross_product == 0 and current_cross_product != 0:
first_cross_product = current_cross_product
return True

def __contains__(self, point: Point) -> bool:
"""
检查点是否在多边形内,包括边界和顶点
"""
if not isinstance(point, Point):
raise TypeError("Input point must be a Point.")

n = len(self.points)
if n < 3:
return False

crossings = 0
for i in range(n):
p1 = self.points[i]
p2 = self.points[(i + 1) % n]

if (p1.y <= point.y < p2.y) or (p2.y <= point.y < p1.y):
x_at_y = p1.x + (point.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y)
if point.x < x_at_y:
crossings += 1

return crossings % 2 == 1


class CirclePos(Enum):
INTERSECT = auto()
TANGENT = auto()
DISJOINT = auto()
CONTAINS = auto()
CONTAINED_BY = auto()
EQUAL = auto()


@dataclass
class Circle:
center: Point
radius: Decimal

def area(self) -> Decimal:
return PI * self.radius**2

def circumference(self) -> Decimal:
return 2 * PI * self.radius

def intersection_with_line(self, line: Line) -> list[Point]:
"""
计算圆和直线的交点
"""
if not isinstance(line, Line):
raise TypeError("Input line must be a Line.")

pc_vector = self.center - line.p

v_magnitude_squared = line.v.x**2 + line.v.y**2

if v_magnitude_squared == 0:
raise ValueError("Line direction vector cannot be zero.")

distance_to_line = (
abs(pc_vector.cross_product(line.v)) / v_magnitude_squared.sqrt()
)

if distance_to_line > self.radius + EPSILON:
return []
elif abs(distance_to_line - self.radius) <= EPSILON:
dot_product_pc_v = pc_vector.x * line.v.x + pc_vector.y * line.v.y
t = dot_product_pc_v / v_magnitude_squared
tangent_point = line.p + line.v * t
return [tangent_point]
else:
dot_product_pc_v = pc_vector.x * line.v.x + pc_vector.y * line.v.y
t_closest = dot_product_pc_v / v_magnitude_squared
closest_point_on_line = line.p + line.v * t_closest

distance_from_closest_to_intersection_squared = (
self.radius**2 - distance_to_line**2
)

if distance_from_closest_to_intersection_squared < 0:
distance_from_closest_to_intersection = Decimal(0)
else:
distance_from_closest_to_intersection = (
distance_from_closest_to_intersection_squared.sqrt()
)

v_magnitude = v_magnitude_squared.sqrt()
if v_magnitude == 0:
raise ValueError("Line direction vector cannot be zero.")

unit_v = line.v / v_magnitude

intersection1 = (
closest_point_on_line + unit_v * distance_from_closest_to_intersection
)
intersection2 = (
closest_point_on_line - unit_v * distance_from_closest_to_intersection
)

return [intersection1, intersection2]

def intersection_with_circle(self, other: Self) -> list[Point]:
"""
计算两个圆的交点
"""
if not isinstance(other, Circle):
raise TypeError("Input other must be a Circle.")

if self.center == other.center:
if self.radius == other.radius:
return []
else:
return []

distance_between_centers = (self.center - other.center).magnitude()

sum_of_radii = self.radius + other.radius
diff_of_radii = abs(self.radius - other.radius)

if distance_between_centers > sum_of_radii + EPSILON:
return []
elif abs(distance_between_centers - sum_of_radii) <= EPSILON:
direction_vector = (other.center - self.center).normalized()
intersection_point = self.center + direction_vector * self.radius
return [intersection_point]
elif distance_between_centers < diff_of_radii - EPSILON:
return []
elif abs(distance_between_centers - diff_of_radii) <= EPSILON:
direction_vector = (other.center - self.center).normalized()
intersection_point = self.center + direction_vector * self.radius
return [intersection_point]
else:
cx1, cy1 = self.center.x, self.center.y
cx2, cy2 = other.center.x, other.center.y
r1, r2 = self.radius, other.radius

A = 2 * (cx2 - cx1)
B = 2 * (cy2 - cy1)
C = (r1**2 - r2**2) - (cx1**2 - cx2**2) - (cy1**2 - cy2**2)

if abs(B) < EPSILON:
if abs(A) < EPSILON:
return []

x_val = C / A
discriminant = r1**2 - (x_val - cx1) ** 2

if discriminant < -EPSILON:
return []
elif abs(discriminant) <= EPSILON:
y_val = cy1
return [Point(x_val, y_val)]
else:
y1 = cy1 + discriminant.sqrt()
y2 = cy1 - discriminant.sqrt()
return [Point(x_val, y1), Point(x_val, y2)]
else:
a = 1 + (A / B) ** 2
k = C / B - cy1
b = -2 * (cx1 + k * (A / B))
c = cx1**2 + k**2 - r1**2

discriminant = b**2 - 4 * a * c

if discriminant < -EPSILON:
return []
elif abs(discriminant) <= EPSILON:
x_val = -b / (2 * a)
y_val = (C - A * x_val) / B
return [Point(x_val, y_val)]
else:
x1 = (-b + discriminant.sqrt()) / (2 * a)
x2 = (-b - discriminant.sqrt()) / (2 * a)
y1 = (C - A * x1) / B
y2 = (C - A * x2) / B
return [Point(x1, y1), Point(x2, y2)]

def relationship_with_point(self, point: Point) -> CirclePos:
"""确定圆和点的关系"""
if not isinstance(point, Point):
raise TypeError("Input point must be a Point.")

distance_squared = (point.x - self.center.x) ** 2 + (
point.y - self.center.y
) ** 2
radius_squared = self.radius**2

if distance_squared > radius_squared + EPSILON:
return CirclePos.DISJOINT
elif abs(distance_squared - radius_squared) <= EPSILON:
return CirclePos.TANGENT
else:
return CirclePos.INTERSECT

def relationship_with_line(self, line: Line) -> CirclePos:
"""确定圆和直线的关系"""
if not isinstance(line, Line):
raise TypeError("Input line must be a Line.")

distance_to_line = self.distance_to_point(line)

if distance_to_line > self.radius + EPSILON:
return CirclePos.DISJOINT
elif abs(distance_to_line - self.radius) <= EPSILON:
return CirclePos.TANGENT
else:
return CirclePos.INTERSECT

def relationship_with_circle(self, other: Self) -> list[CirclePos]:
"""确定两个圆的关系"""
if not isinstance(other, Circle):
raise TypeError("Input other must be a Circle.")

distance_between_centers = (self.center - other.center).magnitude()

sum_of_radii = self.radius + other.radius
diff_of_radii = abs(self.radius - other.radius)

if distance_between_centers > sum_of_radii + EPSILON:
return [CirclePos.DISJOINT]
elif abs(distance_between_centers - sum_of_radii) <= EPSILON:
return [CirclePos.TANGENT]
elif distance_between_centers < diff_of_radii - EPSILON:
if self.radius > other.radius:
return [CirclePos.CONTAINS]
elif other.radius > self.radius:
return [CirclePos.CONTAINED_BY]
else:
return [CirclePos.EQUAL]
elif abs(distance_between_centers - diff_of_radii) <= EPSILON:
return [CirclePos.TANGENT, CirclePos.CONTAINS]
else:
return [CirclePos.INTERSECT]

def intersection_area_with_circle(self, other: Self) -> Decimal:
"""
计算两个圆的相交面积

"""
if not isinstance(other, Circle):
raise TypeError("Input other must be a Circle.")

if self.center == other.center:
if self.radius == other.radius:
return self.area()
else:
return min(self.area(), other.area())

distance_between_centers = (self.center - other.center).magnitude()

sum_of_radii = self.radius + other.radius
diff_of_radii = abs(self.radius - other.radius)

if distance_between_centers > sum_of_radii + EPSILON:
return Decimal(0)
elif abs(distance_between_centers - sum_of_radii) <= EPSILON:
return Decimal(0)
elif distance_between_centers < diff_of_radii - EPSILON:
return min(self.area(), other.area())
elif abs(distance_between_centers - diff_of_radii) <= EPSILON:
return min(self.area(), other.area())
else:
r1, r2 = self.radius, other.radius
d = distance_between_centers

if d == 0:
if r1 == r2:
return self.area()
else:
return min(self.area(), other.area())

arg1 = (r1**2 + d**2 - r2**2) / (2 * r1 * d)
arg2 = (r2**2 + d**2 - r1**2) / (2 * r2 * d)

if abs(arg1) > 1:
arg1 = Decimal(1) if arg1 > 1 else Decimal(-1)
if abs(arg2) > 1:
arg2 = Decimal(1) if arg2 > 1 else Decimal(-1)

theta1 = Decimal(math.acos(arg1))
theta2 = Decimal(math.acos(arg2))

return r1**2 * theta1 + r2**2 * theta2 - d * r1 * Decimal(math.sin(theta1))

def __contains__(self, point: Point) -> bool:
"""检查点是否在圆内"""
if not isinstance(point, Point):
raise TypeError("Input point must be a Point.")
distance_squared = (point.x - self.center.x) ** 2 + (
point.y - self.center.y
) ** 2
return distance_squared <= self.radius**2

一些例题

https://www.luogu.com.cn/record/218577892
P1183 多边形的面积

https://www.luogu.com.cn/record/218580033
P1652 圆

我很可爱,请给我钱

其他文章
目录导航 置顶
  1. 1. 代码
  2. 2. 一些例题