Advertisement
Guest User

Circle.lua

a guest
Mar 18th, 2015
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. --[[Example percentages array
  2.  
  3. local percentages={
  4.   {
  5.   Percentage=12,
  6.   Colour=colours.red
  7.   },
  8.   {
  9.   Percentage=30,
  10.   Colour=colours.blue
  11.   },
  12.   {
  13.   Percentage=25,
  14.   Colour=colours.green
  15.   }
  16. }]]
  17.  
  18. BackgroundColour = colours.lightGrey
  19. ActiveColour = colours.blue
  20. Percentages = {
  21.         {
  22.         Percentage=10,
  23.         Colour=ActiveColour
  24.         }
  25. }
  26. Filled = false
  27. TextColour = colors.black
  28. Radius = 1
  29. SubType = "Circle"
  30. ShowText = false
  31.  
  32. OnDraw = function(self,x,y)
  33. self.Height = self.Radius * 2+10
  34. self.Width = self.Radius * 2+10
  35.     if self.SubType == "Circle" then
  36.      drawCircle(x,y,self.Radius,self.ActiveColour,self.Filled)
  37.     elseif self.SubType == "Pie" then
  38.      drawPie(x,y,self.Radius,self.Percentages,self.Filled,self.BackgroundColour)
  39.     end
  40. end
  41.  
  42. function drawCircle(StartX, StartY, radius, colour, filled)
  43. local Filled = filled or false
  44. local percentages={
  45.   {
  46.   Percentage=100,
  47.   Colour=colour
  48.   }
  49. }
  50.  
  51. ShowText = false
  52. local BGColour = colour
  53. drawPie(StartX,StartY,radius,percentages,Filled,BGColour)
  54. end
  55.  
  56. function drawPie(StartX,StartY,radius, percentages, filled,BGColour)
  57. local Filled = filled or false
  58. local Radius = radius
  59. local CenterX = math.floor(radius + StartX)
  60. local CenterY = math.floor(radius  + StartY)
  61. local first = true
  62. local lastPercentage = nil
  63.     --calculate arc
  64.     for key, value in next, percentages do
  65.       theta = nil
  66.       if first then
  67.         theta = 0
  68.         first = false
  69.       else
  70.           theta = lastPercentage
  71.       end
  72.  
  73.       lastPercentage = theta + math.rad((value.Percentage/100)*360)
  74.         --[[check if somebody added an element pushing us past 100%]]
  75.         if lastPercentage > math.rad(360) then
  76.           --[[undo that and break this loop let us finish up.]]
  77.           lastPercentage = lastPercentage - math.rad((value.Percentage/100)*360)
  78.           break
  79.         end
  80.  
  81.         if ShowText and Filled then
  82.           --[[use start and end radian values to get starting/ending x-y
  83.           cumulative based on how many slices]]
  84.           segX2 = CenterX + Radius * math.sin(lastPercentage)
  85.           segY2 = CenterY + Radius * math.cos(lastPercentage)
  86.           segX1 = CenterX + Radius * math.sin(theta)
  87.           segY1 = CenterY + Radius * math.cos(theta)
  88.           --[[get centerpoint of arc called c]]
  89.           c = math.atan2(segY1-segY2,segX1-segX2)
  90.           --[[get proper quadrant based on ending angle
  91.           atan2 should get this automagically apparently not however]]
  92.           if(c - lastPercentage)<-math.pi then
  93.           --[[notice their not really midpoint of the radius
  94.           adjusted slightly to make it look prettier.]]
  95.           --[[compensate for negative x values in relation to centerpoint
  96.           if it was 0,0]]
  97.           textX = CenterX + (Radius/1.3) * math.sin(c-2*math.pi)
  98.           --[[flip y value atan2 seems to be interpreting a wrong 0 point]]
  99.           textY = CenterY + (Radius/1.5) * math.cos(c-math.pi)
  100.           else
  101.           textX = CenterX + (Radius/1.5) * math.sin(c)
  102.           --[[flip y value atan2 seems to be interpreting a wrong 0 point]]
  103.           textY = CenterY + (Radius/1.5) * math.cos(c-math.pi)
  104.           end
  105.         end
  106.  
  107.       --draw arc
  108.       while theta <= lastPercentage do
  109.         x = CenterX + Radius * math.sin(theta)
  110.         y = CenterY + Radius * math.cos(theta)
  111.           if Filled then
  112.             drawLine(CenterX,CenterY,x,y,value.Colour)
  113.           else
  114.             Drawing.WriteToBuffer(x, y," ",colours.white,value.Colour)
  115.           end
  116.         theta = theta + 0.01
  117.       end--end drawing arc
  118.       --[[Draw based on calculations for midpoint we got before drawing the
  119.       slice that way we don't end up drawing underneath]]
  120.       if ShowText and Filled then
  121.         Drawing.DrawCharacters(textX, textY, value.Percentage.."%", TextColour, value.Colour)
  122.       end
  123.     end--end calculating arc
  124.     --finish drawing circle
  125.     if lastPercentage < math.rad(360) then
  126.       theta = lastPercentage
  127.         while theta < math.rad(360) do
  128.           x = CenterX + Radius * math.sin(theta)
  129.           y = CenterY + Radius * math.cos(theta)
  130.             if Filled then
  131.               drawLine(CenterX,CenterY,x,y,BGColour)
  132.             else
  133.               Drawing.WriteToBuffer(x, y," ",colours.white,BGColour)
  134.             end
  135.           theta = theta + 0.01
  136.       end--end drawing arc
  137.     end--end finish drawing circle
  138. end
  139.  
  140. function drawLine(x1,y1,x2,y2,colour)
  141.   for i = 0,1,0.03 do
  142.     x = (x2-x1)*i + x1
  143.     y = (y2-y1)*i + y1
  144.     Drawing.WriteToBuffer(x,y," ",colours.white,colour)
  145.   end
  146. end
  147.  
  148. local function ParseColour(value)
  149.   if type(value) == 'string' then
  150.     if colours[value] and type(colours[value]) == 'number' then
  151.       return colours[value]
  152.     elseif colors[value] and type(colors[value]) == 'number' then
  153.       return colors[value]
  154.     end
  155.   elseif type(value) == 'number' and (value == colours.transparent or (value >= colours.white and value <= colours.black)) then
  156.     return value
  157.   end
  158.   error('Invalid colour: "'..tostring(value)..'"')
  159. end
  160.  
  161. OnUpdate = function(self,value)
  162.   if value == 'Percentages' then
  163.     for key, subvalue in pairs(self.Percentages) do
  164.     subvalue.Colour = ParseColour(subvalue.Colour)
  165.     end
  166.   end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement