filled circle with gradient

Started by
5 comments, last by alvaro 12 years, 10 months ago
Hi everyone,
I couldn't find anything so sorry for this probably classic question : I am looking for an algorithm for drawing a 2D circle filled with a gradient.
Thanks guys! wink.gif
Advertisement
What kind of gradient? Left to right, bottom to top, from the center outwards, linear, quadratic, ...

What kind of API do you have? Can you draw triangles (like in DirectX, OpenGL) or do you only have a framebuffer and thus must do the rasterisation yourself?
define the centre of the circle and use a distance function to control the colour? (assuming radial gradient)
Oops, sorry for the lack of details.
The gradient has to be radial, and i have to do it from scratch, e.g. i can only use a "putPixel" function.
use Pythagoras theorem to get the distance of the pixel from the centre, then use that to get a value from 0-1 by dividing it by the radius of your gradient then use the result to choose a colour
[color=#1C2837][size=2]Wouldn't Pythagoras imply the use of square root and power functions? Isn't there a less cpu consuming method?

[color="#1C2837"]Wouldn't Pythagoras imply the use of square root and power functions? Isn't there a less cpu consuming method?


Just a square root. The "power functions" in these case are only a couple of multiplications, which are no big deal. You should probably start doing it that way, and then try to optimize it if you have to. For instance, as a preprocessing step, you can create a 1D texture that is indexed by the distance squared. That way you don't need to take a square root per pixel.

To find the pixels that belong in the circle, you can try to use the midpoint circle algorithm.

This topic is closed to new replies.

Advertisement