计算机图形学正方形简单旋转,计算机图形学之二维平移旋转缩放代码

以下均为MFC工程代码

代码一:简单实现

void CGeotranView::Onyuantu()

{

// 原图,画三角形

CDC *pDC=GetDC();

pDC->MoveTo(100,100);

pDC->LineTo(200,50);

pDC->LineTo(200,150);

pDC->LineTo(100,100);

ReleaseDC(pDC);

}

void CGeotranView::Ontranslation()

{

// 平移 tx=50,ty=60

CDC *pDC=GetDC();

int tx=50,ty=60;

pDC->MoveTo(100+tx,100+ty);

pDC->LineTo(200+tx,50+ty);

pDC->LineTo(200+tx,150+ty);

pDC->LineTo(100+tx,100+ty);

ReleaseDC(pDC);

}

void CGeotranView::Onrotation()

{

// 旋转, 角度sita=30度

CDC *pDC=GetDC();

double sita=30;

double hudu=sita*3.14159/180;

pDC->MoveTo(int(100*cos(hudu)-100*sin(hudu)),int(100*sin(hudu)+100*cos(hudu)));

pDC->LineTo(int(200*cos(hudu)-50*sin(hudu)),int(200*sin(hudu)+50*cos(hudu)));

pDC->LineTo(int(200*cos(hudu)-150*sin(hudu)),int(200*sin(hudu)+150*cos(hudu)));

pDC->LineTo(int(100*cos(hudu)-100*sin(hudu)),int(100*sin(hudu)+100*cos(hudu)));

ReleaseDC(pDC);

}

void CGeotranView::Onscaling()

{

// 缩放 sx=2,sy=3

int sx=2,sy=3;

CDC *pDC=GetDC();

pDC->MoveTo(100*sx,100*sy);

pDC->LineTo(200*sx,50*sy);

pDC->LineTo(200*sx,150*sy);

pDC->LineTo(100*sx,100*sy);

ReleaseDC(pDC);

}

代码二:新的点的坐标计算通过函数实现

POINT translationPoint(POINT point,int tx,int ty)

{

POINT newpoint;

newpoint.x=point.x+tx;

newpoint.y=point.y+ty;

return newpoint;

}

POINT rotationPoint(POINT point,double sita)

{

POINT newpoint;

double hudu=sita*3.14159/180;

newpoint.x=(int)(point.x*cos(hudu)-point.y*sin(hudu));

newpoint.y=(int)(point.x*sin(hudu)+point.y*cos(hudu));

return newpoint;

}

POINT scalingPoint(POINT point,int sx,int sy)

{

POINT newpoint;

newpoint.x=point.x*sx;

newpoint.y=point.y*sy;

return newpoint;

}

void CGeotranView::Onyuantu2()

{

POINT point1,point2,point3;

point1.x=200;point1.y=200;

point2.x=300;point3.y=150;

point3.x=300;point2.y=250;

// 原图,画三角形

CDC *pDC=GetDC();

pDC->MoveTo(point1);

pDC->LineTo(point2);

pDC->LineTo(point3);

pDC->LineTo(point1);

ReleaseDC(pDC);

}

void CGeotranView::Ontranslation2()

{

POINT point1,point2,point3;

point1.x=200;point1.y=200;

point2.x=300;point3.y=150;

point3.x=300;point2.y=250;

// 平移 tx=50,ty=60

CDC *pDC=GetDC();

int tx=50,ty=60;

pDC->MoveTo(translationPoint(point1,tx,ty));

pDC->LineTo(translationPoint(point2,tx,ty));

pDC->LineTo(translationPoint(point3,tx,ty));

pDC->LineTo(translationPoint(point1,tx,ty));

ReleaseDC(pDC);

}

void CGeotranView::Onrotation2()

{

POINT point1,point2,point3;

point1.x=200;point1.y=200;

point2.x=300;point3.y=150;

point3.x=300;point2.y=250;

// 旋转, 角度sita=30度

CDC *pDC=GetDC();

double sita=30;

pDC->MoveTo(rotationPoint(point1,sita));

pDC->LineTo(rotationPoint(point2,sita));

pDC->LineTo(rotationPoint(point3,sita));

pDC->LineTo(rotationPoint(point1,sita));

ReleaseDC(pDC);

}

void CGeotranView::Onscaling2()

{

POINT point1,point2,point3;

point1.x=200;point1.y=200;

point2.x=300;point3.y=150;

point3.x=300;point2.y=250;

// 缩放 sx=2,sy=2

int sx=2,sy=2;

CDC *pDC=GetDC();

pDC->MoveTo(scalingPoint(point1,sx,sy));

pDC->LineTo(scalingPoint(point2,sx,sy));

pDC->LineTo(scalingPoint(point3,sx,sy));

pDC->LineTo(scalingPoint(point1,sx,sy));

ReleaseDC(pDC);

}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值