Advertisement
programcreator

Segment

Jan 11th, 2016
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. --[[
  2.     Class: Segment
  3.     Author: Me a.k.a. Creator
  4. ]]--
  5.  
  6. local Internal, Nano, NanoPath, ApplicationPath, ClassPath = ...
  7.  
  8. local function Segment(x1,y1,x2,y2)
  9.     --Private
  10.     local x1 = x1
  11.     local x2 = x2
  12.     local y1 = y1
  13.     local y2 = y2
  14.  
  15.     local maxX = x1 > x2 and x1 or x2
  16.     local minX = x1 < x2 and x1 or x2
  17.  
  18.     local maxY = y1 > y2 and y1 or y2
  19.     local minY = y1 < y2 and y1 or y2
  20.  
  21.     --Public
  22.     local self = {}
  23.     function self.render(resolution)
  24.         local path = {}
  25.         for i=0,1,1/resolution do
  26.             path[#path + 1] = {x1*(1-i)+x2*i, y1*(1-i)+y2*i}
  27.         end
  28.         return path
  29.     end
  30.     --get x
  31.     function self.getX(y)
  32.         index = (y - y1)/(y2 - y1)
  33.         print("Index:",index)
  34.         if index <= 1 and index >= 0 then
  35.             local toret = x1 + index*(x2 - x1)
  36.             return toret
  37.         end
  38.     end
  39.     --get y
  40.     function self.getY(x)
  41.         index = (x - x1)/(x2 - x1)
  42.         print("Index:",index)
  43.         if index <= 1 and index >= 0 then
  44.             return y1 + index*(y2 - y1)
  45.         end
  46.     end
  47.     --scanLine
  48.     function self.scanRender()
  49.         local path = {}
  50.         local counter = 1
  51.         for i=minY, maxY do
  52.             path[counter] = self.getX(i)
  53.             path[counter + 1] = i
  54.             counter = counter + 2
  55.         end
  56.         return path
  57.     end
  58.     --Constructor
  59.     return self
  60. end
  61.  
  62. Internal.Graphics.Segment = Segment
  63.  
  64. Nano.Segment = Segment
  65.  
  66. --[[
  67.     bluey = blacky1 + f*(blacky2 - blacky1)
  68.     bluey - blacky1 = f*(blacky2 - blacky1)
  69.     (bluey - blacky1)/(blacky2 - blacky1) = f
  70. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement