diff --git a/cg/__init__.py b/cg/__init__.py index 694712e..3a6b82c 100644 --- a/cg/__init__.py +++ b/cg/__init__.py @@ -1,2 +1,2 @@ -from .point import Point, turn +from .point import Point, turn, dist2, intersects, intersects from .dcel import Vertex, Face, walk_around, walk_along diff --git a/cg/dcel.py b/cg/dcel.py index a859709..01f7092 100644 --- a/cg/dcel.py +++ b/cg/dcel.py @@ -1,5 +1,5 @@ import numpy as np -from cg import Point, turn +from cg import Point, turn, in_triangle from cg.utils import look_back from typing import List, Iterator from operator import methodcaller @@ -60,6 +60,9 @@ def nxt(self, point: Vertex): def prv(self, point: Vertex): return self.step(point, Face.cw) + def contains(self, point: Vertex): + return in_triangle(q, self.vertices[0], self.vertices[1], self.vertices[2]) + def __getitem__(self, item: Vertex) -> int: i = 0 while item != self.vertices[i]: diff --git a/cg/point.py b/cg/point.py index 5f261a2..1b90db4 100644 --- a/cg/point.py +++ b/cg/point.py @@ -191,7 +191,28 @@ def vol(point: Point, *hyperplane): def turn(point: Point, *hyperplane): return np.sign(vol(point, *hyperplane)) - + +''' +Возвращает True, если отрезки ab и cd пересекаются +''' +def intersects(a: Point, b: Point, c: Point, d: Point): + ''' + Вспомогательная функция. Вовращает true, если на числовой прямой отрезки ab и cd пересекаются + ''' + def bb (a, b, c, d): + if a > b: + a, b = b, a + if c > d: + c, d = d, c + return max(a, c) <= min(b, d) + + return bb(a.x(), b.x(), c.x(), d.x()) and \ + bb(a.y(), b.y(), c.y(), d.y()) and \ + turn(a, b, c) * turn(a, b, d) <= 0 and \ + turn(c, d, a) * turn(c, d, b) <= 0 + +def in_triangle(x: Point, a: Point, b: Point, c: Point): + return turn(a, b, x) >= 0 and turn(b, c, x) >= 0 and turn(c, a, x) >= 0 class PointSet: """