📄 clip_a_lineview.cpp
字号:
// Clip_a_lineView.cpp : implementation of the CClip_a_lineView class
//
#include "stdafx.h"
#include "Clip_a_line.h"
#include "Clip_a_lineDoc.h"
#include "Clip_a_lineView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView
IMPLEMENT_DYNCREATE(CClip_a_lineView, CView)
BEGIN_MESSAGE_MAP(CClip_a_lineView, CView)
//{{AFX_MSG_MAP(CClip_a_lineView)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView construction/destruction
CClip_a_lineView::CClip_a_lineView()
{
// TODO: add construction code here
x1=10;
y1=10;
x2=800;
y2=400;
xw_min=50;
xw_max=300;
yw_min=50;
yw_max=300;
}
CClip_a_lineView::~CClip_a_lineView()
{
}
BOOL CClip_a_lineView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView drawing
void CClip_a_lineView::OnDraw(CDC* pDC)
{
CClip_a_lineDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
clip_a_line(pDC, x1, y1,x2, y2, xw_min,xw_max,yw_min,yw_max);
}
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView printing
BOOL CClip_a_lineView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CClip_a_lineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CClip_a_lineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView diagnostics
#ifdef _DEBUG
void CClip_a_lineView::AssertValid() const
{
CView::AssertValid();
}
void CClip_a_lineView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CClip_a_lineDoc* CClip_a_lineView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClip_a_lineDoc)));
return (CClip_a_lineDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CClip_a_lineView message handlers
void CClip_a_lineView::clip_a_line(CDC *pDC, int x1, int y1, int x2, int y2, int xw_min, int xw_max, int yw_min, int yw_max)
{
int i,code1[4],code2[4],done,display;
float m;
int x11,x22,y11,y22,mark;
done=0;
display=0;
while(done==0)
{
x11=x1;
x22=x2;
y11=y1;
y22=y2;
encode(x1,y1,code1,xw_min,xw_max,yw_min,yw_max);
encode(x2,y2,code2,xw_min,xw_max,yw_min,yw_max);
if(accept(code1,code2))
{
done=1;
display=1;
break;
}
else
if(reject(code1,code2))
{
done=1;
break;
}
mark=swap_if_needed(code1,code2);
if(mark==1)
{
x1=x22;
x2=x11;
y1=y22;
y2=y11;
}
if(x2==x1)
m=999999;
else
m=(float)(y2-y1)/(float)(x2-x1);
if(code1[0])
{
x1+=(yw_min-y1)/m;
y1=yw_min;
}
else if(code1[1])
{
x1-=(y1-yw_max)/m;
y1=yw_max;
}
else if(code1[2])
{
y1-=(x1-xw_min)*m;
x1=xw_min;
}
else if(code1[3])
{
y1+=(xw_max-x1)*m;
x1=xw_max;
}
}
if(display==1)
{
pDC->MoveTo(x1,y1);
pDC->LineTo(x2,y2);
}
pDC->MoveTo(xw_min,yw_max);
pDC->LineTo(xw_max,yw_max);
pDC->MoveTo(xw_min,yw_min);
pDC->LineTo(xw_min,yw_max);
pDC->MoveTo(xw_min,yw_min);
pDC->LineTo(xw_max,yw_min);
pDC->MoveTo(xw_max,yw_min);
pDC->LineTo(xw_max,yw_max);
}
void CClip_a_lineView::encode(int x, int y, int code[], int xw_min, int xw_max, int yw_min, int yw_max)
{
int i;
for(i=0;i<4;i++)
code[i]=0;
if(x<xw_min)
code[2]=1;
else if(x>xw_max)
code[3]=1;
if(y>yw_max)
code[1]=1;
else if(y<yw_min)
code[0]=1;
}
int CClip_a_lineView::accept(int code1[], int code2[])
{
int i,flag;
flag=1;
for(i=0;i<4;i++)
{
if((code1[i]==1)||(code2[i]==1))
{
flag=0;
break;
}
}
return (flag);
}
int CClip_a_lineView::reject(int code1[], int code2[])
{
int i,flag;
flag=0;
for(i=0;i<4;i++)
{
if((code1[i]==1)&&(code2[i]==1))
{
flag=1;
break;
}
}
return (flag);
}
int CClip_a_lineView::swap_if_needed(int code1[], int code2[])
{
int i,flag1,flag2,tmp;
flag1=1;
for(i=0;i<4;i++)
if(code1[i]==1)
{
flag1=0;
break;
}
flag2=1;
for(i=0;i<4;i++)
if(code2[i]==1)
{
flag2=0;
break;
}
if((flag1==0)&&(flag2==0))
return (0);
if((flag1==1)&&(flag2==0))
{
for(i=0;i<4;i++)
{
tmp=code1[i];
code1[i]=code2[i];
code2[i]=tmp;
}
return (1);
}
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -