Rotate triangle with Sdl_gfx

How can i rotate a triangle drawn in c with sdl_gfx with :

        filledTrigonRGBA (...)

(i want to do an animation where the triangle rotates itself) The only function i’ve found to rotate stuff is in SDL::GFX::Rotozoom

SDL::GFX::Rotozoom::surface( $surface, $angle, $zoom, $smooth );

But this function takes a surface… I’m new with this library and I’m not sure how to solve something this simple. Is this possible or I need to draw the triangle each time with different coordinates?if this is the case, it’s insanely long and complicated.

I do not understand why you think it is “insanely long and complicated”. Drawing the triangle each time, with different coordinates, is straightforward in terms of code and almost certainly computationally less effort than rotating a surface!

If for some reason you don’t want to do it this way, you could render the triangle once onto a target texture, and then rotate it using SDL_RenderCopyEx().

1 Like

Because to make the transition looks smooth, i need to figure out how to draw the triangle in a way to make it rotate a little each frame, i didn’t try it yet but it seems to me to be complicated :slight_smile:

If you want to achieve smooth, slow, rotation then the fact that filledTrigonRGBA() takes integer (rather than float) vertex coordinates is unfortunate and may result in aliasing artifacts. You might want to consider using the anti-aliased routine aaFilledPolygonRGBA() which is one of my extensions to SDL2_gfx that I described here recently.

1 Like

If i choose

        filledTrigonRGBA (...)

Is there a formula available to do a full rotation having the coordinates of the triangle??

An easy way is to specify the vertices of your triangle using polar coordinates rather than Cartesian coordinates. Rotation can then be achieved simply by adding the same offset to all three angles, and you need only convert the polar coordinates to Cartesian (which is the easier direction) for plotting.

I’d rather use filledPolygonRGBA() or aaFilledPolygonRGBA() because they receive the vertex coordinates in an array and it’s neater to do the polar-to-Cartesian conversion in a loop.

1 Like