Guest User

Roblox 2D Triangle Drawer

a guest
Sep 29th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local Quality = 2
  2.  
  3. --Points are based on offset, do not account for scale!
  4. local function DrawTriangle(Parent,Point1,Point2,Point3)
  5.     local X1,Y1 = Point1.X.Offset,Point1.Y.Offset
  6.     local X2,Y2 = Point2.X.Offset,Point2.Y.Offset
  7.     local X3,Y3 = Point3.X.Offset,Point3.Y.Offset
  8.        
  9.     local function Lerp(S,E,A)
  10.         return S + (E-S)*A
  11.     end
  12.    
  13.    
  14.     local function GetLeftAndRightPointsOnY(PosY)
  15.         local ReturnPoint1,ReturnPoint2
  16.         local function SetPoint(UDim1,UDim2)
  17.             if not ReturnPoint1 then
  18.                 ReturnPoint1 = {UDim1,UDim2}
  19.             elseif not ReturnPoint2 then
  20.                 ReturnPoint2 = {UDim1,UDim2}
  21.             end
  22.         end
  23.        
  24.         if (Y1 < PosY and Y2 >= PosY) or (Y2 < PosY and Y1 >= PosY) then
  25.             SetPoint(Point1,Point2)
  26.         end
  27.         if (Y3 < PosY and Y2 >= PosY) or (Y2 < PosY and Y3 >= PosY) then
  28.             SetPoint(Point2,Point3)
  29.         end
  30.         if (Y1 < PosY and Y3 >= PosY) or (Y3 < PosY and Y1 >= PosY) then
  31.             SetPoint(Point1,Point3)
  32.         end
  33.        
  34.         return ReturnPoint1,ReturnPoint2
  35.     end
  36.    
  37.     local function GetLeftAndRightPointsOnX(PosX)
  38.         local ReturnPoint1,ReturnPoint2
  39.         local function SetPoint(UDim1,UDim2)
  40.             if not ReturnPoint1 then
  41.                 ReturnPoint1 = {UDim1,UDim2}
  42.             elseif not ReturnPoint2 then
  43.                 ReturnPoint2 = {UDim1,UDim2}
  44.             end
  45.         end
  46.        
  47.         if (X1 < PosX and X2 >= PosX) or (X2 < PosX and X1 >= PosX) then
  48.             SetPoint(Point1,Point2)
  49.         end
  50.         if (X3 < PosX and X2 >= PosX) or (X2 < PosX and X3 >= PosX) then
  51.             SetPoint(Point2,Point3)
  52.         end
  53.         if (X1 < PosX and X3 >= PosX) or (X3 < PosX and X1 >= PosX) then
  54.             SetPoint(Point1,Point3)
  55.         end
  56.        
  57.         return ReturnPoint1,ReturnPoint2
  58.     end
  59.    
  60.     local function DrawLineOnY(PosY)
  61.         local Pos1T,Pos2T = GetLeftAndRightPointsOnY(PosY)
  62.         if Pos1T and Pos2T then
  63.             local function GetRatio(Point1Y,Point2Y)
  64.                 local Point2Y,PosY = Point2Y - Point1Y,PosY - Point1Y
  65.                 return PosY/Point2Y
  66.             end
  67.            
  68.             local R1,R2 = GetRatio(Pos1T[1].Y.Offset,Pos1T[2].Y.Offset),GetRatio(Pos2T[1].Y.Offset,Pos2T[2].Y.Offset)
  69.             local LerpedPos1,LerpedPos2 = UDim2.new(0,Lerp(Pos1T[1].X.Offset,Pos1T[2].X.Offset,R1),0,PosY),UDim2.new(0,Lerp(Pos2T[1].X.Offset,Pos2T[2].X.Offset,R2),0,PosY)
  70.        
  71.             if LerpedPos1.X.Offset > LerpedPos2.X.Offset then
  72.                 local TempPos = LerpedPos2
  73.                 LerpedPos2,LerpedPos1 = LerpedPos1,TempPos
  74.             end
  75.            
  76.             local Length = LerpedPos2.X.Offset - LerpedPos1.X.Offset
  77.             if Length > 0 then
  78.                 local Frame = Instance.new("Frame")
  79.                 Frame.Position = LerpedPos1
  80.                 Frame.Size = UDim2.new(0,Length,0,Quality)
  81.                 Frame.BorderSizePixel = 0
  82.                 Frame.Parent = Parent
  83.             end
  84.         end
  85.     end
  86.    
  87.     local function DrawLineOnX(PosX)
  88.         local Pos1T,Pos2T = GetLeftAndRightPointsOnX(PosX)
  89.         if Pos1T and Pos2T then
  90.             local function GetRatio(Point1X,Point2X)
  91.                 local Point2X,PosX = Point2X - Point1X,PosX - Point1X
  92.                 return PosX/Point2X
  93.             end
  94.            
  95.             local R1,R2 = GetRatio(Pos1T[1].X.Offset,Pos1T[2].X.Offset),GetRatio(Pos2T[1].X.Offset,Pos2T[2].X.Offset)
  96.             local LerpedPos1,LerpedPos2 = UDim2.new(0,PosX,0,Lerp(Pos1T[1].Y.Offset,Pos1T[2].Y.Offset,R1)),UDim2.new(0,PosX,0,Lerp(Pos2T[1].Y.Offset,Pos2T[2].Y.Offset,R2))
  97.        
  98.             if LerpedPos1.Y.Offset > LerpedPos2.Y.Offset then
  99.                 local TempPos = LerpedPos2
  100.                 LerpedPos2,LerpedPos1 = LerpedPos1,TempPos
  101.             end
  102.            
  103.             local Length = LerpedPos2.Y.Offset - LerpedPos1.Y.Offset
  104.             if Length > 0 then
  105.                 local Frame = Instance.new("Frame")
  106.                 Frame.Position = LerpedPos1
  107.                 Frame.Size = UDim2.new(0,Quality,0,Length)
  108.                 Frame.BorderSizePixel = 0
  109.                 Frame.Parent = Parent
  110.             end
  111.         end
  112.     end
  113.    
  114.     local MinX = math.min(Point1.X.Offset,Point2.X.Offset,Point3.X.Offset)
  115.     local MaxX = math.max(Point1.X.Offset,Point2.X.Offset,Point3.X.Offset)
  116.     local MinY = math.min(Point1.Y.Offset,Point2.Y.Offset,Point3.Y.Offset)
  117.     local MaxY = math.max(Point1.Y.Offset,Point2.Y.Offset,Point3.Y.Offset)
  118.    
  119.     if (MaxY - MinY) < (MaxX - MinX) then
  120.         for Add = 1, (MaxY - MinY)/Quality do
  121.             DrawLineOnY(MinY + (Quality*Add))
  122.         end
  123.     else
  124.         for Add = 1, (MaxX - MinX)/Quality do
  125.             DrawLineOnX(MinX + (Quality*Add))
  126.         end
  127.     end
  128. end
  129.  
  130.  
  131. local Point1 = UDim2.new(0,220,0,20)
  132. local Point2 = UDim2.new(0,25,0,100)
  133. local Point3 = UDim2.new(0,170,0,260)
  134.  
  135. local Point4 = UDim2.new(0,300,0,500)
  136. local Point5 = UDim2.new(0,500,0,500)
  137. local Point6 = UDim2.new(0,500,0,300)
  138.    
  139. game.StarterGui.ScreenGui.Container:ClearAllChildren()
  140. DrawTriangle(game.StarterGui.ScreenGui.Container,Point1,Point2,Point3)
  141. DrawTriangle(game.StarterGui.ScreenGui.Container,Point4,Point5,Point6)
Advertisement
Add Comment
Please, Sign In to add comment