Pygame Curve Library for Anti-Aliased Circles and Curved Surface Corners.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
god0fgod god0fgod is offline Offline Sep 24th, 2009, 7:17 pm |
0
I've created a few functions to be used with pygame which are based on anti-aliased curves. At the moment and likely forever, it has two useful functions:

aacircle(s,x,y,r,colour):

Draws an anti-aliased circle on the surface s at the starting point x and y with the radius r. The circle will be coloured with the colour parameter which takes the usual colour tuples eg. (160,200,255).

rounded_corners(s,r)

Takes the surface s and creates rounded corners with the radius r. The corners are anti-aliased. Please not due to a problem with pygame's aaline function, 2 pixels will be cropped from the top and bottom and 1 pixel with be cropped from the sides.

Run the library directly to load a demonstration. You should get http://www.godofgod.co.uk/my_files/curvelib.png

It's been tested on Mac OSX 10.6.1 Snow Leopard
Quick reply to this message  
Python Syntax
  1. #!/usr/bin/env python2.3
  2. #
  3. # curvelib.py
  4. #
  5. #
  6. # Created by Matthew Mitchell on 20/09/2009.
  7. # Copyright (c) 2009 Matthew Mitchell. All rights reserved.
  8. #
  9. import math,numpy,pygame
  10. from pygame.locals import *
  11. def basic_arc_gen(r): #For basic aliased circles
  12. arc = []
  13. radius_sq = float(r) ** float(2)
  14. for y in xrange(r):
  15. x = (radius_sq - float(y) ** float(2)) ** 0.5
  16. arc.append((x,y))
  17. for x in xrange(r):
  18. y = (radius_sq - float(x) ** float(2)) ** 0.5
  19. if not (x,y) in arc:
  20. arc.append((x,y))
  21. return arc
  22. def arc_gen(r):
  23. arc = []
  24. for d in xrange(0,46):
  25. rad = math.radians(d)
  26. arc.append([math.cos(rad) * r,math.sin(rad) * r])
  27. for co in reversed(arc):
  28. arc.append([co[1],co[0]])
  29. return arc
  30. def aacircle(s,x,y,r,colour):
  31. r -= 2
  32. x += 1
  33. y += 1
  34. circle = []
  35. arc = arc_gen(r)
  36. for c in arc:
  37. circle.append([c[0] + r + x,c[1] + r + y])
  38. arc.reverse()
  39. for c in arc:
  40. c[0] = r - c[0]
  41. circle.append([c[0] + x,c[1] + r + y])
  42. arc.reverse()
  43. for c in arc:
  44. c[1] = r - c[1]
  45. circle.append([c[0] +x,c[1] +y])
  46. arc.reverse()
  47. for c in arc:
  48. c[0] = r - c[0]
  49. circle.append([c[0] + r + x,c[1] + y])
  50. pxb = 0
  51. for px in circle:
  52. if pxb != 0:
  53. pygame.draw.aaline(s, colour, px, pxb)
  54. pxb = px
  55. def rounded_corners(s,r):
  56. t = pygame.Surface((r,r))
  57. r -= 2
  58. arc = arc_gen(r)
  59. pxb = 0
  60. for px in arc:
  61. if pxb != 0:
  62. pygame.draw.aaline(t, (255,255,255), px, pxb)
  63. pxb = px
  64. pa = pygame.PixelArray(t)
  65. ab = pygame.surfarray.pixels_alpha(s)
  66. xlen = len(pa[0])
  67. ablen = [len(ab)-1,len(ab[1])-2]
  68. for y in xrange(len(pa)):
  69. a = 0
  70. for x in xrange(xlen):
  71. if pa[y][x] < a:
  72. alpha = 0
  73. ab[(ablen[0] - r) + y][(ablen[1] - r) + x] = alpha
  74. ab[(ablen[0] - r) + y][r - x] = alpha
  75. ab[r - y][(ablen[1] - r) + x] = alpha
  76. ab[r - y][ r - x] = alpha
  77. else:
  78. if pa[y][x] != 0:
  79. alpha = int(float(255) - float(pa[y][x]) ** (float(1)/float(3)))
  80. ab[(ablen[0] - r) + y][(ablen[1] - r) + x] = alpha
  81. ab[(ablen[0] - r) + y][r - x] = alpha
  82. ab[r - y][(ablen[1] - r) + x] = alpha
  83. ab[r - y][r - x] = alpha
  84. a = pa[y][x]
  85. for y in xrange(ablen[0]):
  86. ab[y][0] = 0
  87. ab[y][1] = 0
  88. ab[y][ablen[1]] = 0
  89. ab[y][ablen[1] + 1] = 0
  90. for x in xrange(ablen[1]):
  91. ab[0][x] = 0
  92. ab[ablen[0]][x] = 0
  93. del ab
  94. del pa
  95. if __name__ == '__main__':
  96. import sys
  97. pygame.init()
  98. window = pygame.display.set_mode((500,500))
  99. screen = pygame.Surface((400,400),SRCALPHA)
  100. screen.fill((200,120,250))
  101. rounded_corners(screen,60)
  102. aacircle(screen,100,100,100,(10,60,200))
  103. aacircle(screen,30,20,80,(180,150,60))
  104. window.blit(screen,(50,50))
  105. pygame.display.flip()
  106. while 1:
  107. events = pygame.event.get()
  108. for event in events:
  109. if event.type == QUIT:
  110. sys.exit()


Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC