?? fivepointview.cpp
字號:
// FivePointView.cpp : implementation of the CFivePointView class
//
#include "stdafx.h"
#include "FivePoint.h"
#include "FivePointDoc.h"
#include "FivePointView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFivePointView
IMPLEMENT_DYNCREATE(CFivePointView, CView)
BEGIN_MESSAGE_MAP(CFivePointView, CView)
//{{AFX_MSG_MAP(CFivePointView)
ON_COMMAND(PDraw, OnPDraw)
ON_COMMAND(PFlat, OnPFlat)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CFivePointView construction/destruction
CFivePointView::CFivePointView()
{
pointnum = 0;
m_flag = false;
m_isbegin = false;
this->m_color = RGB(0,0,0);
m_brush.CreateSolidBrush(m_color);
// TODO: add construction code here
}
CFivePointView::~CFivePointView()
{
}
BOOL CFivePointView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CFivePointView drawing
void CFivePointView::OnDraw(CDC* pDC)
{
CFivePointDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CFivePointView printing
BOOL CFivePointView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CFivePointView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CFivePointView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CFivePointView diagnostics
#ifdef _DEBUG
void CFivePointView::AssertValid() const
{
CView::AssertValid();
}
void CFivePointView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CFivePointDoc* CFivePointView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFivePointDoc)));
return (CFivePointDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFivePointView message handlers
void CFivePointView::OnPDraw()
{
m_flag = true;
this->Invalidate(true);
Plist.clear();
m_isbegin=false;
pointnum = 0;
// TODO: Add your command handler code here
}
void CFivePointView::OnPFlat()
{
list<CMypoint>::iterator Phead = Plist.begin();
list<CMypoint>::iterator Pend = Plist.end();
for(int i=2;Phead != Pend;i++){
x[i] = (*Phead).x;
y[i] = (*Phead).y;
Phead++;}
x[1] = x[2] - x[4] + x[3];
x[0] = x[2] - x[4] + x[2];
y[1] = (2*(y[2] - y[3])/(x[2] - x[3]) - (y[3] - y[4])/(x[3] - x[4]))*(x[1] - x[2]) + y[2];
y[0] = (2*(y[1] - y[2])/(x[1] - x[2]) - (y[2] - y[3])/(x[2] - x[3]))*(x[0] - x[1]) + y[1];
x[7] = x[6] - x[4] +x[5];
x[8] = x[6] - x[4] +x[6];
y[7] = (2*(y[6] - y[5])/(x[6] - x[5]) - (y[5] - y[4])/(x[5] - x[4]))*(x[7] - x[6]) + y[6];
y[8] = (2*(y[7] - y[6])/(x[7] - x[6]) - (y[6] - y[5])/(x[6] - x[5]))*(x[8] - x[7]) + y[7];
for(i=0;i<8;i++){
m[i] = x[i+1] - x[i];
n[i] = y[i+1] - y[i];
}
for(i=2;i<6;i++){
getab(i);
double du = (0.1)/fabs((x[i+1] - x[i]));
CClientDC dc(this);
for(double u = 0.0;u<=1;u=u+du){
dc.SetPixel((a[0] + a[1]*u + a[2]*u*u + a[3]*u*u*u),
(b[0] + b[1]*u + b[2]*u*u + b[3]*u*u*u),RGB(0,0,0));}
}
// TODO: Add your command handler code here
}
void CFivePointView::OnLButtonDown(UINT nFlags, CPoint point)
{
CMypoint pt;
if(m_flag){
if(!m_isbegin){
if(pointnum!=5)
{
CClientDC dc(this);
CBrush *poldbrush = dc.SelectObject(&m_brush);
dc.Ellipse(point.x-3,point.y-3,point.x+3,point.y+3);
}
pointnum++;
pt.x = point.x;
pt.y = point.y;
pt.PointNumber = pointnum;
if(pointnum < 6)
Plist.push_back(pt);
if(pointnum == 6){
AfxMessageBox("只需要五個點!");
m_isbegin = true;
}
}
}
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
void CFivePointView::getab(int i)
{
double r;
r = sqrt((x[i+1] - x[i])*(x[i+1] - x[i]) + (y[i+1] - y[i])*(y[i+1] - y[i]));
a[0] = x[i];
a[1] = r*getcos(i);
a[2] = 3*(x[i+1] - x[i]) - r*(getcos(i+1) + 2*getcos(i));
a[3] = -2*(x[i+1] - x[i]) + r*(getcos(i+1) + getcos(i));
b[0] = y[i];
b[1] = r*getsin(i);
b[2] = 3*(y[i+1] - y[i]) - r*(getsin(i+1) + 2*getsin(i));
b[3] = -2*(y[i+1] - y[i]) + r*(getsin(i+1) + getsin(i));
}
double CFivePointView::getcos(int i)
{
double m0;
double n0;
double w1;
double w2;
w1 = m[i-2]*fabs(m[i]*n[i+1] - m[i+1]*n[i]);
w2 = m[i+1]*fabs(m[i-2]*n[i-1] - m[i-1]*n[i-2]);
m0 = m[i-1]*w1 + m[i]*w2;
n0 = n[i-1]*w1 + n[i]*w2;
return(m0/sqrt(m0*m0 +n0*n0));
}
double CFivePointView::getsin(int i)
{
double m0;
double n0;
double w1;
double w2;
w1 = m[i-2]*fabs(m[i]*n[i+1] - m[i+1]*n[i]);
w2 = m[i+1]*fabs(m[i-2]*n[i-1] - m[i-1]*n[i-2]);
m0 = m[i-1]*w1 + m[i]*w2;
n0 = n[i-1]*w1 + n[i]*w2;
return(n0/sqrt(m0*m0 +n0*n0));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -