-2

I tried to write a code to draw a triangle with its base facing downwards, using the following program:

#include<graphics.h>
#include<conio.h>
using namespace std;
int main()
{
    initwindow(400,400,"Triangle");
    line(100,100,200,300);
    line(200,300,300,100);
    line(100,100,300,100);

    getch();
    return 0;
}

enter image description here

But this gave the wrong output ^^

Whereas when I inverted the coordinates and created a mirror of them, then the triangle was drawn correctly

#include<graphics.h>
#include<conio.h>
using namespace std;
int main()
{
    initwindow(400,400,"Triangle");
    line(100,300,200,100);
    line(200,100,300,300);
    line(300,300,100,300);
    getch();
    return 0;
}

enter image description here

How do I fix this bug?

Botje
  • 26,269
  • 3
  • 31
  • 41
Ayush
  • 23
  • 7
  • 2
    I am not familiar with graphics.h, but in a graphics programming context, upside-down Cartesian coordinate system (with 0, 0 at top left, rather than bottom left) is fairly common. Unless that indeed isn't supposed to be the case for this library and is only caused by some freak bug, perhaps just wrap the library functions, making them mirror coordinates back to Your preferred system? – S. Exchange Considered Harmful Aug 05 '20 at 11:58
  • If a function does not do what you expect it's usually not a bug, but your expectations are wrong. I'd like to link to the original documentation, but I can't find any (if it ever existed). That said, graphics.h is _ancient_ and you might want to look to other frameworks if you wish to proceed in graphics programming. See also https://stackoverflow.com/questions/7860569/how-i-can-get-and-use-the-header-file-graphics-h-in-my-c-program – Lukas-T Aug 05 '20 at 13:02

1 Answers1

1

It seems that you are using a library which supports the Borland Graphics Interface (BGI). In BGI the origin is the top left of the screen, as S.E.C.H surmised. Here some documentation:

Each pixel is specified by its row and column position where coordinates (0, 0) corresponds to the pixel in the upper-left corner of the window and (width-1, height-1) corresponds to the pixel in the lower-right corner of the window.

http://dsearls.org/courses/C122CompSci/Graphics/GraphicsIntro.htm

AlDante
  • 336
  • 3
  • 8
  • @AllDante, so there must be a way to fix this, right? – Ayush Aug 06 '20 at 02:16
  • As S.E.C.H said, wrap the library functions. It's not a fix, as such, as it's not a bug, just a different coordinate system. The x coordinates stay the same, but you need to subtract your y coordinates from your screen height, e.g. `void myline(int screenheight, int x1, int y1, int x2, int y2)` `{` `line(x1, screenheight - y1, x2, screenheight - y2);` `}` In your case it looks as if screenheight is 400 – AlDante Aug 06 '20 at 14:12