forked from gisalgs/geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linesegment.py
69 lines (67 loc) · 2.31 KB
/
linesegment.py
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
from point import *
from sideplr import *
## Two statuses of the left endpoint
ENDPOINT = 0 ## original left endpoint
INTERIOR = 1 ## interior in the segment
class Segment:
"""
A class for line segments.
"""
def __init__(self, e, p0, p1, c=None):
"""
Constructor of Segment class.
Input
e: segment ID, an integer
p0, p1: endpoints of segment, Point objects
"""
if p0>=p1:
p0,p1 = p1,p0 # p0 is always left
self.edge = e # ID, in all edges
self.lp = p0 # left point
self.lp0 = p0 # original left point #*@\label{lineseg:lp0}
self.rp = p1 # right point
self.status = ENDPOINT # status of segment
self.c = c # c: feature ID
def __eq__(self, other):
if isinstance(other, Segment):
return (self.lp==other.lp and self.rp==other.rp)\
or (self.lp==other.rp and self.rp==other.lp)
return NotImplemented
def __ne__(self, other):
result = self.__eq__(other)
if result is NotImplemented:
return result
return not result
def __lt__(self, other):
if isinstance(other, Segment):
if self.lp and other.lp:
lr = sideplr(self.lp, other.lp, other.rp)
if lr == 0:
lrr = sideplr(self.rp, other.lp, other.rp)
if other.lp.x < other.rp.x:
return lrr > 0
else:
return lrr < 0
else:
if other.lp.x > other.rp.x:
return lr < 0
else:
return lr > 0
return NotImplemented
def __gt__(self, other):
result = self.__lt__(other)
if result is NotImplemented:
return result
return not result
def __repr__(self):
return "{0}".format(self.edge)
def contains(self, p):
"""
Returns none zero if segment has p as an endpoint
"""
if self.lp == p:
return -1
elif self.rp == p:
return 1
else:
return 0