#include "library.h" const double pi=acos(-1); const int scrw=500, scrh=500; struct point { double x, y; }; void print(point p) { print("("); print(p.x); print(","); print(p.y); print(")"); } double distance(point a, point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } point rotate(point p, double angle) { double r=sqrt(p.x*p.x+p.y*p.y); double theta=atan2(p.x, p.y); theta=theta+angle; point q; q.x=r*sin(theta); q.y=r*cos(theta); return q; } point scale(point p, double factor) { point q; q.x=p.x*factor; q.y=p.y*factor; return q; } struct point3d { double x, y, z; }; point3d rotatex(point3d p, double a) { point3d q; q.x=p.x; q.y=p.y*cos(a)+p.z*sin(a); q.z=p.z*cos(a)-p.y*sin(a); return q; } point3d rotatey(point3d p, double a) { point3d q; q.x=p.x*cos(a)+p.z*sin(a); q.y=p.y; q.z=p.z*cos(a)-p.x*sin(a); return q; } point3d rotatez(point3d p, double a) { point3d q; q.x=p.x*cos(a)+p.y*sin(a); q.y=p.y*cos(a)-p.x*sin(a); q.z=p.z; return q; } point project(point3d p) { const double from=500; const double scale=from/(from-p.z); point q; q.x=p.x*scale+scrw/4; q.y=p.y*scale+scrh/2; return q; } struct cube { point3d p1, p2, p3, p4, p5, p6, p7, p8; }; cube rotatex(cube c, double a) { cube d; d.p1=rotatex(c.p1, a); d.p2=rotatex(c.p2, a); d.p3=rotatex(c.p3, a); d.p4=rotatex(c.p4, a); d.p5=rotatex(c.p5, a); d.p6=rotatex(c.p6, a); d.p7=rotatex(c.p7, a); d.p8=rotatex(c.p8, a); return d; } cube rotatey(cube c, double a) { cube d; d.p1=rotatey(c.p1, a); d.p2=rotatey(c.p2, a); d.p3=rotatey(c.p3, a); d.p4=rotatey(c.p4, a); d.p5=rotatey(c.p5, a); d.p6=rotatey(c.p6, a); d.p7=rotatey(c.p7, a); d.p8=rotatey(c.p8, a); return d; } cube rotatez(cube c, double a) { cube d; d.p1=rotatez(c.p1, a); d.p2=rotatez(c.p2, a); d.p3=rotatez(c.p3, a); d.p4=rotatez(c.p4, a); d.p5=rotatez(c.p5, a); d.p6=rotatez(c.p6, a); d.p7=rotatez(c.p7, a); d.p8=rotatez(c.p8, a); return d; } void draw(cube c) { PenColor(PEN_BLUE); PenWidth(3); point p; p=project(c.p1); MoveTo(p.x, p.y); p=project(c.p2); DrawLineTo(p.x, p.y); p=project(c.p3); DrawLineTo(p.x, p.y); p=project(c.p4); DrawLineTo(p.x, p.y); p=project(c.p1); DrawLineTo(p.x, p.y); p=project(c.p5); MoveTo(p.x, p.y); p=project(c.p6); DrawLineTo(p.x, p.y); p=project(c.p7); DrawLineTo(p.x, p.y); p=project(c.p8); DrawLineTo(p.x, p.y); p=project(c.p5); DrawLineTo(p.x, p.y); p=project(c.p1); MoveTo(p.x, p.y); p=project(c.p5); DrawLineTo(p.x, p.y); p=project(c.p2); MoveTo(p.x, p.y); p=project(c.p6); DrawLineTo(p.x, p.y); p=project(c.p3); MoveTo(p.x, p.y); p=project(c.p7); DrawLineTo(p.x, p.y); p=project(c.p4); MoveTo(p.x, p.y); p=project(c.p8); DrawLineTo(p.x, p.y); } void whiteout(void) { PenColor(PEN_WHITE); FillRectangleXYWH(0, 0, 500, 500); } void main(void) { cube xxx = { { 70, 70, 70 }, { 210, 70, 70 }, { 210, 70, -70 }, { 70, 70, -70 }, { 70, 210, 70 }, { 210, 210, 70 }, { 210, 210, -70 }, { 70, 210, -70 } }; MakeWindow(scrw, scrh); while (true) { draw(xxx); xxx = rotatex(xxx, 0.03); xxx = rotatey(xxx, 0.01); Delay(0.03); whiteout(); } }