Tuesday, October 3, 2017

Bresenham VS DDA

Write a program to input the line coordinates from the user to generate a line using Bresenham’s method and DDA algorithm. Compare the lines for their values on theplotted line.

Code:


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    float x1,x2,y1,y2;
    cout<<"Enter coordinates\n";
    cout<<"x1:";
    cin>>x1;
    cout<<"y1:";
    cin>>y1;
    cout<<"x2:";
    cin>>x2;
    cout<<"y2:";
    cin>>y2;
    int x3=x1+100;
    int x4=x2+100;
    int y3=y1,y4=y2;
    int y=y2-y1;
    int x=x2-x1;
    float m=y/x;
    int d=2*y-x;
    putpixel(x1,y1,10);
    if(m<=1){
    while(x1!=x2){
      if(d>0){
y1++;
d=d+2*(y-x);
      }else{
      d=d+2*y;
      }
      x1++;
      putpixel(x1,y1,10);
    }}else{
      while(y1!=y2){
      if(d>0){
x1++;
d=d+2*(y-x);
      }else{
      d=d+2*y;
      }
      y1++;
      putpixel(x1,y1,10);
    }
    }
    if(m<=1){
    while(x3!=x4){
    x3++;
    y3=y3+m;
    putpixel(x3,y3,10);
    }
    }else{
    while(y3!=y4){
    y3++;
    x3=x3+1/m;
    putpixel(x3,y3,10);
    }

    }
    setcolor(15);
    settextstyle(1,HORIZ_DIR,2);
    outtextxy(x2+15,y2,"Bresenham");
    outtextxy(x2+135,y2,"DDA");
    getch();
}

   Output: