How to start CImg drawing from bottom-left corner rather than top-left corner?

StackOverflow https://stackoverflow.com/questions/19217883

  •  30-06-2022
  •  | 
  •  

Pregunta

In CImg the co-ordinate system is considered to be started at top,left corner . But , I am normally used to see the co-ordinate system started from bottom,left corner . So whenever i am trying to draw a triangle with co-ordinates (10,100),(50,50),(100,100) I got the drawing as upside down . Is there any to flip this ?Expected is I want this

Found is But i found this

¿Fue útil?

Solución

To put it as simply as possible, every time you specify a y value to CImg, you need to first flip the coordinate system from what you want to what it offers. Perhaps the easiest way to do this would be to write your own function that converts the y value for you:

unsigned int flip_y( unsigned int y, unsigned int height ) {
    return height - y;
}

This would mean that every place you would have to specify y, you would have to remember to use the function:

img.draw_text( x, y, "hello world!", ...);
// becomes
img.draw_text( x, flip_y( y, img.height() ), "hello world!", ...);

Understandably, this would get very tedious very fast, and forgetting to use flip_y in a single situation could be quite time consuming to track down and fix.

As an alternative, you could make your own subclass of CImg and wrapping all the functions that take y values and inverts y before passing the values to the original CImg method. This would have the advantage of being able to work when passing references to your subclass to library functions expecting a CImg parameter.

Keep in mind that many graphical libraries use this style of coordinates, typically because it is much easier to do buffer addressing with that kind of coordinate system. If you plan on doing a lot more work with computer graphics in the future, I would recommend learning to use this coordinate system directly instead of trying to force it to a more mathematical one.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top