-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram12.c
More file actions
70 lines (57 loc) · 1.73 KB
/
Copy pathprogram12.c
File metadata and controls
70 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// write a c program for Drawing three dimensional objects and Scenes
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
void rotateY(float *x, float *z, float angle) {
float tempX = *x;
*x = *x * cos(angle) - *z * sin(angle);
*z = tempX * sin(angle) + *z * cos(angle);
}
void project(float x, float y, float z, int *x2D, int *y2D, int viewDist) {
*x2D = (int)(x * viewDist / (z + viewDist));
*y2D = (int)(y * viewDist / (z + viewDist));
}
int main() {
int gd = DETECT, gm;
int cx, cy, i;
float angle = 0.05;
int x2D[8], y2D[8];
float cube[8][3] = {
{-50, -50, -50},
{ 50, -50, -50},
{ 50, 50, -50},
{-50, 50, -50},
{-50, -50, 50},
{ 50, -50, 50},
{ 50, 50, 50},
{-50, 50, 50}
};
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
cx = getmaxx() / 2;
cy = getmaxy() / 2;
while (!kbhit()) {
cleardevice();
for (i = 0; i < 8; i++) {
rotateY(&cube[i][0], &cube[i][2], angle);
}
for (i = 0; i < 8; i++) {
project(cube[i][0], cube[i][1], cube[i][2], &x2D[i], &y2D[i], 200);
x2D[i] += cx;
y2D[i] += cy;
}
for (i = 0; i < 4; i++) {
line(x2D[i], y2D[i], x2D[(i + 1) % 4], y2D[(i + 1) % 4]);
line(x2D[i + 4], y2D[i + 4], x2D[((i + 1) % 4) + 4], y2D[((i + 1) % 4) + 4]);
line(x2D[i], y2D[i], x2D[i + 4], y2D[i + 4]);
}
line(x2D[0], y2D[0], x2D[4], y2D[4]);
line(x2D[1], y2D[1], x2D[5], y2D[5]);
line(x2D[2], y2D[2], x2D[6], y2D[6]);
line(x2D[3], y2D[3], x2D[7], y2D[7]);
delay(10);
}
closegraph();
return 0;
}