-
Notifications
You must be signed in to change notification settings - Fork 0
/
bresenham.lua
98 lines (87 loc) · 1.72 KB
/
bresenham.lua
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
function Bresenham3D(x1, y1, z1, x2, y2, z2)
local ListOfPoints = {}
table.insert(ListOfPoints, {x1, y1, z1})
local dx = x2 - x1
local dy = y2 - y1
local dz = z2 - z1
local xs, ys, zs, p1, p2
if (x2 > x1) then
xs = 1
else
xs = -1
end
if (y2 > y1) then
ys = 1
else
ys = -1
end
if (z2 > z1) then
zs = 1
else
zs = -1
end
if (dx >= dy and dx >= dz) then
p1 = 2 * dy - dx
p2 = 2 * dz - dx
while (x1 ~= x2) do
x1 = x1 + xs
if (p1 >= 0) then
y1 = y1 + ys
p1 = p1 - 2 * dx
end
if (p2 >= 0) then
z1 = z1 + zs
p2 = p2 - 2 * dx
end
p1 = p1 + 2 * dy
p2 = p2 + 2 * dz
table.insert(ListOfPoints, {x1, y1, z1})
end
elseif (dy >= dx and dy >= dz) then
p1 = 2 * dx - dy
p2 = 2 * dz - dy
while (y1 ~= y2) do
y1 = y1 + ys
if (p1 >= 0) then
x1 = x1 + xs
p1 = p1 - 2 * dy
end
if (p2 >= 0) then
z1 = z1 + zs
p2 = p2 - 2 * dy
end
p1 = p1 + 2 * dx
p2 = p2 + 2 * dz
table.insert( ListOfPoints, {x1, y1, z1})
end
else
p1 = 2 * dy - dz
p2 = 2 * dx - dz
while (z1 ~= z2) do
z1 = z1 + zs
if (p1 >= 0) then
y1 = y1 + ys
p1 = p1 - 2 * dz
end
if (p2 >= 0) then
x1 = x1 + xs
p2 = p2 - 2 * dz
end
p1 = p1 + 2 * dy
p2 = p2 + 2 * dx
table.insert( ListOfPoints, {x1, y1, z1})
end
end
return ListOfPoints
end
x1, y1, z1 = 0, 5, 0
x2, y2, z2 = 10, 30, 10
ListOfPoints = Bresenham3D(x1, y1, z1, x2, y2, z2)
local basepart = Instance.new('Part')
basepart.Size = Vector3.new(1, 1, 1)
for i, points in pairs(ListOfPoints) do
local part = basepart:Clone()
part.Position = Vector3.new(points[1], points[2], points[3])
part.Anchored = true
part.Parent = workspace
end