ASSIEMENT-1
NAME:-Abdela Aman
RollNo:-16071059
//1. Program: To draw a line using Direct approach, DDA algorithm
and Bresenham's algorithm.
#include<iostream>
#include<process.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int round1(float x)
return (int)(x+0.5);
int temp;
temp=*a;
*a=*b;
*b=temp;
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
if(x1>x2)
swap(&x1,&x2);
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);
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;
x=x1;
y=y1;
for ( i=1;i<=step;i++)
x=x+xinc;
y=y+yinc;
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()
char c;
errorcode=graphresult();
if(errorcode!=grOk)
Cout<< grapherrormsg(errorcode);
getch();
exit(0);
do
cin>>&k;
5|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017
cin>>&x1;
cout<<"y1= ";
cin>>&y1);
cin>>&x2;
cout<<"y2= ";
cin>>&y2;
switch(k)
case 1:
break;
case 2:
break;
case 3:
break;
default:
6|Page
PUNJABI UNIVERSITY, COMPUTER SCIENCE DEPARTEMENT , 2017
getch();
clear device();
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