-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_geom.py
More file actions
65 lines (59 loc) · 1.1 KB
/
Copy pathmy_geom.py
File metadata and controls
65 lines (59 loc) · 1.1 KB
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
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
# python please add this to standard library
def range2d(w, h):
return ((x,y) for x in range(w) for y in range(h))
# and this too
def box2d(x, y, w, h):
return ((a+x,b+y) for (a,b) in range2d(w,h))
def square2d(x, y, w, h):
# top
for i in range(w):
yield (x+i, y)
# sides
for i in range(h-2):
yield (x, y+i+1)
yield (x+w-1, y+i+1)
# bot
for i in range(w):
yield (x+i, y+h-1)
# seriously i'm using python to avoid this kind of
# trivial stuff
def line2d(x1, y1, x2, y2):
points = []
issteep = abs(y2-y1) > abs(x2-x1)
if issteep:
x1, y1 = y1, x1
x2, y2 = y2, x2
rev = False
if x1 > x2:
x1, x2 = x2, x1
y1, y2 = y2, y1
rev = True
deltax = x2 - x1
deltay = abs(y2-y1)
error = int(deltax / 2)
y = y1
ystep = None
if y1 < y2:
ystep = 1
else:
ystep = -1
for x in range(x1, x2 + 1):
if issteep:
points.append((y,x))
else:
points.append((x,y))
error -= deltay
if error < 0:
y += ystep
error += deltax
# Reverse list if coordinates were reversed
if rev:
points.reverse()
return points