Computer graphics linear anti-aliasing Wu algorithm (4)

Source: Internet
Author: User

Ching

Original address: http://blog.csdn.net/qingdujun/article/details/40048285


In this paper, a complete example is presented to illustrate the linear anti-aliasing Wu algorithm.

1) Create CP2 class

Header file: P2.h

P2.h:interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if! Defined (afx_p2_h__dd23884f_7f62_48e8_a906_65c4558de4eb__included_) #define Afx_p2_h__dd23884f_7f62_48e8_a906_ 65c4558de4eb__included_#if _msc_ver > 1000#pragma once#endif//_msc_ver > 1000//Two-dimensional point class  CP2    {  Public:      CP2 ();      CP2 (double x,double y);      Virtual ~cp2 ();  Public:         //easy access, directly defined as a total of      double X;      Double y;  };    #endif//!defined (AFX_P2_H__DD23884F_7F62_48E8_A906_65C4558DE4EB__INCLUDED_)
implementation file: P2.cpp

P2.cpp:implementation of the CP2 class.////////////////////////////////////////////////////////////////////////# Include "stdafx.h" #include "WuLine.h" #include "P2.h" #ifdef _debug#undef this_filestatic Char this_file[]=__file__;# Define New debug_new#endif////////////////////////////////////////////////////////////////////////Construction/ Destruction//////////////////////////////////////////////////////////////////////cp2::cp2 () {}CP2::~CP2 () {}
2) Create Cwuanti class

Header file: WuAnti.h

Wuanti.h:interface for the Cwuanti class./////////////////////////////////////////////////////////////////////// /#if!defined (afx_wuanti1_h__2c2d354f_c8aa_4f64_81cc_56195dee5704__included_) #define Afx_wuanti1_h__2c2d354f_ c8aa_4f64_81cc_56195dee5704__included_#if _msc_ver > 1000#pragma once#endif//_msc_ver > 1000#include "P2.h" Class Cwuanti  {Public:cwuanti (); virtual ~cwuanti (); void MoveTo (CP2 p0)              ; Moves to the specified position      void MoveTo (Double x, double y);      void LineTo (CP2 p1, CDC *PDC);    Plots Wu Anti-aliasing line with no end point      void LineTo (Double x, double y, CDC *PDC), private:      CP2 P0;      Beginning      CP2 P1;      end}; #endif//!defined (AFX_WUANTI1_H__2C2D354F_C8AA_4F64_81CC_56195DEE5704__INCLUDED_)
implementation file: WuAnti.cpp

WuAnti.cpp:implementation of the Cwuanti class.///////////////////////////////////////////////////////////////// #include "stdafx.h" #include "WuLine.h" #include "WuAnti.h" #include <math.h> #define ROUND (d) Int (Floor (d+ 0.5)//Rounding macro definition #ifdef _debug#undef this_filestatic char this_file[]=__file__; #define NEW debug_new#endif///////////// construction/destruction/////////////////////////// Cwuanti::cwuanti () {}cwuanti::~cwuanti () {}void Cwuanti::moveto (CP2 p0)  Record line start function {p0=p0;      } void Cwuanti::moveto (Double x, double y) {p0.x = x;  P0.y = y;      } void Cwuanti::lineto (Double x, double y, CDC *PDC)//Draw {CP2 p;      p.x = x;        P.y = y;  LineTo (P, PDC);      } void Cwuanti::lineto (CP2 p1, CDC *pdc) {p1=p1;  CP2 p,t;    Double k,e;  The intersection of P (U) and P (d) is f (i), E is f (i) to P (U) and the distance colorref clr = RGB (0,0,0); Black Pixel point if (fabs (p0.x-p1.x) <1e-6)    Draw the vertical line (no antialiasing required) {if (P0.Y&GT;P1.Y)//swap vertices so that the starting point is below the endpoint {t=p0; P0=P1;          p1=t;              } for (p=p0;p.y<p1.y;p.y++)//execute draw, fill pixel dot {pdc->setpixelv (Round (p.x), Round (P.Y), CLR); }} else {k= (P1.Y-P0.Y)/(p1.x-p0.x);//slope if (p0.x>p1.x) {t=p0; P0=P1;  p1=t; } if (k>=0)//Draw 13 Quadrant line {for (p=p0,e=0;p.x<p1.x;p.x++) {Pdc->setpixelv                (Round (p.x), Round (P.Y), RGB (e*255,e*255,e*255));            Pdc->setpixelv (Round (p.x), Round (p.y+1), RGB ((1-E) *255, (1-E) *255, (1-E) *255)); E+=k;if (e>=1.0) {p.y++;e--;} }} if (k<0)//Draw 24 Quadrant line {for (p=p0,e=0;p.x<p1.x;p.x++) {Pdc-&gt ;                Setpixelv (Round (p.x), Round (P.Y), RGB (e*255,e*255,e*255));            Pdc->setpixelv (Round (p.x), Round (p.y+1), RGB ((1-E) *255, (1-E) *255, (1-E) *255)); E+=k;if (e<=-1.0) {p.y--;e--;}       }   }} P0=P1;   Assign the end point to the start}
3) OnDraw function

void Cwulineview::ondraw (cdc* pDC) {cwulinedoc* PDoc = GetDocument (); Assert_valid (PDOC);//Todo:add Draw code for native data Herecwuanti *line=new cwuanti;//dynamically creates a line drawing class object      line-> MoveTo (100,100);      Line->lineto (500,500, PDC);  }
4) Operation effect (magnification 25 times times)


Original address: http://blog.csdn.net/qingdujun/article/details/40048285

Reference: Basic Computer Graphics tutorial (Visual C + + Edition) (2nd edition) Konglingde

Computer graphics linear anti-aliasing Wu algorithm (4)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.