1

The code below compiles fine but I cant run it on TURBO C++. The runtime screen just flashes. But i have also used getch(). I dont know where I am going wrong. What should I do?

#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
    int gm;
    int gd = DETECT; //graphic driver
    int x1, x2, x3, y1, y2, y3, x1n, x2n, x3n, y1n, y2n, y3n, c; //vertices of triangle
    int r; //rotation angle  
    float t;
    initgraph(&gd, &gm, "C:\TURBOC3:\BGI:");
    setcolor(RED);

    printf("\t Enter vertices of triangle: ");
    scanf("%d%d%d%d%d%d", &x1,&y1,&x2,&y2,&x3,&y3);
    line(x1,y1,x2,y2);
    line(x2,y2,x3,y3);
    line(x3,y3,x1,y1);

    printf("\nEnter angle of rotation: ");
    scanf("%d",&r);
    t = 3.14*r/180; //converting degree into radian
    
    //applying 2D rotation equations
    x1n = abs(x1*cos(t)-y1*sin(t));
    y1n = abs(x1*sin(t)+y1*cos(t));
    x2n = abs(x2*cos(t)-y2*sin(t));
    y2n = abs(x2*sin(t)+y2*cos(t));
    x3n = abs(x3*cos(t)-y3*sin(t));
    y3n = abs(x3*sin(t)+y3*cos(t));

    //Drawing the rotated triangle
    line(x1n,y1n,x2n,y2n);
    line(x2n,y2n,x3n,y3n);
    line(x3n,y3n,x1n,y1n);
    getch();
}
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Not sure what the arguments to `initgraph` represent but, if the last is a directory+filename, then you'll need to escape the backslash (i.e. double it up). – Adrian Mole Mar 07 '21 at 16:28
  • 1
    haven't use BGI for decades so I might be wrong but are you sure you can use `printf` after initgraph? You know `printf` uses text mode (like cout) and BGI gfx mode maybe your print just resets video mode back to mode 3 (80x25 text). The same goes for `scanf` ... Also you are runnung the Turbo C++under MS-DOS? DOSbox? differnet emulator? or you got entirely different compiler and use BGI port like winBGI under Win or Linux ? under DOS box you need more getch in series (at least 3) also try to hit IIRC [F5] to see what was outputed by your program. – Spektre Mar 07 '21 at 19:24

1 Answers1

0

Many useful pieces of info in comments.

The problem (or at least the main one) is clear: path to .bgi files ("C:\TURBOC3:\BGI:") is wrong, actually it's not even a valid Win (DOS) path.

  • It contains a bunch of colons (:), when only the drive letter (if present) should contain one
  • It's always good to escape (double) bkslashes (\) in paths. This doesn't affect you in this case, but it's a general guideline

As a consequence, initgraph fails.

Another golden rule when programming, is: always check a function outcome (return code, error flags, ...), don't assume everything just worked fine!
In this case, graphresult should be used. I don't know where the official documentation is (or if it exists), but here's a pretty good substitute: [Colorado.CS]: Borland Graphics Interface (BGI) for Windows.

There are also some minor problems, like printf not functioning in graphic mode (scanf does, but it lets the user input to be displayed (in text mode), so it messes up (part of) the graphic screen).

Here's a modified version of the code (I added the test variable to avoid entering the 7 values every time the program is run).

main00.c:

#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>


int main() {
    int err, gm, gd = DETECT;  // Graphic driver
    int x1, x2, x3, y1, y2, y3, x1n, x2n, x3n, y1n, y2n, y3n, c;  // Vertices of triangle
    int r;  // Rotation angle
    float t;
    int test = 1;  // Set to: 0 to read from keyboard, or anything else to use predefined values

    if (test) {
        x1 = 220;
        y1 = 200;
        x2 = 420;
        y2 = 200;
        x3 = 320;
        y3 = 280;
        r = 45;
    } else {
        printf("\nEnter vertices (x, y) of triangle: ");
        scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3);
        printf("\nEnter angle of rotation (degrees): ");
        scanf("%d", &r);
    }

    initgraph(&gd, &gm, "Y:\\BC\\BGI");  // You should use "C:\\TURBOC3\\BGI"
    err = graphresult();
    if (err != grOk) {
        printf("Error initializing graphics: %d\n", err);
        getch();
        return -1;
    }

    setcolor(WHITE);
    outtextxy(10, 10, "Triangle rotation demo");

    setcolor(LIGHTRED);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);

    t = M_PI * r / 180;  // Converting degrees into radians
    // Applying 2D rotation equations
    x1n = abs(x1 * cos(t) - y1 * sin(t));
    y1n = abs(x1 * sin(t) + y1 * cos(t));
    x2n = abs(x2 * cos(t) - y2 * sin(t));
    y2n = abs(x2 * sin(t) + y2 * cos(t));
    x3n = abs(x3 * cos(t) - y3 * sin(t));
    y3n = abs(x3 * sin(t) + y3 * cos(t));

    // Drawing the rotated triangle
    setcolor(YELLOW);
    line(x1n, y1n, x2n, y2n);
    line(x2n, y2n, x3n, y3n);
    line(x3n, y3n, x1n, y1n);

    getch();
    return 0;
}

Output (in a DOSBox emulator):

  • Build:

    Img00

  • Run:

    Img01

Note: The rotated triangle (yellow) might seem positioned a bit unexpectedly (translated), but that is because no rotation center is explicitly provided, so O(0, 0) (origin - upper left corner) is used, and the 3 points are rotated around it.
If choosing one of the triangle vertices (or better: one of its centers) as rotation center, the 2 triangles will overlap, making the rotation more obvious. But that's just (plane) geometry, and it's beyond this question's scope.

CristiFati
  • 38,250
  • 9
  • 50
  • 87