Realization of computer graphics VIEW.CPP:C computer Graphics view class
//
#include "stdafx.h"
#include "math.h"
Shared_handlers can be used to implement previews, thumbnails, and search filter handles.
The ATL project is defined and allows the document code to be shared with the project.
#ifndef shared_handlers
#include "computer graphics. H"
#endif
#include "Computer Graphics Doc.h"
#include "Computer Graphics View.h"
#ifdef _DEBUG
#define NEW Debug_new
#endif
C Computer Graphics View
Implement_dyncreate (c computer Graphics view, CView)
Begin_message_map (c computer Graphics view, CView)
Standard PRINT Commands
On_command (Id_file_print, &cview::onfileprint)
On_command (Id_file_print_direct, &cview::onfileprint)
On_command (Id_file_print_preview, &cview::onfileprintpreview)
On_command (id_32773, &c computer graphics View::liang)
End_message_map ()
C Computer Graphics view construction/destruction
c Computer Graphics View::c computer Graphics view ()
{
TODO: Add construction code here
}
c Computer Graphics View::~c computer Graphics view ()
{
}
BOOL c Computer Graphics view::P Recreatewindow (createstruct& CS)
{
TODO: here by modifying
CREATESTRUCT CS To modify a window class or style
Return CView::P Recreatewindow (CS);
}
C Computer Graphics View rendering
void c Computer Graphics View::ondraw (cdc*/*pdc*/)
{
C computer graphics doc* PDoc = GetDocument ();
Assert_valid (PDOC);
if (!pdoc)
Return
cdc* Pdc=getdc ();
Pdc->rectangle (10,20,90,80);
int x1=20,y1=10,x2=80,y2=90;
Pdc->moveto (X1,Y1);
Pdc->lineto (X2,Y2);
}
c Computer Graphics View printing
BOOL c Computer Graphics view::onprepareprinting (cprintinfo* pInfo)
{
Default Preparation
return doprepareprinting (PInfo);
}
void c Computer Graphics view::onbeginprinting (cdc*/*pdc*/, cprintinfo*/*pinfo*/)
{
TODO: Add additional pre-print initialization process
}
void c Computer Graphics view::onendprinting (cdc*/*pdc*/, cprintinfo*/*pinfo*/)
{
TODO: Add a clean-up process after printing
}
C Computer Graphics View Diagnostics
#ifdef _DEBUG
void c Computer Graphics View::assertvalid () const
{
Cview::assertvalid ();
}
void C Computer Graphics view::D UMP (cdumpcontext& DC) const
{
CView::D UMP (DC);
}
c Computer graphics doc* C computer graphics view::getdocument () const//non-debug version is inline
{
ASSERT (M_pdocument->iskindof (Runtime_class (c computer graphics doc));
Return (c computer graphics doc*) m_pdocument;
}
#endif//_debug
C Computer Graphics view message processing program
void c Computer Graphics View::liang ()
{
int x1,x2,y1,y2,xl,xr,yb,yt;
xl=10,xr=90,yb=20,yt=80;
x1=20,y1=10,x2=80,y2=90;
int dx=x2-x1,dy=y2-y1;
int p1=-dx,p2=dx,p3=-dy,p4=dy;
int q1=x1-xl,q2=xr-x1,q3=y1-yb,q4=yt-y1;
float U1,u2,u3,u4;
float umax,umin;
int x11,y11,x22,y22;
if (dx==0)
{U3=Q3/P3,U4=Q4/P4;
if (q1<0| | Q2<0)
Exit (0);
if (q1>=0&&q2>=0)
Umax=max (u3,0);
Umin=min (u4,1);
/*if (p3>0)
{
Umin=min (u3,1);
Umax=max (u4,0);
}
if (p3<0)
{
Umax=max (u3,0);
Umin=min (u4,1);
}
if (umax>umin)
Exit (0);
if (umax<=umin)
{
x11=x1+umax* (X2-X1);
y11=y1+umax* (Y2-Y1);
x22=x1+umin* (X2-X1);
y22=x1+umin* (Y2-Y1);
}
*/
}
if (dy==0)
{
U1=Q1/P1;U2=P2/Q2;
if (q3<0| | Q4<0)
Exit (0);
if (q3>=0&&q4>=0)
{
umax= (u3,0);
Umin=min (u4,1);
}
/*if (p1>0)
{
Umin=min (u1,1);
Umax=max (u2,0);
}
if (p1<0)
{
Umax=max (u1,0);
Umin=min (u2,1);
}
if (umax>umin)
Exit (0);
if (umax<=umin)
{
x11=x1+umax* (X2-X1);
y11=y1+umax* (Y2-Y1);
x22=x1+umin* (X2-X1);
y22=x1+umin* (Y2-Y1);
}*/
}
if (dx!=0&&dy!=0)
{
u1= (float) q1/(float) p1,u2= (float) q2/(float) p2,u3= (float) q3/(float) P3, u4= (float) q4/(float) P4;
Float umin1,umax1;
Umin1=min (u2,1);
Umin=min (UMIN1,U4);
Umax1=max (u1,0);
Umax=max (UMAX1,U3);
/*int umin1,umax1;
if (p1>0&&p3>0)
{
Umin1=min (u1,1);
Umin=min (UMIN1,U3);
Umax1=max (u2,0);
Umax=max (UMAX1,U4);
}
if (p1>0&&p3<0)
{
Umin1=min (u1,1);
Umin=min (UMIN1,U4);
Umax1=max (u2,0);
Umax=max (UMAX1,U3);
}
if (p1<0&&p3<0)
{
Umin1=min (u2,1);
Umin=min (UMIN1,U4);
Umax1=max (u3,0);
Umax=max (UMAX1,U1);
}
if (p1<0&&p3>0)
{
Umin1=min (u2,1);
Umin=min (UMIN1,U3);
Umax1=max (u1,0);
Umax=max (UMAX1,U4);
}
if (umax>umin)
exit (0);
if (umax<=umin)
{
x11=x1+umax* (x2-x1);
y11=y1+umax* (Y2-Y1);
x22=x1+umin* (X2-X1);
y22=x1+umin* (y2-y1);
}
*/
}
if (umax>umin)
Exit (0);
if (umax<=umin)
{
X11=X1+UMAX*DX;
Y11=y1+umax*dy;
X22=X1+UMIN*DX;
Y22=y1+umin*dy;
}
cdc* Pdc=getdc (); CPen Penblack;
Penblack.createpen (Ps_solid, 3, RGB (0, 0, 255));
cpen* Poldpen = Pdc->selectobject (&penblack);
Pdc->moveto (X11,Y11);
Pdc->lineto (X22,Y22);
}
Liang Youdong algorithm for clipping segments with polygons