Advertisement
StefanBashkir

dTriangle

Jan 25th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. local p1,p2,p3 = Workspace['Point1']['Position'], Workspace['Point2']['Position'], Workspace['Point3']['Position'];
  2.  
  3. function AngleBetween(v1, v2)
  4.     return math['deg'](math['acos']( (v1:Dot(v2))/(v1['magnitude'] * v2['magnitude']) ))
  5. end
  6.  
  7. function GetWedge()
  8.     local p = Instance['new']([[WedgePart]], Workspace);
  9.     p['TopSurface'], p['BottomSurface'] = "Smooth", "Smooth";
  10.     p['Anchored'] = true;
  11.     p['FormFactor'] = "Custom";
  12.     --p['Size'] = Vector3['new'](.1,5,5);
  13.     return p;
  14. end
  15.  
  16. function GetPoint()
  17.     local p = Instance['new']("Part", Workspace)
  18.     p.FormFactor = "Symmetric"
  19.     p.BrickColor = BrickColor.Blue()
  20.     p.Size = Vector3.new(1,1,1)
  21.     p.Anchored = true
  22.     return p;
  23. end
  24.  
  25. function DrawLine(to, from)
  26.     --[[
  27.         Required an object
  28.     local f = Instance.new("FloorWire", Workspace)
  29.     f.To = to;
  30.     f.From = from
  31.     f.Color = BrickColor.Yellow();]]
  32.     local r = Instance.new("Part")
  33.     r.FormFactor = "Custom"
  34.     r.Size = Vector3.new(0, 0, (from-to)['magnitude'])
  35.     r.Anchored = true
  36.     r.BrickColor = BrickColor.Yellow();
  37.     r.CFrame = (CFrame.new(to, from))
  38.     r.CFrame = r.CFrame + (r.CFrame.lookVector * r.Size.Z/2)
  39.     r.Parent = Workspace
  40. end
  41.  
  42. function dTriangle(v1,v2,v3)
  43.     local A, B, C = AngleBetween((v2-v1)['unit'], (v3-v1)['unit']), AngleBetween((v1-v2)['unit'], (v3-v2)['unit']), AngleBetween((v1-v3)['unit'], (v2-v3)['unit']);
  44.     local a, b, c = (v3-v2)['magnitude'], (v1-v3)['magnitude'], (v1-v2)['magnitude'];
  45.     DrawLine(v1, v2);
  46.     DrawLine(v2, v3);
  47.     DrawLine(v3, v1);
  48.     if A ~= 90 and B ~= 90 and C ~= 90 then -- no right triangle
  49.         local longest;
  50.         if A > B and A > C then
  51.             local adjacent = math['cos'](math.rad(B)) * c
  52.             local bisector = v2 + ((v3-v2)['unit'] * adjacent);
  53.             local bisector_length = math.sqrt(c^2 - adjacent^2)--(bisector-v1)['magnitude']
  54.             local np = GetPoint()
  55.             np.CFrame = CFrame.new(bisector)
  56.             DrawLine(v1, bisector);
  57.             local w1 = GetWedge();
  58.             local tl = (w1.CFrame * CFrame.Angles(math.rad(90),0,0)).lookVector;
  59.             local mid = v1 + ((v2-v1)['unit'] * c/2);
  60.             local nl = CFrame.new(mid, v3).lookVector
  61.             local d = tl:Dot(nl)/(tl.magnitude * nl.magnitude);
  62.             w1.Size = Vector3.new(0,adjacent, bisector_length);
  63.             w1.CFrame = (CFrame.new(mid) * CFrame.Angles(0,0,math.acos(d)))-- * CFrame.Angles(0,0,-math.pi);
  64.         elseif B > A and B > C then
  65.             local adjacent = math['cos'](math.rad(C)) * a
  66.             local bisector = v3 + ((v1-v3)['unit'] * adjacent);
  67.             local np = GetPoint()
  68.             np.CFrame = CFrame.new(bisector)
  69.             DrawLine(v2, bisector);
  70.         else -- C is greatest angle
  71.             local adjacent = math['cos'](math.rad(A)) * b
  72.             local bisector = v1 + ((v2-v1)['unit'] * adjacent);
  73.             local np = GetPoint()
  74.             np.CFrame = CFrame.new(bisector)
  75.             DrawLine(v3, bisector);
  76.         end
  77.     else -- already a right triangle
  78.         local w = GetWedge();
  79.         if A == 90 then -- point1 is the 90
  80.             --w['CFrame'] = CFrame['new'](v1 ) * CFrame['Angles'](0,math['rad'](-90),math['rad'](90));
  81.         elseif B == 90 then
  82.            
  83.         else
  84.            
  85.         end
  86.     end
  87.     print(A,B,C)
  88. end
  89.  
  90. dTriangle(p1,p2,p3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement