Two line drawing algorithms (Dda&bersenham)

Source: Internet
Author: User

DDA (Digital differential analyzer)

Introduced by the oblique-cut equation of straight line

For a line segment with a positive slope, if the slope is <=1, the unit x interval (δx=1) is sampled and each Y-value is calculated one at a

yk+1 = Yk + M (m is the slope determined by the initial point)

For line segments with slope >1

xk+1 = Xk + 1/m (M is the slope determined by the initial point)

The starting endpoint is on the right when "+"-"-"

#include"stdlib.h"#include"math.h"inlineintRoundConst floatA) {return int(A +0.5);}//Implement arithmetic roundingvoidLineDDA (intX0,intY0,intXEnd,intyend) {    intDX = xend-x0, dy = yend-y0, steps, K; floatXincrement, yincrement, x = (float) x0, y = (float) y0; if(fabs (DX) >fabs (DY)) {Steps=fabs (DX); }    Else{Steps=fabs (DY); }    //Compare Slopexincrement=float(DX)/float(steps); Yincrement=float(DY)/float(steps);    SetPixel (Round (x), round (y));  for(k=0; k<steps;k++) {x+=xincrement; Y+=yincrement;    SetPixel (Round (x), round (y)); }    //Incremental Drawing} 

Bresenham Drawing Line algorithm

The Bresenham algorithm constructs the decision parameters by calculating the distance between the next theoretical point and its neighboring grid, and then uses the decision parameters to carry out the recursive plot points.

Dlower = Y-yk = m (xk+1) +b-yk

Dupper = (yk+1)-y = yk + 1–m (xk+1)-B

Decision parameter P = x (dlower-dupper) = 2 Y*xk-2 X*yk + C

The Bresenham line algorithm for |m|<1 is:

    1. Enter the two endpoints of the segment and store the left endpoint in (x0,y0)
    2. Load (X0,Y0) into the frame cache and draw the first point;
    3. The calculation constants x, have, 2y and 2 y-2 x, and get the first value of the decision parameters:

P0 = 2 Y-X

4. Starting with k = 0, the following tests are performed at each xk of the path along the line segment:

If pk<0, the next point to draw is (Xk+1,yk), and

pk+1 = pk+2 y

Otherwise, the next point to draw is (xk+1,yk+1), and

pk+1 = pk+2 y-2 x

5. Repeat steps 4, x-1 times

#include <stdlib.h>#include<math.h>/*Bresenham algorithm in |m|<1.0 time*/voidLinebres (intX0,intY0,intXEnd,intyend) {    intDX = fabs (xend-x0), dy = fabs (Yend-y0); intp =2* DY-DX; intTwody =2* dy, TWODYMINUSDX =2* (DY-DX); intx, y; /*starting and ending points based on the slope plus or minus*/    if(X0 >xEnd) {x=xEnd; Y=yend; XEnd=x0; }    Else{x=x0; Y=y0;    } setpixel (x, y);  while(X <xEnd) {x++; if(P <0) P+=Twody; Else{y++; p + =TWODYMINUSDX; } setpixel (x, y)}}

Two line drawing algorithms (Dda&bersenham)

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.