Anda di halaman 1dari 7

PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

ASSIEMENT-1
NAME:-Abdela Aman
RollNo:-16071059
//1. Program: To draw a line using Direct approach, DDA algorithm
and Bresenham's algorithm.

#include<iostream>

Using namespace std;

#include<process.h>

#include<graphics.h>

#include<math.h>

#include<stdlib.h>

int round1(float x)

return (int)(x+0.5);

void swap(int *a, int *b)

int temp;

temp=*a;

*a=*b;

*b=temp;

void direct_approach(int x1, int y1, int x2, int y2)

float m, b,x,y;

if(x1!=x2)

m=(y2-y1)/(x2-x1);
b=y1-m*x1;

1|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

put_pixel(x1, y1, RED);

if(x1>x2)

swap(&x1,&x2);

for( x=x1; x<=x2;x++)

y = m*x+b;

put_pixel(round1(x),round1(y),BLUE);

else

if(y1>y2)

swap(&y1,&y2);

for(y=y1;y<=y2;y++)

Put_pixel(x1,y,BLUE);

void dda(int x1, int y1, int x2, int y2)

int dx, dy, step, xinc, yinc,x,y,i;

dx=x2-x1;

dy=y2-y1;

if(dx==0&&dy==0)

Put_pixel(x1,y1,RED);

else

2|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

if(abs(dx)>=abs(dy))

step=abs(dx);

else

step=abs(dy);

xinc=dx/step;

yinc=dy/step;

put_pixel(x1, y1, BLUE);

x=x1;

y=y1;

for ( i=1;i<=step;i++)

x=x+xinc;

y=y+yinc;

put_pixel(round1(x), round1(y), BLUE);

void bresenham(int x1, int y1, int x2, int y2)

int dx, dy, p,x, y, final;

dx=abs(x2-x1);

dy=abs(y2-y1);

if (dx==0)

if(y1>y2)

swap(&y1,&y2);

3|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

for(y=y1;y<=y2;y++)

Put_pixel(x1, y, BLUE);

else

p=2*dy-dx;

if(x1>x2)

x=x2;

y=y2;

final=x1;

else

x=x1;

y=y1;

final=x2;

Put_pixel(x,y,BLUE);

while(x<=final)

if(p<0)

p=p+2*dy;

else

4|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

p=p+2*(dy-dx);

y=y+1;

x=x+1;

put_pixel(x,y, RED);

int main()

int x1, y1, x2, y2,i,k;

char c;

int gdriver=DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "C:\Users\pc\Desktop\nmok\");

errorcode=graphresult();

if(errorcode!=grOk)

Cout<< grapherrormsg(errorcode);

getch();

exit(0);

do

Cout<<"choose your choice:\n;

Cout<<1. Direct Approach\n";

cout<<"2. Digital differential Analyser\n");

cout<<"3. Bresenham Line Algorithm\n" ;

cin>>&k;

cout<<"Give the starting point of line: \t x1= ";

5|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

cin>>&x1;

cout<<"y1= ";

cin>>&y1);

cout<<"Give the end point\t x2= ";

cin>>&x2;

cout<<"y2= ";

cin>>&y2;

switch(k)

case 1:

Cout<<Line drawn using direct approach\n";

direct_approach(x1, y1, x2, y2);

break;

case 2:

cout<<"Line drawn through DDA algorithm\n";

dda(x1, y1, x2, y2);

break;

case 3:

cout<<"Line drawn through Bresenham algorithm\n";

bresenham(x1, y1, x2, y2);

break;

default:

6|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017

cout<<"Wrong choice entered\n";

getch();

clear device();

cout<< Do you want to continue\n ;

cout<<"If yes press 'y' or 'Y' otherwise press any other key\n";

cin>>&k;

cin>>&c;

while(c=='y' || c=='Y');

getch();

closegraph();

return 0;

7|Page

Anda mungkin juga menyukai