-
Notifications
You must be signed in to change notification settings - Fork 4
/
perimeter.py
59 lines (50 loc) · 1.7 KB
/
perimeter.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
import math
from collections import defaultdict
import numpy as np
def linesToVoxels(lineList, pixels):
for x in range(len(pixels)):
isBlack = False
lines = list(findRelevantLines(lineList, x))
targetYs = list(map(lambda line:int(generateY(line,x)),lines))
for y in range(len(pixels[x])):
if isBlack:
pixels[x][y] = True
if y in targetYs:
for line in lines:
if onLine(line, x, y):
isBlack = not isBlack
pixels[x][y] = True
if isBlack:
print("an error has occured at x%sz%s"%(x,lineList[0][0][2]))
def findRelevantLines(lineList, x, ind=0):
for line in lineList:
same = False
above = False
below = False
for pt in line:
if pt[ind] > x:
above = True
elif pt[ind] == x:
same = True
else:
below = True
if above and below:
yield line
elif same and above:
yield line
def generateY(line, x):
if line[1][0] == line[0][0]:
return -1
ratio = (x - line[0][0]) / (line[1][0] - line[0][0])
ydist = line[1][1] - line[0][1]
newy = line[0][1] + ratio * ydist
return newy
def onLine(line, x, y):
newy = generateY(line, x)
if int(newy) != y:
return False
if int(line[0][0]) != x and int(line[1][0]) != x and (max(line[0][0], line[1][0]) < x or min(line[0][0], line[1][0]) > x):
return False
if int(line[0][1]) != y and int(line[1][1]) != y and (max(line[0][1], line[1][1]) < y or min(line[0][1], line[1][1]) > y):
return False
return True