|
发表于 2020-9-3 14:15:01
|
显示全部楼层
[code=C/C++]
#include <cmath>
void CD3DrawDlg::Draw3D()
{
const COLORREF CR_NULL = RGB(0,0,0); //无点处是黑色
const COLORREF CR_POINT = RGB(0,0,255); //有点处是蓝色
const int MAXRANGE = 10; //测试数组的大小
const int NULLDIA = 3; //没有数据点的直径
const int POINTDIA = 8; //有数据点的直径
const int POINTDST = 20; //点的距离
const int AXISLENGTH = 200; //x,y,z轴长
//这个是Z = 1平面上的 X = Y 的曲线
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};
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)");//画原点
dc.MoveTo(X0,Y0); //画X轴
dc.LineTo(X0 + AXISLENGTH,Y0);
dc.TextOut(X0 + AXISLENGTH + 10,Y0 - 20,"X轴");
dc.MoveTo(X0,Y0); //画Y轴
dc.LineTo(X0 - int(AXISLENGTH*sqrt(0.5)),Y0 + 141);
dc.TextOut(X0 - int(AXISLENGTH*sqrt(0.5)) - 35,Y0 + 141,"Y轴");
dc.MoveTo(X0,Y0); //画Z轴
dc.LineTo(X0,Y0 - AXISLENGTH);
dc.TextOut(X0 - 35,Y0 - AXISLENGTH - 20,"Z轴");
//画出所有坐标点
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)//这个操作可以省去不画,画面会简洁一些
{
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);
}
}
}
//画数组里的点
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]
终于写好了,试过可以的. |
|