This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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
define the centre of the circle and use a distance function to control the colour? (assuming radial gradient)

##### Share on other sites
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
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
[color=#1C2837][size=2]Wouldn't Pythagoras imply the use of square root and power functions? Isn't there a less cpu consuming method?

##### Share on other sites

[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.

• 16
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×