This is a computer graphics code for 2D rotation in TURBO C++. It compiles fine but I can't run it. What should I do?

1.8k Views Asked by At

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();
}
1

There are 1 best solutions below

0
CristiFati On

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.