roundrect.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- Filename: roundrect.lua
  2. -- Author: Luke Perkin
  3. -- Date: 2010-02-25
  4. -- Desc: Draws a round rectangle
  5. -- Thanks to Robin for the draw code.
  6. local function roundrect(mode, x, y, width, height, xround, yround)
  7. local points = {}
  8. local precision = (xround + yround) * .1
  9. local tI, hP = table.insert, .5*math.pi
  10. if xround > width*.5 then xround = width*.5 end
  11. if yround > height*.5 then yround = height*.5 end
  12. local X1, Y1, X2, Y2 = x + xround, y + yround, x + width - xround, y + height - yround
  13. local sin, cos = math.sin, math.cos
  14. for i = 0, precision do
  15. local a = (i/precision-1)*hP
  16. tI(points, X2 + xround*cos(a))
  17. tI(points, Y1 + yround*sin(a))
  18. end
  19. for i = 0, precision do
  20. local a = (i/precision)*hP
  21. tI(points, X2 + xround*cos(a))
  22. tI(points, Y2 + yround*sin(a))
  23. end
  24. for i = 0, precision do
  25. local a = (i/precision+1)*hP
  26. tI(points, X1 + xround*cos(a))
  27. tI(points, Y2 + yround*sin(a))
  28. end
  29. for i = 0, precision do
  30. local a = (i/precision+2)*hP
  31. tI(points, X1 + xround*cos(a))
  32. tI(points, Y1 + yround*sin(a))
  33. end
  34. love.graphics.polygon(mode, unpack(points))
  35. end
  36. return roundrect