Reverse sample of color line segments in computer graphics (5)

Source: Internet
Author: User

Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/40083207


This article uses a complete example to demonstrate the reverse walk of a color straight line segment.

1) Create CP2 class

Header file: p2.h

// P2.h: interface for the CP2 class. //////////////////////////////////////// /// // If! Defined (partition _) # define partition _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 # include "RGB. H "// two-dimensional vertex class CP2 {public: CP2 (); CP2 (Double X, Double Y, crgb C); Virtual ~ CP2 (); Public: // convenient access, directly defined as a total of Double X; Double Y; crgb C ;};# endif //! Defined (afx_p2_h1_dd23884f_7f62_48e8_a906_65c4558de4eb1_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(double x,double y, CRGB c){this->x = x;this->y = y;this->c = c;}CP2::~CP2(){}
2) create a crgb class

Header file: RGB. h

// RGB. h: interface for the crgb class. //////////////////////////////////////// /// // If! Defined (empty _) # define empty _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 class crgb {public: crgb (); crgb (Double R, double G, double B); Virtual ~ Crgb (); friend crgb operator + (const crgb &, const crgb &); // operator overload friend crgb operator-(const crgb &, const crgb &); friend crgb operator * (const crgb &, const crgb &); friend crgb operator * (const crgb &, double); friend crgb operator * (double, const crgb &); friend crgb operator/(const crgb &, double); Public: public: Double red; // red weight double green; // green weight Double blue; // blue weight }; # endif //! Defined (afx_rgb_hda-f163ce21_a4e9_4b66_9011_3b63d6e588d200000000ded _)
Implementation file: RGB. cpp

// RGB. CPP: Implementation of the crgb class. //////////////////////////////////////// //// // # include "stdafx. H "# include" wuline. H "# include" RGB. H "# ifdef _ debug # UNDEF this_filestatic char this_file [] =__ file __; # define new debug_new # endif ////////////////////////////////// /// // construction/ destruction /////////////////////////////////////// ///// //// // Crgb: crgb () {Red = 1.0; green = 1.0; Blue = 1.0;} crgb ::~ Crgb () {} crgb: crgb (Double R, double G, double B) // reload constructor {Red = r; Green = g; Blue = B ;} crgb operator + (const crgb & C1, const crgb & C2) // + operator overload {crgb C; C. red = c1.red + c2.red; C. green = c1.green + c2.green; C. blue = c1.blue + c2.blue; return C;} crgb operator-(const crgb & C1, const crgb & C2) //-operator overload {crgb C; C. red = c1.red-c2.red; C. green = c1.green-c2.green; C. blue = c1.blue-c2.blue; return C;} crgb operator * (const crgb & C1, const crgb & C2) // * operator overload {crgb C; C. red = c1.red * c2.red; C. green = c1.green * c2.green; C. blue = c1.blue * c2.blue; return C;} crgb operator * (const crgb & C1, Double K) // * operator overload {crgb C; C. red = K * c1.red; C. green = K * c1.green; C. blue = K * c1.blue; return C;} crgb operator * (Double K, const crgb & C1) // * operator overload {crgb C; C. red = K * c1.red; C. green = K * c1.green; C. blue = K * c1.blue; return C;} crgb operator/(const crgb & C1, Double K) // operator overload {crgb C; C. red = c1.red/K; C. green = c1.green/K; C. blue = c1.blue/K; return C;} crgb operator + = (crgb & C1, crgb & C2) // + = Operator overload {c1.red = c1.red + c2.red; c1.green = c1.green + c2.green; c1.blue = c1.blue + c2.blue; return C1;} crgb operator-= (crgb & C1, crgb & C2) //-= Operator overload {c1.red = c1.red-c2.red; c1.green = c1.green-c2.green; c1.blue = c1.blue-c2.blue; return C1;} crgb operator * = (crgb & C1, crgb & C2) // * = Operator overload {c1.red = c1.red * c2.red; c1.green = c1.green * c2.green; c1.blue = c1.blue * c2.blue; return C1 ;} crgb operator/= (crgb & C1, Double K) // = Operator overload {c1.red = c1.red/K; c1.green = c1.green/K; c1.blue = c1.blue/K; return C1 ;}
3) create a cwuanti class
Header file: wuanti. h

// Wuanti. h: interface for the cwuanti class. //////////////////////////////////////// /// // If! Defined (partition _) # define partition _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 # include "p2.h" class cwuanti {public: cwuanti (); virtual ~ Cwuanti (); void moveTo (Double X, Double Y, crgb C); // move to the specified position and specify the starting color void moveTo (CP2 P0); void lineto (CDC * PDC, double X, Double Y, crgb C); // draw a Wu anti-sample line without the end point. Specify the end color void lineto (CDC * PDC, CP2 P1); Private: CP2 P0; // start point CP2 P1; // end point}; # endif //! Defined (afx_wuanti1_hda-2c2d354f_c8aa_4f64_81cc_56195dee5704366included _)
Implementation file: wuanti. cpp

// Wuanti. CPP: Implementation of the cwuanti class. //////////////////////////////////////// //// // # include "stdafx. H "# include" wuline. H "# include" wuanti. H "# include" RGB. H "# include <math. h> # define round (d) int (floor (D + 0.5) // rounds the 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 (Double X, Double Y, crgb c) // draw a colored line and specify the starting color {symbol x = x; symbol Y = y; export C = C;} void cwuanti: moveTo (CP2 P0) // draw a colored line {p0 = P0;} void cwuanti: lineto (CDC * PDC, double X, double Y, crgb c) // draw, specify the end color {lineto (PDC, CP2 (X, Y, C);} void cwuanti: lineto (CDC * PDC, CP2 P1) {p1 = p1; CP2 P, T; crgb C0, C1; // colorref CF, CB = PDC-> getbkcolor (); If (FABS (p0.x-p1.x) = 0) // draw a vertical line {If (then Y> p1.y) // swap the vertex so that the starting point is lower than the destination vertex {T = P0; p0 = p1; P1 = T ;} for (P = P0; p. Y <p1.y; p. Y ++) {// linear interpolation: c = (1-T) C0 + TC1, t = (x-x0)/(x1-x0) or t = (y-y0)/(y1-y0) // ------> C = C0 + T (c1-c0) p. C = Objective C + (p1.c-Objective C) * (p. y-P0.y)/(p1.y-venture y); // reload RGB, '+ ''-''' *''/' operator PDC-> setpixelv (round (P. x), round (P. y), RGB (P. c. red, P. c. green, P. c. blue) ;}} else {Double K, E = 0; k = (p1.y-y)/(p1.x-p0.x); If (k> 1.0) // plot K> 1 (Y in the main direction) {If (p0.y> p1.y) {T = P0; p0 = p1; P1 = T ;}for (P = P0; p. Y <p1.y; p. Y ++) {// pixel brightness level C0 = crgb (E, E, E); C1 = crgb (1.0-e, 1.0-e, 1.0-e); // linear interpolation: C = (1-T) C0 + TC1, t = (x-x0)/(x1-x0) or t = (y-y0)/(y1-y0) p. C = Objective C + (p1.c-Objective C) * (p. y-P0.y)/(p1.y-p0.y); PDC-> setpixelv (round (P. x), round (P. y), RGB (c0.red * P. c. red, c0.green * P. c. green, c0.blue * P. c. blue); PDC-> setpixelv (round (P. X + 1), round (P. y), RGB (c1.red * P. c. red, c1.green * P. c. green, c1.blue * P. c. blue); e + = (1.0/K); If (E> = 1.0) {P. X ++; e -- ;}}}if (0.0 <= K & K <= 1.0) // draw 0 = <k = <1 (X in the main direction) {If (distinct x> p1.x) {T = P0; p0 = p1; P1 = T;} For (P = P0; p. x <p1.x; p. X ++) {C0 = crgb (E, E, E); C1 = crgb (1.0-e, 1.0-e, 1.0-e); p. C = Objective C + (p1.c-Objective C) * (p. x-P0.x)/(p1.x-Snapshot X); PDC-> setpixelv (round (P. x), round (P. y), RGB (c0.red * P. c. red, c0.green * P. c. green, c0.blue * P. c. blue); PDC-> setpixelv (round (P. x), round (P. Y + 1), RGB (c1.red * P. c. red, c1.green * P. c. green, c1.blue * P. c. blue); E = e + k; If (E >= 1.0) {P. Y ++; e -- ;}}}if (K >=- 1.0 & K <0.0) // draw-1 = <k <0 (X as the main direction) {If (distinct x> p1.x) {T = P0; p0 = p1; P1 = T;} For (P = P0; p. x <p1.x; p. X ++) {C0 = crgb (E, E, E); C1 = crgb (1.0-e, 1.0-e, 1.0-e); p. C = Objective C + (p1.c-Objective C) * (p. x-P0.x)/(p1.x-Snapshot X); PDC-> setpixelv (round (P. x), round (P. y), RGB (c0.red * P. c. red, c0.green * P. c. green, c0.blue * P. c. blue); PDC-> setpixelv (round (P. x), round (P. y-1), RGB (c1.red * P. c. red, c1.green * P. c. green, c1.blue * P. c. blue); E = e-K; If (E >= 1.0) {P. Y --; e -- ;}}if (k <-1.0) // plot k <-1 (y) {If (p0.y <p1.y) {T = P0; p0 = p1; P1 = T;} For (P = P0; p. y> p1.y; p. Y --) {C0 = crgb (E, E, E); C1 = crgb (1.0-e, 1.0-e, 1.0-e); p. C = Objective C + (p1.c-Objective C) * (p. y-P0.y)/(p1.y-p0.y); PDC-> setpixelv (round (P. x), round (P. y), RGB (c0.red * P. c. red, c0.green * P. c. green, c0.blue * P. c. blue); PDC-> setpixelv (round (P. X + 1), round (P. y), RGB (c1.red * P. c. red, c1.green * P. c. green, c1.blue * P. c. blue); E = E-1/K; If (E> = 1.0) {P. X ++; e -- ;}}}p0 = p1 ;}
4) ondraw Function

Void cwulineview: ondraw (CDC * PDC) {cwulinedoc * pdoc = getdocument (); assert_valid (pdoc); // set the drawing coordinate system: the origin is the view region center, the X axis is horizontal to the right, and the Y axis is vertical to crect rect; getclientrect (& rect); // obtain the size of the rectangle in the customer zone PDC-> setmapmode (mm_anisotropic ); // customize the coordinate system PDC-> setjavaswext (rect. width (), rect. height (); // set the size of the window PDC-> setviewportext (rect. width (),-rect. height (); // set the ratio of the video area, and the X axis to the right, the Y axis to the upward PDC-> setviewportorg (rect. width ()/2, rect. height ()/2); // set the customer Zone center as the coordinate system origin rect. offsetrect (-rect. width ()/2,-rect. height ()/2); // The rectangle overlaps with the customer area cwuanti * line = new cwuanti; // dynamically creates a line-drawn class object line-> moveTo (100,-200, crgb (100,200, 0); line-> lineto (PDC, 0,255, crgb (, 0 )); // 0 <= k <= 1 line-> moveTo (-206,-137, crgb (214,175, 0); line-> lineto (PDC, crgb (0,255, 0); // K> 1line-> moveTo (-201,-253, crgb (201,266, 0, 0); line-> lineto (PDC, crgb (0,255, 0); // K <-1line-> moveTo (-186,293, crgb (201, 0, 0); line-> lineto (PDC, -236, crgb (0,255, 0); //-1 <k <0line-> moveTo (-286,193, crgb (, 0, 0 )); line-> lineto (PDC, 221,-246, crgb (0,255, 0 ));}
5) Running Effect (25 times larger)







Address: http://blog.csdn.net/qingdujun/article/details/40083207

References: tutorial on basic computer graphics (Visual C ++) (2nd) by Kong lingde

Baidu Library, life contribution, Wu anti-sample linear algorithm: http://wenku.baidu.com/link? Url = Response


Reverse sample of color line segments in computer graphics (5)

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.