# drawingAPI

Oct 16th, 2021
967
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local function drawTri(p1,p2,p3,c,pr) --Called to draw a triangle, given three points, a peripheral (pr), and a color (c).
2.     if p2.y < p1.y then p1,p2 = p2,p1 end --sort points
3.     if p3.y < p2.y then p2,p3 = p3,p2 end
4.     if p2.y < p1.y then p1,p2 = p2,p1 end
5.
6.     if p1.y == p2.y then --natural flat top
7.         if p2.x < p1.x then p1,p2 = p2,p1 end
8.         drawFlatBottom(p1,p2,p3,c,pr)
9.     elseif p2.y == p3.y then --natural flat bottom
10.         if p3.x < p2.x then p2,p3 = p3,p2 end
11.         drawFlatBottom(p1,p2,p3,c,pr)
12.     else
13.             --splitting vertex
14.         local alphaSplit = (p2.y - p1.y) / (p3.y - p1.y)
15.         local vi ={
16.             x = p1.x + ((p3.x - p1.x) * alphaSplit),
17.             y = p1.y + ((p3.y - p1.y) * alphaSplit),
18.             z = p1.z + ((p3.z - p1.z) * alphaSplit) }
19.         if pv2.x < vi.x then --Major right
20.             drawFlatBottom(p1,p2,vi,c,pr)
21.             drawFlatTop(p2,vi,p3,c,pr)
22.         else --Major left
23.             drawFlatBottom(p1,vi,p2,c,pr)
24.             drawFlatTop(vi,p2,p3,c,pr)
25.         end
26.     end
27. end
28.
29. local function pPixel() end --draw a pixel at coodinates x,y with color c, to a peripheral (pr)
30.
31. local function pPixelTerm(x,y,c)
32.     term.setCursorPos(x,y)
33.     term.setBackgroundColor(c)
34.     print(" ")
35. end
36.
37. local function pPixelMon(x,y,c,pr)
38.     pr.setCursorPos(x,y)
39.     pr.setCursorPos(c)
40.     print(" ")
41. end
42.
43. local function pPixelAR(x,y,c,pr)
44.     pr.horizontalLine(x, x, y, c)
45. end
46.
47. local function drawFlatTriangle() end --switches between terminal output and AR goggle output depending on bind mode.
48.
49. local function drawFlatTriangleCC(xStart,xEnd,y,c,pr)
50.     for x = xStart, xEnd do
51.         pPixel(x,y,c,pr)
52.     end
53. end
54.
55. local function drawFlatTriangleAR(xStart,xEnd,y,c,pr)
56.     pr.horizontalLine(xStart, xEnd, y, c)
57. end
58.
59. local function drawFlatBottom(p1,p2,p3,c,pr) --Draw the top portion of a triangle with a flat bottom.
60.     local slope1 = (p2.x - p1.x) / (p2.y - p1.y)
61.     local slope2 = (p3.x - p1.x) / (p3.y - p1.y)
62.     for y = p1.y, p3.y do
63.         local yP1 = (y+p1.y)
64.         local xStart = slope1 * yP1 + p1.x
65.         local xEnd =   slope2 * yP1 + p1.x
66.         drawFlatTriangle(xStart,xEnd,y,c,pr)
67.     end
68. end
69.
70. local function drawFlatTop(p1,p2,p3,c,pr) --Draw the bottom portion of a triangle with a flat top.
71.     local slope1 = (p3.x - p1.x) / (p3.y - p1.y)
72.     local slope2 = (p3.x - p2.x) / (p3.y - p2.y)
73.     for y = p1.y, p3.y do
74.         local xStart = slope1 * (y - p1.y) + p1.x
75.         local xEnd =   slope2 * (y - p2.y) + p2.x
76.         drawFlatTriangle(xStart,xEnd,y,c,pr)
77.     end
78. end
79.
80. local function bindOutputMode(mode) --This switches drawing functions between Advanced peripherals AR goggles and Terminal output.
81.     if mode == "term" then
82.         drawFlatTriangle = drawFlatTriangleCC
83.         pPixel = pPixelTerm
84.     elseif mode == "mon" then
85.         drawFlatTriangle = drawFlatTriangleCC
86.         pPixel = pPixelMon
87.     elseif mode == "AR" then
88.         drawFlatTriangle = drawFlatTriangleAR
89.         pPixel = pPixelAR
90.     else error("invalid mode ("..mode..") for bindTriMode. Please use either 'term', 'mon', or 'AR'.") end
91. end
92.
93. return {
94.     bindOutputMode = bindOutputMode,
95.     drawTri = drawTri,
96. }
RAW Paste Data