|
[code=C/C++]
#include <cmath>
void CD3DrawDlg::Draw3D()
{
const COLORREF CR_NULL = RGB(0,0,0); //there is black where there is no dot
const COLORREF CR_POINT = RGB(0,0,255); //A little bit is blue
const int MAXRANGE = 10; //Test the size of the array
const int NULLDIA = 3; //No data point diameter
const int POINTDIA = 8; //The diameter of the data point
const int POINTDST = 20; //The distance of the point
const int AXISLENGTH = 200; //x,y,z axis length
To
//This is the X = Y curve on the Z = 1 plane
int xdata[MAXRANGE] = {1,2,3,4,5,6,7,8,9,10};
int ydata[MAXRANGE] = {1,2,3,4,5,6,7,8,9,10};
int zdata[MAXRANGE] = {1,1,1,1,1,1,1,1,1,1};
To
CRect rt;
GetClientRect(rt);
CClientDC dc(this);
int X0 = rt.Width()/2;
int Y0 = rt.Height()/2;
dc.TextOut(X0-35,Y0-20,"(0,0)");//Draw origin
To
dc.MoveTo(X0,Y0); //Draw X axis
dc.LineTo(X0 + AXISLENGTH,Y0);
dc.TextOut(X0 + AXISLENGTH + 10, Y0-20, "X axis");
To
dc.MoveTo(X0,Y0); //Draw Y axis
dc.LineTo(X0-int(AXISLENGTH*sqrt(0.5)),Y0 + 141);
dc.TextOut(X0-int(AXISLENGTH*sqrt(0.5))-35,Y0 + 141,"Y axis");
To
dc.MoveTo(X0,Y0); //Draw Z axis
dc.LineTo(X0,Y0-AXISLENGTH);
dc.TextOut(X0-35, Y0-AXISLENGTH-20, "Z axis");
//Draw all coordinate points
int x,y,z;
CBrush br(CR_NULL);
CPen pen(PS_SOLID,1,CR_NULL);
dc.SelectObject(&br);
dc.SelectObject(&pen);
for (x=0;x<MAXRANGE;++x)//This operation can be omitted, and the screen will be simpler
{
for (y=0;y<MAXRANGE;++y)
{
for (z=0;z<MAXRANGE;++z)
{
dc.Ellipse(
X0 + x*POINTDST-int(sqrt(0.5)*POINTDST*y),
Y0-z*POINTDST + int(sqrt(0.5)*POINTDST*y),
X0 + x*POINTDST-int(sqrt(0.5)*POINTDST*y) + NULLDIA,
Y0-z*POINTDST + int(sqrt(0.5)*POINTDST*y) + NULLDIA);
}
}
}
//Draw the points in the array
CPen penPt(PS_SOLID,1,CR_POINT);
CBrush brPt(CR_POINT);
dc.SelectObject(&penPt);
dc.SelectObject(&brPt);
int i=0;
for (i=0;i<MAXRANGE;++i)
{
dc.Ellipse(
X0 + xdata[i]*POINTDST-int(sqrt(0.5)*POINTDST*ydata[i]),
Y0-zdata[i]*POINTDST + int(sqrt(0.5)*POINTDST*ydata[i]),
X0 + xdata[i]*POINTDST-int(sqrt(0.5)*POINTDST*ydata[i]) + POINTDIA,
Y0-zdata[i]*POINTDST + int(sqrt(0.5)*POINTDST*ydata[i]) + POINTDIA);
}
}
[/code]
Finally finished it, tried it. |
|