Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function drawTri(p1,p2,p3,c,pr) --Called to draw a triangle, given three points, a peripheral (pr), and a color (c).
- if p2.y < p1.y then p1,p2 = p2,p1 end --sort points
- if p3.y < p2.y then p2,p3 = p3,p2 end
- if p2.y < p1.y then p1,p2 = p2,p1 end
- if p1.y == p2.y then --natural flat top
- if p2.x < p1.x then p1,p2 = p2,p1 end
- drawFlatBottom(p1,p2,p3,c,pr)
- elseif p2.y == p3.y then --natural flat bottom
- if p3.x < p2.x then p2,p3 = p3,p2 end
- drawFlatBottom(p1,p2,p3,c,pr)
- else
- --splitting vertex
- local alphaSplit = (p2.y - p1.y) / (p3.y - p1.y)
- local vi ={
- x = p1.x + ((p3.x - p1.x) * alphaSplit),
- y = p1.y + ((p3.y - p1.y) * alphaSplit),
- z = p1.z + ((p3.z - p1.z) * alphaSplit) }
- if pv2.x < vi.x then --Major right
- drawFlatBottom(p1,p2,vi,c,pr)
- drawFlatTop(p2,vi,p3,c,pr)
- else --Major left
- drawFlatBottom(p1,vi,p2,c,pr)
- drawFlatTop(vi,p2,p3,c,pr)
- end
- end
- end
- local function pPixel() end --draw a pixel at coodinates x,y with color c, to a peripheral (pr)
- local function pPixelTerm(x,y,c)
- term.setCursorPos(x,y)
- term.setBackgroundColor(c)
- print(" ")
- end
- local function pPixelMon(x,y,c,pr)
- pr.setCursorPos(x,y)
- pr.setCursorPos(c)
- print(" ")
- end
- local function pPixelAR(x,y,c,pr)
- pr.horizontalLine(x, x, y, c)
- end
- local function drawFlatTriangle() end --switches between terminal output and AR goggle output depending on bind mode.
- local function drawFlatTriangleCC(xStart,xEnd,y,c,pr)
- for x = xStart, xEnd do
- pPixel(x,y,c,pr)
- end
- end
- local function drawFlatTriangleAR(xStart,xEnd,y,c,pr)
- pr.horizontalLine(xStart, xEnd, y, c)
- end
- local function drawFlatBottom(p1,p2,p3,c,pr) --Draw the top portion of a triangle with a flat bottom.
- local slope1 = (p2.x - p1.x) / (p2.y - p1.y)
- local slope2 = (p3.x - p1.x) / (p3.y - p1.y)
- for y = p1.y, p3.y do
- local yP1 = (y+p1.y)
- local xStart = slope1 * yP1 + p1.x
- local xEnd = slope2 * yP1 + p1.x
- drawFlatTriangle(xStart,xEnd,y,c,pr)
- end
- end
- local function drawFlatTop(p1,p2,p3,c,pr) --Draw the bottom portion of a triangle with a flat top.
- local slope1 = (p3.x - p1.x) / (p3.y - p1.y)
- local slope2 = (p3.x - p2.x) / (p3.y - p2.y)
- for y = p1.y, p3.y do
- local xStart = slope1 * (y - p1.y) + p1.x
- local xEnd = slope2 * (y - p2.y) + p2.x
- drawFlatTriangle(xStart,xEnd,y,c,pr)
- end
- end
- local function bindOutputMode(mode) --This switches drawing functions between Advanced peripherals AR goggles and Terminal output.
- if mode == "term" then
- drawFlatTriangle = drawFlatTriangleCC
- pPixel = pPixelTerm
- elseif mode == "mon" then
- drawFlatTriangle = drawFlatTriangleCC
- pPixel = pPixelMon
- elseif mode == "AR" then
- drawFlatTriangle = drawFlatTriangleAR
- pPixel = pPixelAR
- else error("invalid mode ("..mode..") for bindTriMode. Please use either 'term', 'mon', or 'AR'.") end
- end
- return {
- bindOutputMode = bindOutputMode,
- drawTri = drawTri,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement