设计一个要进行图形变换的二维平面图形(比如一个多边形),编程实现5种基本几何变换(平移、比例缩放、旋转、对称、错切),运行程序,分别显示出变换前、后的图形。
平移:
// suoView.cpp : implementation of the CSuoView class
//
#include "stdafx.h"
#include "suo.h"
#include "suoDoc.h"
#include "suoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CSuoView
void ping(POINT p[20],int m,float MoveX,float MoveY)
{
for(int i=0;i<m;i++)
{p[i].x=p[i].x+MoveX;p[i].y=p[i].y+MoveY;}
}
IMPLEMENT_DYNCREATE(CSuoView, CView)
BEGIN_MESSAGE_MAP(CSuoView, CView)
//{{AFX_MSG_MAP(CSuoView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CSuoView construction/destruction
CSuoView::CSuoView()
{
// TODO: add construction code here
}
CSuoView::~CSuoView()
{
}
BOOL CSuoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CSuoView drawing
void CSuoView::OnDraw(CDC* pDC)
{
CSuoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int color,n=4;
float th,x0,y0,SkewX,SkewY,ScaleX,ScaleY,MoveX,MoveY;
POINT ply[4]={{75,100},{110,100},{150,30},{120,30}},p0={300,300},ps={20,20},pe={300,400};
th=90;x0=p0.x;y0=p0.y;SkewX=0.6;SkewY=0;ScaleX=2.0;ScaleY=2.0;MoveX=100,MoveY=100;
//
color=RGB(28,28,28);
CPen pen1(PS_DASH,1,color);
pDC->SelectObject(&pen1);
pDC->Polygon(ply,n);
ping(ply,n,MoveX,MoveY);
color=RGB(255,0,0);
CPen pen2(PS_SOLID,2,color);
pDC->SelectObject(&pen2);
pDC->Polygon(ply,n);
}
/
// CSuoView printing
BOOL CSuoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSuoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSuoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CSuoView diagnostics
#ifdef _DEBUG
void CSuoView::AssertValid() const
{
CView::AssertValid();
}
void CSuoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSuoDoc* CSuoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSuoDoc)));
return (CSuoDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CSuoView message handlers
比例缩放:
// PingView.cpp : implementation of the CPingView class
//
#include "stdafx.h"
#include "Ping.h"
#include "PingDoc.h"
#include "PingView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CPingView
void suofang(POINT p[20],int m,float ScaleX,float ScaleY)
{
for(int i=0;i<m;i++)
{ p[i].x=p[i].x*ScaleX;
p[i].y=p[i].y*ScaleY;
}
}
IMPLEMENT_DYNCREATE(CPingView, CView)
BEGIN_MESSAGE_MAP(CPingView, CView)
//{{AFX_MSG_MAP(CPingView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CPingView construction/destruction
CPingView::CPingView()
{
// TODO: add construction code here
}
CPingView::~CPingView()
{
}
BOOL CPingView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CPingView drawing
void CPingView::OnDraw(CDC* pDC)
{
CPingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int color,n=4;
float th,x0,y0,SkewX,SkewY,ScaleX,ScaleY,MoveX,MoveY;
POINT ply[4]={{75,100},{110,100},{150,30},{120,30}},p0={300,300},ps={20,20},pe={300,400};
th=90;x0=p0.x;y0=p0.y;SkewX=0.4;SkewY=0;ScaleX=2.0;ScaleY=2.0;MoveX=400,MoveY=200;
//
color=RGB(28,28,28);
CPen pen1(PS_DASH,1,color);
pDC->SelectObject(&pen1);
pDC->Polygon(ply,n);
suofang(ply,n,ScaleX,ScaleY);
//
color=RGB(255,0,0);
CPen pen2(PS_SOLID,2,color);
pDC->SelectObject(&pen2);
pDC->Polygon(ply,n);
}
/
// CPingView printing
BOOL CPingView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CPingView diagnostics
#ifdef _DEBUG
void CPingView::AssertValid() const
{
CView::AssertValid();
}
void CPingView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPingDoc* CPingView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPingDoc)));
return (CPingDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CPingView message handlers
旋转:
// xuanView.cpp : implementation of the CXuanView class
//
#include "stdafx.h"
#include "xuan.h"
#include <cmath>
#include "xuanDoc.h"
#include "xuanView.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846 // 定义 PI 常量
#endif
void RotateTransform(POINT p[20], int m, POINT Cen, float theta, CDC* pDC)
{
int i;
double a, b;
POINT p1[20];
double alfa = theta * M_PI / 180; // 使用 M_PI 替代 PI
a = cos(alfa); // 使用 cos 函数
b = sin(alfa); // 使用 sin 函数
for (i = 0; i < m; i++)
{
p1[i].x = p[i].x * a - p[i].y * b + Cen.x * (1 - a) + Cen.y * b;
p1[i].y = p[i].x * b + p[i].y * a - Cen.x * b + Cen.y * (1 - a);
}
for (i = 0; i < m; i++)
{
p[i].x = p1[i].x;
p[i].y = p1[i].y;
}
}
IMPLEMENT_DYNCREATE(CXuanView, CView)
BEGIN_MESSAGE_MAP(CXuanView, CView)
//{{AFX_MSG_MAP(CXuanView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CXuanView construction/destruction
CXuanView::CXuanView()
{
// TODO: add construction code here
}
CXuanView::~CXuanView()
{
}
BOOL CXuanView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CXuanView drawing
void CXuanView::OnDraw(CDC* pDC)
{
CXuanDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int color, n = 4;
float th, x0, y0, SkewX, SkewY, ScaleX, ScaleY, MoveX, MoveY;
POINT ply[4] = {{50, 75}, {150, 100}, {150, 200}, {120, 180}}, p0 = {300, 300}, ps = {200, 20}, pe = {300, 400};
th = 140;
x0 = p0.x;
y0 = p0.y;
SkewX = 0.6;
SkewY = 0;
ScaleX = 2.0;
ScaleY = 2.0;
MoveX = 400;
MoveY = 200;
color = RGB(28, 28, 28);
CPen pen1(PS_DASH, 1, color);
pDC->SelectObject(&pen1);
pDC->Polygon(ply, n);
RotateTransform(ply, n, p0, th, pDC);
color = RGB(255, 0, 0);
CPen pen2(PS_SOLID, 2, color);
pDC->SelectObject(&pen2);
pDC->Polygon(ply, n);
}
/
// CXuanView printing
BOOL CXuanView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CXuanView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CXuanView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CXuanView diagnostics
#ifdef _DEBUG
void CXuanView::AssertValid() const
{
CView::AssertValid();
}
void CXuanView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CXuanDoc* CXuanView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CXuanDoc)));
return (CXuanDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CXuanView message handlers
对称:
// duiView.cpp : implementation of the CDuiView class
//
#include "stdafx.h"
#include "dui.h"
#include "duiDoc.h"
#include "duiView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CDuiView
void dui(POINT p[20],int m,POINT ps,POINT pe,CDC *pDC)
{
int i;POINT p1[20];double A,B,C;
A=pe.y-ps.y;B=ps.x-pe.x;C=-(A*ps.x+B*ps.y);
for(i=0;i<m;i++)
{
p1[i].x=((B*B-A*A)*p[i].x-2*A*B*p[i].y-2*A*C)/(A*A+B*B);
p1[i].y=((A*A-B*B)*p[i].y-2*A*B*p[i].x-2*B*C)/(A*A+B*B);
}
for(i=0;i<m;i++)
{
p[i].x=p1[i].x; p[i].y=p1[i].y;
}
CPen pen(PS_DASHDOT,1,RGB(0,0,255));
pDC->SelectObject(&pen);
pDC->MoveTo(ps.x,ps.y);
pDC->LineTo(pe.x,pe.y);
}
IMPLEMENT_DYNCREATE(CDuiView, CView)
BEGIN_MESSAGE_MAP(CDuiView, CView)
//{{AFX_MSG_MAP(CDuiView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CDuiView construction/destruction
CDuiView::CDuiView()
{
// TODO: add construction code here
}
CDuiView::~CDuiView()
{
}
BOOL CDuiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CDuiView drawing
void CDuiView::OnDraw(CDC* pDC)
{
CDuiDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int color,n=4;
float th,x0,y0,SkewX,SkewY,ScaleX,ScaleY,MoveX,MoveY;
POINT ply[4]={{50,75},{110,100},{150,300},{120,30}},p0={50,300},ps={200,200},pe={30,40};
th=90;x0=p0.x;y0=p0.y;SkewX=0.6;SkewY=0;ScaleX=2.0;ScaleY=2.0;MoveX=400,MoveY=200;
//
color=RGB(28,28,28);
CPen pen1(PS_DASH,1,color);
pDC->SelectObject(&pen1);
pDC->Polygon(ply,n);
dui(ply,n,ps,pe,pDC);
//
color=RGB(255,0,0);
CPen pen2(PS_SOLID,2,color);
pDC->SelectObject(&pen2);
pDC->Polygon(ply,n);
}
/
// CDuiView printing
BOOL CDuiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDuiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDuiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CDuiView diagnostics
#ifdef _DEBUG
void CDuiView::AssertValid() const
{
CView::AssertValid();
}
void CDuiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDuiDoc* CDuiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDuiDoc)));
return (CDuiDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CDuiView message handlers
错切:
// tuView.cpp : implementation of the CTuView class
//
#include "stdafx.h"
#include "tu.h"
#include "tuDoc.h"
#include "tuView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CTuView
void cuoqie(POINT p[20],int m,float SkewX,float SkewY)
{
for(int i=0;i<m;i++)
{
p[i].x=p[i].x+p[i].y*SkewX; p[i].y=p[i].y+p[i].x*SkewY;
}
}
IMPLEMENT_DYNCREATE(CTuView, CView)
BEGIN_MESSAGE_MAP(CTuView, CView)
//{{AFX_MSG_MAP(CTuView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CTuView construction/destruction
CTuView::CTuView()
{
// TODO: add construction code here
}
CTuView::~CTuView()
{
}
BOOL CTuView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CTuView drawing
void CTuView::OnDraw(CDC* pDC)
{
CTuDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int color,n=4;
float th,x0,y0,SkewX,SkewY,ScaleX,ScaleY,MoveX,MoveY;
POINT ply[4]={{300,400},{150,200},{300,200},{250,180}},p0={240,240},ps={190,200},pe={150,140};
th=90;x0=p0.x;y0=p0.y;SkewX=0.4;SkewY=0;ScaleX=2.0;ScaleY=2.0;MoveX=400,MoveY=300;
//
color=RGB(28,28,28);
CPen pen1(PS_DASH,1,color);
pDC->SelectObject(&pen1);
pDC->Polygon(ply,n);
cuoqie(ply,n,SkewX,SkewY);
color=RGB(144,238,144);
CPen pen2(PS_SOLID,2,color);
pDC->SelectObject(&pen2);
pDC->Polygon(ply,n);
}
/
// CTuView printing
BOOL CTuView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTuView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTuView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CTuView diagnostics
#ifdef _DEBUG
void CTuView::AssertValid() const
{
CView::AssertValid();
}
void CTuView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTuDoc* CTuView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTuDoc)));
return (CTuDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CTuView message handlers