-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_line.py
55 lines (46 loc) · 1.51 KB
/
scan_line.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
#This file stores the scan-line intersection
#algorithm
import copy
import one_color
def main( line, scan ):
temp = one_color.struc()
points = [ copy.deepcopy( one_color.struc ) ]
lineMax = 0
length = len( line )
if line[ 0 ].y < line[ length - 1 ].y:
start = line[ 0 ].y
end = line[ length - 1 ].y
else:
start = line[ length - 1 ].y
end = line[ 0 ].y
#Line
if line[ length - 1 ].y - line[ 0 ].y == 0:
#This is a horizontal line
del points[ 0 ]
return points
#If scan line is not within the edge
if scan not in range( start, end ):
del points[ 0 ]
return points
else:
if line[ 0 ].y >= line[ length - 1 ].y:
#y1 is maximum
lineMax = line[ 0 ].y
elif line[ 0 ].y < line[ length - 1 ].y:
#y2 is maximum
lineMax = line[ length - 1 ].y
if scan == lineMax:
#Max vertex point, no intersection
del points[ 0 ]
return points
elif scan in range( start, end ) and scan != lineMax:
frac = ( line[ length - 1 ].x - line[ 0 ].x ) / ( line[ length - 1 ].y - line[ 0 ].y )
first = frac * scan
second = ( frac * line[ 0 ].y )
third = first - second
newX = third + line[ 0 ].x
temp.x = round( newX )
temp.y = scan
points.append( copy.deepcopy( temp ) )
del points[ 0 ]
return points