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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <math.h>
#include <windows.h>
#include<gdiplus.h>
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
using namespace std;
using namespace Gdiplus;
struct point
{
float x,y,z;
} ;
struct angle
{
float sx,sy,sz;
float cx,cy,cz;
} ;
void construct(angle &res,const point &a)
{
res={sin(a.x),sin(a.y),sin(a.z),cos(a.x),cos(a.y),cos(a.z)};
}
point rotate(const point &c,const point &p,const angle &a,const point &scale={1,1,1})
{
float dx=p.x-c.x,dy=p.y-c.y,dz=p.z-c.z;
return{(scale.x)*((a.cy*a.cz)*dx+(-a.cx*a.sz+a.sx*a.sy*a.cz)*dy+(a.sx*a.sz+a.cx*a.sy*a.cz)*dz)+c.x,
(scale.y)*((a.cy*a.sz)*dx+(a.cx*a.cz+a.sx*a.sy*a.sz)*dy+(-a.sx*a.cz+a.cx*a.sy*a.sz)*dz)+c.y,
(scale.z)*((-a.sy)*dx+(a.sx*a.cy)*dy+(a.cx*a.cy)*dz)+c.z};
}
point perspective(const point &p,const point &eyepoint)
{
float w=1+(p.z/eyepoint.z);
return {(p.x-eyepoint.x)/w+eyepoint.x,
(p.y-eyepoint.y)/w+eyepoint.y,
(p.z-eyepoint.z)/w+eyepoint.z};
}
void bubblesort(point p[])
{
for (int i=0;i<=6;i++)
{
for(int j=i+1;j<=7;j++)
{
if(p[i].z<p[j].z) swap (p[i],p[j]);
}
}
}
void circle(const HDC &WinHDC,int x,int y,int r)
{
Ellipse(WinHDC,(x-r),(y-r),(x+r),(y+r));
}
void draw(const HDC &WinHDC,const point rot[])
{
for (int i=0 ;i<8;i++)
{
int rad=map(-200,200,rot[i].z,15,10);
circle(WinHDC,rot[i].x,rot[i].y,rad);
}
}
void initpaint(HDC WinHDC)
{
SelectObject(WinHDC,GetStockObject(DC_BRUSH));
SelectObject(WinHDC,GetStockObject(DC_PEN));
SetDCBrushColor(WinHDC,RGB(200,0,0));
SetDCPenColor(WinHDC,RGB(0,0,200));
}
int main()
{
point pts[8],rot[8];
pts[0]={200,100,200};
pts[1]={600,100,200};
pts[2]={200,500,200};
pts[3]={600,500,200};
pts[4]={200,100,-200};
pts[5]={600,100,-200};
pts[6]={200,500,-200};
pts[7]={600,500,-200};
HWND mainwin;
MSG emsg;
mainwin=CreateWindowEx(0,"#32770","Basic GDI. Press <escape> to end.",WS_OVERLAPPEDWINDOW | WS_VISIBLE,200,200,800,600,0,0,0,0);
PAINTSTRUCT ps;
HDC hdc2;
hdc2=BeginPaint(mainwin, &ps);
initpaint(hdc2);
point ang;
angle A;
while (!(GetKeyState(VK_ESCAPE) & 0x8000))
{
while (PeekMessage (&emsg, NULL, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage (&emsg);
DispatchMessage (&emsg);
}
// do the graphics
BitBlt(GetDC(mainwin), 0,0, 800,600, 0,0,0, WHITENESS);
ang.x+=.01;
ang.y+=.01/2;
ang.z+=.01/3;
construct(A,ang);
for(int n=0;n<=7;n++)
{
rot[n]=rotate({400,300,0},pts[n],A,{.5,.5,.5});
rot[n]=perspective(rot[n],{400,300,600});
}
bubblesort(rot);
draw(hdc2,rot);
Sleep(1);
}
}
| |