Graphics--(Midpoint drawing line method +bresenham drawing line algorithm)

Source: Internet
Author: User

Programming Environment: Codeblocks+ege Library

Function used: putpixel (int x1,int y1,int color) brightens a coordinate point with a color.

Both of these algorithms are used to draw a straight line on the computer, then why we do not directly with the linear equation with a point and then lit it, this is because every coordinate point in the computer is an integer, and the line is a combination of pixel points, then directly will sit punctuation rounding integer It is good ah, indeed, This is a method, but the rounding of floating-point numbers in a computer can make the efficiency of the operation worse, so the two methods are used when the line is actually drawn.

1. Midpoint Drawing Line Method

Consider only when the slope of the line |k|<1, assuming there is now a line (X1,Y1,X2,Y2), then the first point must be (x1,y1), the next point of the x-coordinate is x1+1,y coordinates either Y1 or y1+. The key is that each time a point is taken, is to take the previous one of the Y1, or y1+1, then it must be taken straight to the nearest point, and judge which point to use the midpoint, we will be the midpoint into the line d=f (x1+1,y1+0.5) =a* (x1+1) +b* (y1+0.5) +c.

(1) If the line d>=0, then the point to remove the edge is also (X1+1,Y1). (2) If the line d<0, then take the upper point is also (x1+1,y1+1).

Its actual process is so each time according to the front point to determine where the next point, and then to light, but so every time you have to judge the linear equation to calculate too much trouble, we will take these two cases into the linear equation can be found in the law:

(1) When the line >=0, through the dissolution of D1=d+a;

(2) When the line <0, through the dissolution of D2=d+a+b;

(3) initial value d0=a+0.5b.

In other words, each increment is either a or a+b, so it's a lot easier to judge, because every time we just judge its positive or negative. So the equation is multiplied by 2, the floating point number 0.5 into an integer, so the hardware operation is undoubtedly faster.

Code:

1#include <iostream>2#include <graphics.h>3 using namespacestd;4 //Midpoint Drawing Line Method5 voidLine1 (intX1,intY1,intX2,inty2) {6 7      intx,y,d0,d1,d2,a,b;8y=Y1; 9A=y1-y2;//the algorithm of a in linear equationTenb=x2-x1;//The algorithm of B in linear equation Oned0=2*a+b;//Increment Initial value Ad1=2*a;//Increment when >=0 -D2=2* (A+B);//Increment when <0 -       for(x=x1;x<=x2;x++){ thePutpixel (X,y,green);//Light -         if(d0<0){ -y++;  -d0+=D2; +}Else{ -  +d0+=D1; A         } at  -      } - } - //Bresenham drawing Line Algorithm - voidLine2 (intX1,intY1,intX2,inty2) { -  in    intx,y,dx,dy,d; -y=Y1;  todx=x2-X1;  +dy=y2-Y1;  -D=2*DY-DX;//the initial value of Delta D the     for(x=x1;x<=x2;x++){ *Putpixel (X,y,green);//Light $     if(d<0){Panax Notoginsengd+=2*dy; -}Else{ they++; +d+=2*dy-2*DX; A     } the  +  -  $    } $  - } - intMain () the { -Initgraph (640,480);//Open Ege InitializationWuyiLine1 ( $, the, -, -);//Draw Line theGetch ();//Wait for user action -Closegraph ();//Turn off Graphics Wu     return 0; -}

2, Bresenham drawing line algorithm

The idea of this line algorithm and midpoint line of the same, only in judging which point, not to see it in the midpoint of the top or the bottom, but the two points to the line of the point of the distance subtraction, judge its positive or negative, if the bottom of the point to a straight line of the actual point distance d1-d2>=0 then take the top point y1+1 , the same is also the way to resolve the line to draw the following conclusions:

(1) When d1-d2<0, D=d+2*dy.

(2) when d1-d2>=0, D=d+2*dy-2*dx.

(3) The initial value of D is d=2*dy-dx.

Its code is as shown above. Run as follows:

Graphics--(Midpoint drawing line method +bresenham drawing line algorithm)

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.