Author: Ching
Original address: http://blog.csdn.net/qingdujun/article/details/40822977
In this paper, a complete example is presented to illustrate the Cohen-sutherland line segment clipping algorithm.
1) Create Class CP2
Header file: P2.h
P2.h:interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if! Defined (afx_p2_h__23095c49_598d_4e8d_b510_87490878e0f6__included_) #define Afx_p2_h__23095c49_598d_4e8d_b510_ 87490878e0f6__included_#if _msc_ver > 1000#pragma once#endif//_msc_ver > 1000class CP2 {public:cp2 (); Virtual ~CP2 ();p ublic:int x;int y;int RC;}; #endif//!defined (AFX_P2_H__23095C49_598D_4E8D_B510_87490878E0F6__INCLUDED_)
implementation file: P2.cpp
P2.cpp:implementation of the CP2 class.////////////////////////////////////////////////////////////////////////# Include "stdafx.h" #include "cohen_sutherland.h" #include "P2.h" #ifdef _debug#undef this_filestatic Char this_file[]=__ file__, #define NEW debug_new#endif//////////////////////////////////////////////////////////////////////// CONSTRUCTION/DESTRUCTION//////////////////////////////////////////////////////////////////////CP2::CP2 () {x = 0;y = 0;RC = 0;} Cp2::~cp2 () {}
2) Engineering class Ccohen_sutherlandview
Header file: cohen_sutherlandview.h
Cohen_SutherlandView.cpp:implementation of the Ccohen_sutherlandview class//#include "stdafx.h" #include "cohen_ Sutherland.h "#include" cohen_sutherlanddoc.h "#include" cohen_sutherlandview.h "#ifdef _debug#define new debug_new# undef this_filestatic char this_file[] = __file__; #endif # define left 1//0001#define right 2//0010#define BOTTOM 4//0100#define TOP 8//1000///////////////////////////////////////////////////////////////////////////////CCoh En_sutherlandviewimplement_dyncreate (Ccohen_sutherlandview, CView) begin_message_map (Ccohen_sutherlandview, CView )//{{afx_msg_map (Ccohen_sutherlandview) On_wm_lbuttondown () On_wm_mousemove () On_wm_lbuttonup ()//}}AFX_MSG_MAPEND _message_map ()///////////////////////////////////////////////////////////////////////////////CCohen_ Sutherlandview Construction/destructionccohen_sutherlandview::ccohen_sutherlandview () {//Todo:add construction Code hereflag = 0; XL = XR = YT = YB = 0;p0.x = P0.y = 50;p1.x = P1.y = 300;} Ccohen_sutherlanDview::~ccohen_sutherlandview () {}bool Ccohen_sutherlandview::P Recreatewindow (createstruct& cs) {//Todo:modify The Window class or styles here by modifying//the createstruct csreturn CView::P Recreatewindow (CS);} Ccohen_sutherlandview Drawingvoid Ccohen_sutherlandview::ondraw (cdc* pDC) {ccohen_sutherlanddoc* PDoc = GetDocument (); Assert_valid (PDOC);//Todo:add Draw code for native data HEREif (1 = = flag) {Pdc->rectangle (CRect (XL,YT,XR,YB));//Draw Cut Window}pdc->moveto (p0.x,p0.y);pD C->lineto (P1.X,P1.Y);} Ccohen_sutherlandview Diagnostics#ifdef _debugvoid ccohen_sutherlandview::assertvalid () Const{cview::assertvalid ();} void Ccohen_sutherlandview::D UMP (cdumpcontext& DC) Const{cview::D UMP (DC);} ccohen_sutherlanddoc* ccohen_sutherlandview::getdocument ()//Non-debug version is Inline{assert (m_pDocument-> IsKindOf (Runtime_class(Ccohen_sutherlanddoc))); Return (ccohen_sutherlanddoc*) m_pdocument;} #endif//_debug///////////////////////////////////////////////////////////////////////////////CCohen_ Sutherlandview message handlersvoid ccohen_sutherlandview::onlbuttondown (UINT nflags, CPoint point) {//Todo:add your Me Ssage handler code here and/or call Defaultflag = 1; XL = Point.x; YT = Point.y; Cview::onlbuttondown (nflags, point);} void Ccohen_sutherlandview::onmousemove (UINT nflags, CPoint point) {//Todo:add your message handler code here and/or CAL L DEFAULTIF (flag = = 1) {XR = Point.x; YB = Point.y;invalidate ();} Cview::onmousemove (nflags, point);} void Ccohen_sutherlandview::onlbuttonup (UINT nflags, CPoint point) {//Todo:add your message handler code here and/or CAL L Defaultflag = 0; Linecut (); XL = XR = YT = YB = 0;invalidate (); Cview::onlbuttonup (nflags, point);} Endpoint encoding void Ccohen_sutherlandview::encode (CP2 &pt) {pt.rc = 0;if (Pt.x < XL) {pt.rc |= left; 0001=1}else if (Pt.x > XR) {pt.rc |= right;//0010=2}if (Pt.y > YB) {pt.rc |= bottom;//0100=4}else if (Pt.y < YT) {pt.rc |= TOP; 1000=8}}//clipping line-conhen-sutherland algorithm void Ccohen_sutherlandview::linecut () {int x, y; CP2 pt0 = p0, pt1 = P1;while (1) {//Endpoint code encode (p0); EnCode (p1);//All inside the window "simply take" if (0 = = (p0.rc | p1.rc)) {return;} All outside the form "simply discard" else if (0! = (p0.rc & p1.rc)) {p0 = PT0;P1 = Pt1;return;} Else{if (0 = = p0.rc)//If the p0 point is within the window, swap p0 and P1, ensure that the p0 point is outside the window {CP2 pt = p0;p0 = P1;p1 = pt;} Crop if (p0.rc & left)//p0 points on the left, right, bottom, and top of the window {x = Xl;y = (p1.y-p0.y)/(p1.x-p0.x) * (x-p0.x) +p0.y;} The Else if (p0.rc & right)//p0 Point is located on the left side of the window {x = Xr;y = (p1.y-p0.y)/(p1.x-p0.x) * (x-p0.x) +p0.y;} The Else if (p0.rc & BOTTOM)//p0 Point is located on the lower side of the window {y = Yb;x = (Y-P0.Y)/((P1.Y-P0.Y)/(p1.x-p0.x)) +p0.x;} The Else if (p0.rc & TOP)//p0 Point is located on the top side of the window {y = Yt;x = (Y-P0.Y)/((P1.Y-P0.Y)/(p1.x-p0.x)) +p0.x;} p0.x = X;p0.y = y;}}}
3) Run:
Source code: http://download.csdn.net/detail/u012339743/8123725
Original address: http://blog.csdn.net/qingdujun/article/details/40822977
Reference: Basic Computer Graphics tutorial (Visual C + + Edition) (2nd edition) Konglingde
Cutting algorithm of Cohen-sutherland line segment in computer graphics