Drawing tons of circles in Python using cairo
Asked Answered
S

1

6

I am currently working on an application, which uses a video projector to create an effect similar to a real laser. A really nice example of what I'm trying to archive can be seen on Youtube here.

Basically that application needs to draw simple moving shapes in various colors. I have a pretty complicated setup using pycairo allowing the primitives to pass through a set of modifiers to change position, scale and rotation. This allows for a great deal of flexibility.

Unfortunately pycairo seems to be pretty slow at drawing dashed circles. I tried drawing 30 circles like this:

# setup, transforms...
# Example color-scheme:
self._colors = [(0.0, 1.0, 0.0)]
# drawing dashes one after another
for count, color in enumerate(self._colors):
    cr.set_dash(dash_len, self._dash_len * count)
    cr.set_source_rgb(color[0], color[1], color[2])

    cr.arc(0, 0, self.radius(), 0, 2 * math.pi)
    cr.stroke()

The whole thing looks like this. This is not able to sustain 25fps with on 800x600 using a Core2Duo.

Is there a faster way to draw circles? Quality is not really an issue.

Thanks for your help!

Subterrane answered 4/10, 2012 at 15:12 Comment(4)
Have you considered drawing n-gons instead of circles? Should be faster, and with a sufficiently high N you won't see too much of a difference.Aleuromancy
you could also try turning off the antialiasing and suchEnright
@Aleuromancy I tried 20-gons, they were roughly the same speed. Your idea looked promising though @Enright I already have a cr.set_tolerance(0.5) in my setup code, is there more?Subterrane
You could draw the circles once on a temporary surface (with a transparent background!) and then draw that temporary surface to the "screen surface". That should avoid the cost of rasterizing the circles all the time.Rafaellle
P
5

Cairo aims at high-quality rendering - and it is used a lot in static, or quasi-static rendering of 2d things.

It is no wonder it can be slow -- I think the first try I'd make in your place would be to use pygame + pyopenGL -- I am sorry I am not comming with a full example, but this project looks like a good start: http://www.willmcgugan.com/blog/tech/2007/6/4/opengl-sample-code-for-pygame/

Pix answered 4/10, 2012 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.