## Recommended Posts

tallano    100
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! [img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img]

##### Share on other sites
Ohforf sake    2052
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?

##### Share on other sites
bwhiting    1562
define the centre of the circle and use a distance function to control the colour? (assuming radial gradient)

##### Share on other sites
tallano    100
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.

##### Share on other sites
bwhiting    1562
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

##### Share on other sites
tallano    100
[color=#1C2837][size=2]Wouldn't Pythagoras imply the use of square root and power functions? Isn't there a less cpu consuming method?[/size][/color]

##### Share on other sites
alvaro    21266
[quote name='tallano' timestamp='1307128099' post='4819184']
[color="#1C2837"][size="2"]Wouldn't Pythagoras imply the use of square root and power functions? Isn't there a less cpu consuming method?[/size][/color]
[/quote]

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 [url="http://en.wikipedia.org/wiki/Midpoint_circle_algorithm"]midpoint circle algorithm[/url].