Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Returns interpolated X value
- local function intY(a,b,y)
- return a.x + ((y - a.y) * (b.x - a.x)) / (b.y - a.y)
- end
- --Returns interpolated Y value
- local function intX(a,b,x)
- return a.y + ((b.y - a.y) * (x - a.x)) / (b.x - a.x)
- end
- --get the t value for a point (p) somewhere between the line formed from two give points (a,b)
- local function getT(a,b,p)
- local v1 = vector.new( a.x-b.x, a.y-b.y )
- local v2 = vector.new( a.x-p.x, a.y-p.y )
- return (v1:dot(v2)) / (v1:dot(v1))
- end
- --use the t value of a point to interpolate between two given values (v1,v2)
- --for interpolating between z's, v1 = starting z value, v2 = ending z value
- local function lerp(v1,v2,t)
- return (1 - t) * v1 + t * v2
- end
- local function pPix(x,y,c)
- term.setCursorPos(x,y)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(c)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- local function fillLerp(a,b,c)
- if a.y < b.y then a,b = b,a end
- if a.y < c.y then a,c = c,a end
- if b.x > c.x then b,c = c,b end
- for y = b.y, a.y-1 do
- local xStart = intY(a,b,y)
- local xEnd = intY(a,c,y)
- local p1 = vector.new(xStart,y)
- local p2 = vector.new(xEnd,y)
- local t1 = getT(a,b,p1)
- local t2 = getT(a,c,p2)
- local z1 = lerp(a.z,b.z,t1)
- local z2 = lerp(a.z,c.z,t2)
- for x = xStart, xEnd do
- local t3 = (x - xStart) / (xEnd - xStart)
- local z = math.floor(lerp(z1,z2,t3))
- pPix(x,y,z)
- end
- end
- end
- local function fillInt(a,b,c)
- if a.y < b.y then a,b = b,a end
- if a.y < c.y then a,c = c,a end
- if b.x > c.x then b,c = c,b end
- for y = b.y, a.y-1 do
- local xStart = intY(a,b,y)
- local xEnd = intY(a,c,y)
- local p1 = vector.new(xStart,y)
- local p2 = vector.new(xEnd,y)
- local t1 = getT(a,b,p1)
- local t2 = getT(a,c,p2)
- local z1 = lerp(a.z,b.z,t1)
- local z2 = lerp(a.z,c.z,t2)
- local startScanDepth = { x = xStart, y = z1}
- local endScanDepth = { x = xEnd, y = z2}
- for x = xStart, xEnd do
- local z = math.floor( intX( startScanDepth , endScanDepth, x ) )
- pPix(x,y,z)
- end
- end
- end
- local function fillYInt(a,b,c)
- if a.y < b.y then a,b = b,a end
- if a.y < c.y then a,c = c,a end
- if b.x > c.x then b,c = c,b end
- for y = b.y, a.y-1 do
- local xStart = intY(a,b,y)
- local xEnd = intY(a,c,y)
- local azp = vector.new(a.z,a.y)
- local bzp = vector.new(b.z,b.y)
- local czp = vector.new(c.z,c.y)
- local z1 = intY(azp,bzp,y)
- local z2 = intY(azp,czp,y)
- local startScanDepth = { x = z1, y = xStart}
- local endScanDepth = { x = z2, y = xEnd}
- for x = xStart, xEnd do
- local z = math.floor( intY( startScanDepth , endScanDepth, x ) )
- pPix(x,y,z)
- end
- end
- end
- local fill = fillYInt
- local a = vector.new(20,20,9)
- local b = vector.new(2,2,5)
- local c = vector.new(30,2,8)
- term.clear()
- fill(a,b,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement