feat: add collisions
This commit is contained in:
29
src/utils.py
29
src/utils.py
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user