Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cg/__init__.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion cg/dcel.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]:
Expand Down
23 changes: 22 additions & 1 deletion cg/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down