feat: add collisions

This commit is contained in:
2025-10-18 21:02:24 +02:00
parent 45ed1c85c8
commit 09f70223b8
7 changed files with 89 additions and 0 deletions

View File

@@ -1,5 +1,34 @@
import os
from pathlib import Path
from src.vec import Vec
ROOT = Path(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
def orientation(a: Vec, b: Vec, c: Vec) -> float:
return (b - a).cross(c - a)
def segments_intersect(a1: Vec, a2: Vec, b1: Vec, b2: Vec) -> bool:
o1 = orientation(a1, a2, b1)
o2 = orientation(a1, a2, b2)
o3 = orientation(b1, b2, a1)
o4 = orientation(b1, b2, a2)
# General case: segments straddle each other
if (o1 * o2 < 0) and (o3 * o4 < 0):
return True
# Special cases: Collinear overlaps
if o1 == 0 and b1.within(a1, a2):
return True
if o2 == 0 and b2.within(a1, a2):
return True
if o3 == 0 and a1.within(b1, b2):
return True
if o4 == 0 and a2.within(b1, b2):
return True
return False