📄 lytgraphicsview.cpp
字号:
void CLYTGraphicsView::OnRectangle()
{
// TODO: Add your command handler code here
s=2;
}
void CLYTGraphicsView::OnEllipse()
{
s=1;
// TODO: Add your command handler code here
}
void CLYTGraphicsView::OnDuobianxing()
{
// TODO: Add your command handler code here
s=4;
}
void CLYTGraphicsView::OnClear1()
{
// TODO: Add your command handler code here
Invalidate();
}
void CLYTGraphicsView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(s!=4) flag1=0;//控制绘制基本图形的
CView::OnLButtonUp(nFlags, point);
}
void CLYTGraphicsView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
flag1=0;//控制绘制基本图形的
CView::OnLButtonDblClk(nFlags, point);
}
void CLYTGraphicsView::parabola(CPen *poldPen, CPoint *pp, int n)//pp:型值点的坐标数组;n:型值点数
{
int x,y,i,j,k=100; //k插值数
double t1,t2,t3,t,a,b,c,d;
CClientDC dc(this);
dc.SelectObject(poldPen);
t=1.0/k;
for(i=0;i<n-3;i++) //画每两个型值点间的曲线段
{
for(j=0;j<=k;j++) //参数t1在(0,0.5)范围内变化
{
t1=j*t;
t2=t1*t1; //t2即t1^2
t3=t1*t2; //t3即t1^3
a=(-t3+3*t2-3*t1+1)/6;
b=(3*t3-6*t2+4)/6;
c=(-3*t3+3*t2+3*t1+1)/6;
d=(t3)/6;
x=int(a*pp[i].x+b*pp[i+1].x+c*pp[i+2].x+d*pp[i+3].x);
y=int(a*pp[i].y+b*pp[i+1].y+c*pp[i+2].y+d*pp[i+3].y);
SetPixel(dc,x,y,RGB(0,200,0));
}
}
}
void CLYTGraphicsView::OnDrawByt()
{
// TODO: Add your command handler code here
flag3=1;
CClientDC dc(this);
CPen yellow_pen(PS_SOLID,3,RGB(255,255,0));
CPen *poldPen=dc.SelectObject(&yellow_pen);
parabola(poldPen,p,m_PN);
m_PN=0;
}
void CLYTGraphicsView::MatrixMultiply(float a[][3], float b[][3])//两个矩阵相乘的函数
{
int i,j,k;
float c[3][3]={0};
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<3;i++)
for(j=0;j<2;j++)
p4[i][j]=c[i][j];
}
void CLYTGraphicsView::InitialMatrix()//将变换矩阵初始化为单位矩阵
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(j==i) t[i][j]=1;
else t[i][j]=0;
}
void CLYTGraphicsView::OnScale() //相对于原点比例缩放
{
// TODO: Add your command handler code here
if(dlg1.DoModal()==IDOK)
{
s4=5;
UpdateData(true);
InitialMatrix();
t[0][0]=dlg1.m1;
t[1][1]=dlg1.m2;
MatrixMultiply(p4,t);
Invalidate();
}
}
void CLYTGraphicsView::OnDrawAngle() //绘制一个三角形
{
// TODO: Add your command handler code here
s4=0;
flag4=1;
}
void CLYTGraphicsView::OnScale1() //相对于任意点缩放
{
// TODO: Add your command handler code here
s4=4;
}
void CLYTGraphicsView::OnXsymmetry() //相对于x轴做对称变换
{
s4=5;
InitialMatrix();
t[1][1]=-1;
MatrixMultiply(p4,t);
Invalidate();
// TODO: Add your command handler code here
}
void CLYTGraphicsView::OnYsymmetry() //相对于y轴做对称变换
{
// TODO: Add your command handler code here
s4=5;
InitialMatrix();
t[0][0]=-1;
MatrixMultiply(p4,t);
Invalidate();
}
void CLYTGraphicsView::OnOsymmetry() //相对于原点做对称变换
{
// TODO: Add your command handler code here
s4=5;
InitialMatrix();
t[0][0]=-1;
t[1][1]=-1;
MatrixMultiply(p4,t);
Invalidate();
}
void CLYTGraphicsView::OnRotationOriginal() //绕原点旋转
{
// TODO: Add your command handler code here
s4=5;
if(dlg2.DoModal()==IDOK)
{
UpdateData(true);
float a=dlg2.angle*3.142/180;
InitialMatrix();
/*计算旋转矩阵*/
t[0][0]=t[1][1]=cos(a);
t[0][1]=sin(a);
t[1][0]=-sin(a);
MatrixMultiply(p4,t);
Invalidate();
}
}
void CLYTGraphicsView::OnRotationAnypoint() //绕任意点旋转(即绕鼠标左键按下的点旋转)
{
// TODO: Add your command handler code here
s4=3;//转鼠标左键按下得消息响应函数
}
void CLYTGraphicsView::OnShear() //进行错切变换
{
// TODO: Add your command handler code here
s4=5;
if(dlg3.DoModal()==IDOK)
{
UpdateData(true);
InitialMatrix();
t[0][1]=dlg3.m1;
t[1][0]=dlg3.m2;
MatrixMultiply(p4,t);
Invalidate();
}
}
void CLYTGraphicsView::OnClearTwo()
{
// TODO: Add your command handler code here
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
p4[i][j]=0;
s4=5;
flag4=0;
n4=0;
Invalidate();
}
void CLYTGraphicsView::OnDrawCuboid() //绘制长方体的消息响应函数
{
// TODO: Add your command handler code here
flag5=1;
RedrawWindow();//Updates a specified rectangle or region in the client area.
point[0].x=0;point[0].y=0;point[0].z=0;
point[1].x=0;point[1].y=0;point[1].z=100;
point[2].x=100;point[2].y=0;point[2].z=100;
point[3].x=100;point[3].y=0;point[3].z=0;
point[4].x=0;point[4].y=100;point[4].z=0;
point[5].x=0;point[5].y=100;point[5].z=100;
point[6].x=100;point[6].y=100;point[6].z=100;
point[7].x=100;point[7].y=100;point[7].z=0;
DrawGraphics(point,8);
}
void CLYTGraphicsView::Transform3DTo2D(Point2D *pt2d, const Point3D *pt3d, const int &n)
{
for(int i=0;i<n;i++)
{
pt2d[i].x=pt3d[i].x - pt3d[i].z /sqrt(2);
pt2d[i].y=pt3d[i].y - pt3d[i].z /sqrt(2);
}
}
void CLYTGraphicsView::ClearMatrix()//将存放长方体顶点的矩阵清零
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
matrix[i][j]=0;
}
void CLYTGraphicsView::DrawGraphics(const Point3D *pt, const int &n)//绘制长方体
{
CClientDC dc(this);
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
CPen *pOld=dc.SelectObject(&pen);
Point2D * point=new Point2D[8];
Transform3DTo2D(point,pt,n);
dc.MoveTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.LineTo(ROUND(point[1].x+MaxX/2),ROUND(MaxY/2-point[1].y));
dc.LineTo(ROUND(point[2].x+MaxX/2),ROUND(MaxY/2-point[2].y));
dc.LineTo(ROUND(point[3].x+MaxX/2),ROUND(MaxY/2-point[3].y));
dc.LineTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.MoveTo(ROUND(point[4].x+MaxX/2),ROUND(MaxY/2-point[4].y));
dc.LineTo(ROUND(point[5].x+MaxX/2),ROUND(MaxY/2-point[5].y));
dc.LineTo(ROUND(point[6].x+MaxX/2),ROUND(MaxY/2-point[6].y));
dc.LineTo(ROUND(point[7].x+MaxX/2),ROUND(MaxY/2-point[7].y));
dc.LineTo(ROUND(point[4].x+MaxX/2),ROUND(MaxY/2-point[4].y));
dc.MoveTo(ROUND(point[2].x+MaxX/2),ROUND(MaxY/2-point[2].y));
dc.LineTo(ROUND(point[3].x+MaxX/2),ROUND(MaxY/2-point[3].y));
dc.LineTo(ROUND(point[7].x+MaxX/2),ROUND(MaxY/2-point[7].y));
dc.LineTo(ROUND(point[6].x+MaxX/2),ROUND(MaxY/2-point[6].y));
dc.LineTo(ROUND(point[2].x+MaxX/2),ROUND(MaxY/2-point[2].y));
dc.MoveTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.LineTo(ROUND(point[1].x+MaxX/2),ROUND(MaxY/2-point[1].y));
dc.LineTo(ROUND(point[5].x+MaxX/2),ROUND(MaxY/2-point[5].y));
dc.LineTo(ROUND(point[4].x+MaxX/2),ROUND(MaxY/2-point[4].y));
dc.LineTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.MoveTo(ROUND(point[1].x+MaxX/2),ROUND(MaxY/2-point[1].y));
dc.LineTo(ROUND(point[2].x+MaxX/2),ROUND(MaxY/2-point[2].y));
dc.LineTo(ROUND(point[6].x+MaxX/2),ROUND(MaxY/2-point[6].y));
dc.LineTo(ROUND(point[5].x+MaxX/2),ROUND(MaxY/2-point[5].y));
dc.LineTo(ROUND(point[1].x+MaxX/2),ROUND(MaxY/2-point[1].y));
dc.MoveTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.LineTo(ROUND(point[3].x+MaxX/2),ROUND(MaxY/2-point[3].y));
dc.LineTo(ROUND(point[7].x+MaxX/2),ROUND(MaxY/2-point[7].y));
dc.LineTo(ROUND(point[4].x+MaxX/2),ROUND(MaxY/2-point[4].y));
dc.LineTo(ROUND(point[0].x+MaxX/2),ROUND(MaxY/2-point[0].y));
dc.SelectObject(pOld);
pen.DeleteObject();
}
Point3D * CLYTGraphicsView::TranslatePoint(Point3D *pt, double matrix[][4], const int &n)
{
for(int i=0;i<n;i++)
{
double a[4] = {pt[i].x,pt[i].y,pt[i].z,1};
pt[i].x = a[0]*matrix[0][0] + a[1]*matrix[1][0] + a[2]*matrix[2][0]+a[3]*matrix[3][0];
pt[i].y = a[0]*matrix[0][1] + a[1]*matrix[1][1] + a[2]*matrix[2][1]+a[3]*matrix[3][1];
pt[i].z = a[0]*matrix[0][2] + a[1]*matrix[1][2] + a[2]*matrix[2][2]+a[3]*matrix[3][2];
}
return pt;
}
void CLYTGraphicsView::GetMaxY()
{
CRect rect;
GetClientRect(&rect);
MaxY=rect.bottom;
}
void CLYTGraphicsView::GetMaxX()
{
CRect rect;
GetClientRect(&rect);
MaxX=rect.right;
}
void CLYTGraphicsView::OnThreeTranslate() //三维平移变换(通过对话框)
{
// TODO: Add your command handler code here
if(model==0)
{
model++;
pdlg=new LYTTHREE();
pdlg->Create(IDD_DIALOG4);
pdlg->ShowWindow(SW_SHOW);
CRect rect;
pdlg->GetWindowRect(&rect);
pdlg->MoveWindow(700,100,rect.Width(),rect.Height());//设置显示对话框的位置
}
model=0;//重新设置model的值,点cancel后第二次再点重新调入对话框
}
void CLYTGraphicsView::OnThreeScale() //三维比例变换
{
// TODO: Add your command handler code here
ClearMatrix();
matrix[0][0]=2;
matrix[1][1]=3;
matrix[2][2]=4;
matrix[3][3]=1;
RedrawWindow();
TranslatePoint(point,matrix,8);
DrawGraphics(point,8);
}
void CLYTGraphicsView::OnThreeRotation() //三维旋转变换
{
// TODO: Add your command handler code here
ClearMatrix();
matrix[0][0]=cos(30*PI/180);
matrix[0][1]=sin(30*PI/180);
matrix[1][0]=-sin(30*PI/180);
matrix[1][1]=cos(30*PI/180);
matrix[2][2]=1;
matrix[3][3]=1;
RedrawWindow();
TranslatePoint(point,matrix,8);
DrawGraphics(point,8);
}
void CLYTGraphicsView::OnThreeReflect() //反射变换
{
// TODO: Add your command handler code here
ClearMatrix();
matrix[0][0]=1;
matrix[1][1]=1;
matrix[2][2]=-1;
matrix[3][3]=1;
RedrawWindow();
TranslatePoint(point,matrix,8);
DrawGraphics(point,8);
}
void CLYTGraphicsView::OnThreeTwist() //三维错切变换
{
// TODO: Add your command handler code here
ClearMatrix();
matrix[0][0]=1;
matrix[1][1]=1;
matrix[2][2]=1;
matrix[3][3]=1;
matrix[1][0]=0.5;
matrix[2][0]=2;
RedrawWindow();
TranslatePoint(point,matrix,8);
DrawGraphics(point,8);
}
LRESULT CLYTGraphicsView::OnTranslate(WPARAM wParam,LPARAM lParam)//手工处理消息映射
{
ClearMatrix();
matrix[0][0]=1;
matrix[1][1]=1;
matrix[2][2]=1;
matrix[3][3]=1;
switch(wParam)
{
case 1:
matrix[3][1]=10;
break;
case 2:
matrix[3][1]=-10;
break;
case 3:
matrix[3][0]=-10;
break;
case 4:
matrix[3][0]=10;
break;
case 5:
matrix[3][2]=10;
break;
case 6:
matrix[3][2]=-10;
break;
case 7:
OnDrawCuboid();
return 0;
default:
break;
}
RedrawWindow();
TranslatePoint(point,matrix,8);
DrawGraphics(point,8);
return 0;
}
void CLYTGraphicsView::OnThreeClear()
{
// TODO: Add your command handler code here
flag5=0;
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -