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();
}
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.
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:
Output (in a DOSBox emulator):
Build:
Run:
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.