-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbresenham3d.py
125 lines (113 loc) · 2.87 KB
/
bresenham3d.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
# Python3 code for generating points on a 3-D and 2-D line
# using Bresenham's Algorithm
# from https://www.geeksforgeeks.org/bresenhams-algorithm-for-3-d-line-drawing/
# and https://www.geeksforgeeks.org/chain-code-for-2d-line/
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 ListOfPoints
def Bresenham2D(x1, y1, x2, y2):
ListOfPoints = []
ListOfPoints.append((x1, y1))
xdif = x2 - x1
ydif = y2 - y1
dx = abs(xdif)
dy = abs(ydif)
if(xdif > 0):
xs = 1
else:
xs = -1
if (ydif > 0):
ys = 1
else:
ys = -1
if (dx > dy):
# Driving axis is the X-axis
p = 2 * dy - dx
while (x1 != x2):
x1 += xs
if (p >= 0):
y1 += ys
p -= 2 * dx
p += 2 * dy
ListOfPoints.append((x1, y1))
else:
# Driving axis is the Y-axis
p = 2 * dx-dy
while(y1 != y2):
y1 += ys
if (p >= 0):
x1 += xs
p -= 2 * dy
p += 2 * dx
ListOfPoints.append((x1, y1))
return ListOfPoints
def main():
(x1, y1, z1) = (-1, 1, 1)
(x2, y2, z2) = (5, 3, -1)
ListOfPoints = Bresenham3D(x1, y1, z1, x2, y2, z2)
ListOfPoints2D = Bresenham2D(x1, y1, x2, y2)
print(ListOfPoints)
print(ListOfPoints2D)
if __name__ == '__main__':
main()