-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.py
174 lines (147 loc) · 4.67 KB
/
geo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import copy
class Point:
def __init__(self, x=0, y=0, z=0):
#assert type(x) in [int, float], type(x)
self.x = x
self.y = y
self.z = z
@classmethod
def from_Point(cls, pt):
return cls(pt.x, pt.y, pt.z)
@classmethod
def from_tuple(cls, tup):
return cls(tup[0], tup[1], tup[2])
def __add__(self, other):
#print(type(self), type(other))
return Point(self.x+other.x, self.y+other.y, self.z+other.z) #, self.d+other.d)
def __str__(self):
return f"<{self.x}, {self.y}, {self.z}>" #", {self.d}"
def __repr__(self):
return str(self)
def __eq__(self, other):
if type(other) is tuple:
return self.x == other[0] and self.y == other[1] and self.z == other[2]
elif other is None:
return False
return self.x == other.x and self.y == other.y and self.z == other.z #and self.d == other.d
def __hash__(self):
return hash(self.tuple()) #, self.d)))
@classmethod
def new(cls, pt):
return cls(pt.x, pt.y, pt.z) #, pt.d)
def tuple(self):
return (self.x, self.y, self.z)
def Rotate(self, direction):
rotatedCoords = Point(0, 0)
direction = direction & 3
#print(direction)
if direction == 3:
rotatedCoords.x = -self.y;
rotatedCoords.y = self.x;
elif direction == 0:
rotatedCoords.x = self.x;
rotatedCoords.y = self.y;
elif direction == 1:
rotatedCoords.x = self.y;
rotatedCoords.y = -self.x;
elif direction == 2:
rotatedCoords.x = -self.x;
rotatedCoords.y = -self.y;
else:
raise Exception(direction)
return rotatedCoords
class Vector:
def __init__(self, pt, dir_=None): #, diag_=None):
if type(pt) is Vector:
self.pt = Point.new(pt.pt)
self.direction = pt.direction
#self.diag = pt.diag
else:
self.pt = pt
self.direction = dir_
#self.diag = diag_
@classmethod
def new(cls, v):
return cls(Point.new(v.pt), v.direction) #, v.diag)
def __eq__(self, other):
return self.direction == other.direction and self.pt == other.pt #and self.diag == other.diag
def __hash__(self):
return hash(tuple((self.pt, self.direction))) #, self.diag)))
def __str__(self):
return f"{self.pt}, {self.direction}" #", {self.diag}"
COORDS_XY_STEP = 32
CoordsDirectionDelta = [
Point( -COORDS_XY_STEP, 0 ),
Point( 0, +COORDS_XY_STEP ),
Point( +COORDS_XY_STEP, 0 ),
Point( 0, -COORDS_XY_STEP ),
Point( -COORDS_XY_STEP, +COORDS_XY_STEP ),
Point( +COORDS_XY_STEP, +COORDS_XY_STEP ),
Point( +COORDS_XY_STEP, -COORDS_XY_STEP ),
Point( -COORDS_XY_STEP, -COORDS_XY_STEP )
]
# adapted from https://www.geeksforgeeks.org/bresenhams-algorithm-for-3-d-line-drawing/
def Bresenham3D(x1, y1, z1, x2, y2, z2):
ListOfPoints = []
ListOfPoints.append((x1, y1, z1))
dx = abs(x2 - x1)
dy = abs(y2 - y1)
dz = abs(z2 - z1)
if (x2 > x1):
xs = 1
else:
xs = -1
if (y2 > y1):
ys = 1
else:
ys = -1
if (z2 > z1):
zs = 1
else:
zs = -1
# Driving axis is X-axis"
if (dx >= dy and dx >= dz):
p1 = 2 * dy - dx
p2 = 2 * dz - dx
while (x1 != x2):
x1 += xs
if (p1 >= 0):
y1 += ys
p1 -= 2 * dx
if (p2 >= 0):
z1 += zs
p2 -= 2 * dx
p1 += 2 * dy
p2 += 2 * dz
ListOfPoints.append((x1, y1, z1))
# Driving axis is Y-axis"
elif (dy >= dx and dy >= dz):
p1 = 2 * dx - dy
p2 = 2 * dz - dy
while (y1 != y2):
y1 += ys
if (p1 >= 0):
x1 += xs
p1 -= 2 * dy
if (p2 >= 0):
z1 += zs
p2 -= 2 * dy
p1 += 2 * dx
p2 += 2 * dz
ListOfPoints.append((x1, y1, z1))
# Driving axis is Z-axis"
else:
p1 = 2 * dy - dz
p2 = 2 * dx - dz
while (z1 != z2):
z1 += zs
if (p1 >= 0):
y1 += ys
p1 -= 2 * dz
if (p2 >= 0):
x1 += xs
p2 -= 2 * dz
p1 += 2 * dy
p2 += 2 * dx
ListOfPoints.append((x1, y1, z1))
return [Point(x, y, z) for x, y, z in ListOfPoints]