Advertisement
programcreator

Lines

Jan 12th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.00 KB | None | 0 0
  1. local function Segment(x1,y1,x2,y2)
  2.     --Private
  3.     local x1 = x1
  4.     local x2 = x2
  5.     local y1 = y1
  6.     local y2 = y2
  7.  
  8.     local maxX = x1 > x2 and x1 or x2
  9.     local minX = x1 < x2 and x1 or x2
  10.  
  11.     local maxY = y1 > y2 and y1 or y2
  12.     local minY = y1 < y2 and y1 or y2
  13.  
  14.     --Public
  15.     local self = {}
  16.     function self.render(resolution)
  17.         local path = {}
  18.         for i=0,1,1/resolution do
  19.             path[#path + 1] = {x1*(1-i)+x2*i, y1*(1-i)+y2*i}
  20.         end
  21.         return path
  22.     end
  23.     --get x
  24.     function self.getX(y)
  25.         index = (y - y1)/(y2 - y1)
  26.         if index <= 1 and index >= 0 then
  27.             local toret = x1 + index*(x2 - x1)
  28.             return toret
  29.         end
  30.     end
  31.     --get y
  32.     function self.getY(x)
  33.         index = (x - x1)/(x2 - x1)
  34.         if index <= 1 and index >= 0 then
  35.             return y1 + index*(y2 - y1)
  36.         end
  37.     end
  38.     --scanLine
  39.     function self.scanRender()
  40.         local path = {}
  41.         local counter = 1
  42.         for i=minY, maxY do
  43.             path[counter] = self.getX(i)
  44.             path[counter + 1] = i
  45.             counter = counter + 2
  46.         end
  47.         return path
  48.     end
  49.     --Constructor
  50.     return self
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement