计算机图形学 运用 中点分割直线段裁剪算法原理

来源:互联网 发布:现在开淘宝店卖什么好 编辑:程序博客网 时间:2024/05/22 00:20
 

// 实验二View.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "实验二.h"

#include "实验二Doc.h"
#include "实验二View.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
 //{{AFX_MSG_MAP(CMyView)
  // 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()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
 // TODO: add construction code here

}

CMyView::~CMyView()
{
}

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs

 return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

int CMyView::Encode(double x,double y,double XL,double XR,double YB,double YT,CMyView *Ecode)
{
 //CMyView *Ecode = new CMyView;
        Ecode->left=0;
     Ecode->right=0;
        Ecode->top=0;
        Ecode->buttom=0;
 if(x<XL)
  Ecode->left=1;
 if(x>XR)
     Ecode->right=1;
 if(y>YT) Ecode->buttom=1;
 if(y<YB)Ecode->top=1;
 return 1;
}
int CMyView::Cut(double x0,double y0,double x1,double y1,double XL,double XR,double YB,double YT)
{
int flag;
    double x,y; 
 x=(x0+x1)/2.0;
 y=(y0+y1)/2.0;
    CMyView *code0=new CMyView;
    CMyView *code1=new CMyView;
   flag= Encode(x0,y0,XL,XR,YB,YT,code0);
    flag=Encode(x1,y1,XL,XR,YB,YT,code1);
 CDC *pDC=GetDC();
 if((code0->top==code1->top)&&(code0->buttom==code1->buttom) &&(code0->left==code1->left) &&(code0->right==code1->right))
 {
  //在矩形外面
  return 0;
 }
 else
 {
  if((code0->top==code0->buttom==code0->left==code0->right==0 )&& (code1->top==code1->buttom==code1->left==code1->right==0 ))
  {
  pDC->MoveTo((int)x0,(int)y0);
  pDC->LineTo((int)x1,(int)y1);
   return 1;
  }
  else
  {
   if(((pow(x,2)+pow(y,2)) - (pow(x0,2) + pow(y0,2)) )< 10e-6)
   {
    return 1;
   }
   else
   {
    Cut(x1,y1,x,y,XL,XR,YB,YT);
    Cut(x,y,x0,y0,XL,XR,YB,YT);    
   }
  }
 }
 return 1;
}

void CMyView::OnDraw(CDC* pDC)
{
 CMyDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 double x0,y0,x1,y1;
 double XL,XR,YB,YT;
x0=0;y0=0;x1=150;y1=200;
XL=20;XR=200;YB=150;YT=100;
pDC->MoveTo((int)x0+50,(int)y0);
pDC->LineTo((int)x1+50,(int)y1);
//Sleep(3000);  //为了显示效果,停顿3秒
int flag=Cut(x0,y0,x1,y1,XL,XR,YB,YT);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView printing

BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
 return DoPreparePrinting(pInfo);
}

void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add extra initialization before printing
}

void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

#ifdef _DEBUG
void CMyView::AssertValid() const
{
 CView::AssertValid();
}

void CMyView::Dump(CDumpContext& dc) const
{
 CView::Dump(dc);
}

CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
 return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

 

原创粉丝点击