模拟3d

  1 #include <graphics.h> 
  2 #include <conio.h>
  3 #include <time.h>
  4 #include <math.h>
  5 
  6 #define maxp 10000
  7 #define end 1000.0
  8 #define T 0
  9 #define space 3.0
 10 #define change 2.0
 11 #define beginning 450.0
 12 
 13 struct ddd{
 14     double x;
 15     double y;
 16     double z;
 17     COLORREF color;
 18 };
 19 
 20 ddd a[maxp],q;
 21 double f;
 22 int cnt = 0,get;
 23 
 24 void print(ddd p){
 25     f = (end - p.z) / (end - q.z);
 26     putpixel((p.x - q.x) * f,(p.y - q.y) * f,p.color);
 27 }
 28 
 29 double len(ddd aa,ddd bb){
 30     return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y)+(aa.z-bb.z)*(aa.z-bb.z)) - 150.0;
 31 }
 32 
 33 void lll(int xx,int yy,int zz,int xxx,int yyy,int zzz){
 34     int pp;
 35     if(xx != xxx){
 36         for(pp = xx;pp <= xxx;pp += space){
 37             cnt++;
 38             a[cnt].x = pp * 1.0;
 39             a[cnt].y = yy * 1.0;
 40             a[cnt].z = zz * 1.0;
 41         }
 42         return;
 43     }
 44     if(yy != yyy){
 45         for(pp = yy;pp <= yyy;pp += space){
 46             cnt++;
 47             a[cnt].x = xx * 1.0;
 48             a[cnt].y = pp * 1.0;
 49             a[cnt].z = zz * 1.0;
 50         }
 51         return;
 52     }
 53     if(zz != zzz){
 54         for(pp = zz;pp <= zzz;pp += space){
 55             cnt++;
 56             a[cnt].x = xx * 1.0;
 57             a[cnt].y = yy * 1.0;
 58             a[cnt].z = pp * 1.0;
 59         }
 60         return;
 61     }
 62 }
 63 
 64 void main()
 65 {
 66     
 67     initgraph(1000, 1000, NOMINIMIZE);   // ÕâÀïºÍ TC ÂÔÓÐÇø±ð
 68 
 69     setorigin(500,500);
 70 
 71     setbkcolor(WHITE);
 72     
 73     srand(time(NULL));
 74     
 75     q.x = 100.0;
 76     q.y = 70.0;
 77     q.z = 10.0;
 78     
 79     /*
 80     for(int f = 0;f < maxp;f++){
 81         a[f].x = rand() % 100 + 50;
 82         a[f].y = rand() % 100 + 20;
 83         a[f].z = rand() % 100 + 200;
 84     }
 85     */
 86     lll(50,20,200,50,120,200);    //
 87     lll(50,20,200,150,20,200);    //
 88     lll(50,20,200,50,120,300);    //
 89     lll(50,20,300,50,120,300);    //
 90     lll(50,120,200,50,120,300);    //
 91     lll(50,120,200,150,120,200);    //
 92     lll(150,20,200,150,120,200);    //    
 93     lll(50,120,300,150,120,300);    //
 94     lll(150,120,200,150,120,300);    //
 95     lll(50,20,300,150,20,300);        
 96     lll(150,20,200,150,20,300);  
 97     lll(150,20,300,150,120,300);  
 98     
 99     lll(0,0,0,0,0,100);
100     lll(0,0,0,0,200,0);
101     lll(0,0,0,300,0,0);
102     /*
103     for(int ii = 0;ii <= maxp;ii++){
104         if(a[ii].x >= 145){
105             a[ii].color = LIGHTRED;
106         }
107         if(a[ii].y >= 115){
108             a[ii].color = LIGHTRED;
109         }
110         if(a[ii].z >= 295){
111             a[ii].color = LIGHTRED;
112         }
113         
114         if(a[ii].x <= 55){
115             a[ii].color = LIGHTRED;
116         }
117         if(a[ii].y <= 25){
118             a[ii].color = LIGHTRED;
119         }
120         if(a[ii].z <= 205){
121             a[ii].color = LIGHTRED;
122         }
123         
124     }
125     */
126 
127     for(int i = 0;i < cnt;i++){
128         a[i].color = RGB(len(a[i],q) / change,len(a[i],q) / change,len(a[i],q) / change);
129     }
130 
131     while(1){
132         /*
133         for(;;){
134             q.x += 1;
135             cleardevice();
136             for(int k = 0;k < maxp;k++){
137                 print(a[k]);
138             }
139             Sleep(T);
140         }
141         */
142         cleardevice();
143         for(int k = 0;k < maxp;k++){
144             print(a[k]);
145         }
146         Sleep(T);
147         get = getch();
148         if(get == 0x48)q.y += 10;
149         if(get == 0x50)q.y -= 10;
150         if(get == 0x4b)q.x -= 10;
151         if(get == 0x4d)q.x += 10;
152         if(get == 0x77)q.z += 10;
153         if(get == 0x73)q.z -= 10;
154         if(get == 0x1b)return;
155     }
156     
157     getch();
158     closegraph();        
159     
160 }

 在easyx环境下编译。。。

键盘上下左右控制。

原文地址:https://www.cnblogs.com/frankying/p/6613143.html