Practical No-1 CG
Practical No-1 CG
Aim: -To draw a line using Simple DDA Algorithm for positive line slope.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm; /* detect the graphics drivers automatically*/
int x1, y1,x2,y2;
initgraph(&gd, &gm,"c:\\TC\\BGI");
cleardevice();
printf("DDA Line Generation Algorithm\n");
printf("\nenter tyhe starting co-ordinates for drawing line");
scanf("%d %d",& x1, & y1);
printf("\nenter the ending co-ordinates");
scanf("%d %d",& x2,& y2);
dda(x1,y1,x2,y2);
printf("\n Thank you");
getch();
closegraph();
}
void dda(int x1, int y1, int x2, int y2)
{
int i,dx,dy,steps;
float x,y;
float xinc, yinc;
dx=(x2-y1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
{
steps= dx;
www.ahirlabs.com
}
else
{
steps = dy;
}
xinc = (float) dx/steps;
yinc = (float) dy/steps;
x = x1;
y= y1;
putpixel(x,y,WHITE);
for(i=1;i<steps;i++)
{
x = x+xinc;
y = y+yinc;
x1 = x+0.5;
y1 = y+0.5;
putpixel(x1,y1,WHITE);
Output:-
www.ahirlabs.com
Practical No – 1(b)
Aim: - To draw a line using Symmetrical DDA Algorithm for positive line
slope.
#include<iostream.h>
#include<conio.h>
#include<graphics>
#include<dos.h>
#include<math.h>
#define ROUND (a) ((int) (a+0.5))
void symDDA (int xa,int ya, int xb,int yb)
{
int dx=xb-xa,dy=yb-ya;float length;
float xinc,yinc,x=xa,y=ya;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);
float n=log10(length)/log10(2);
xinc=dx/(pow(2,n));
yinc=dy/(pow(2,n));
putpixel(ROUND(x),ROUND(y),15);
delay(50);
for(int i=0;i<length;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(ROUND(x),ROUND(y),15);
delay(50);
}
}
www.ahirlabs.com
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
Int xa, xb,ya,yb;
cout<<"enter the points";
cin>>xa>>xb>>ya>>yb;
cleardevice();
symDDA(xa,xb,ya,yb);
getch();
closegraph();
}
Output:-
www.ahirlabs.com
Practical No – 1(c)
Aim: - To draw a line using Bresenham’s Algorithm for positive line slope.
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>
void main(){
int x1,x2,y1,y2,i,e,x,y,dx,dy;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode," ");
cout<<"Enter co-ordinates of point 1:";
cin>>x1>>y1;
cout<<"Enter co-ordinates of point 2:";
cin>>x2>>y2;
dx = abs(x2-x1);
dy = abs(y2-y1);
x=x1;
y=y1;
e = 2*dy-dx;
i=1;
do{
putpixel(x,y,WHITE);
while(e>=0){
y++;
e = e - 2*dx;
putpixel(x,y,WHITE);
}
x++;
e = e + 2*dy;
i++;
www.ahirlabs.com
}while(i<=dx);
getch();
closegraph();
}
Output:-
www.ahirlabs.com