Pages

Showing posts with label Computer Graphics Lab. Show all posts
Showing posts with label Computer Graphics Lab. Show all posts

Thursday, 27 June 2013

BRESENHAM’s LINE DRAWING ALGORITHM



BRESENHAM’s LINE DRAWING ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void linebrel(int,int,int,int);
void main()
{
int gd=DETECT ,gm,xa,xb,ya,yb;
clrscr();
initgraph(&gd,&gm,"c:/tc/bgi");
printf("Enter the starting points of the line:");
scanf("%d%d",&xa,&ya);
printf("Enter the ending points of the line:");
scanf("%d%d",&xb,&yb);
linebrel(xa,ya,xb,yb);
getch();
}
void linebrel(int xa,int ya,int xb,int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,6);
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p+twodydx;
}
putpixel(x,y,6);
}
}




Read more...

DDA LINE DRAWING ALGORITHM



DDA LINE DRAWING ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define ROUND(a)((int)(a+0.5))
void linebrel(int,int,int,int);
void main()
{
int gd=DETECT,gm,xa,xb,ya,yb;
clrscr();
initgraph(&gd,&gm,"c:/tc/bgi");
printf("Enter the starting point of line:");
scanf("%d%d",&xa,&ya);
printf("Enter the ending points of line:");
scanf("%d%d",&xb,&yb);
linebrel(xa,ya,xb,yb);
getch();
}
void linebrel(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),5);
for(k=0;k<steps;k++)
{
x+=xincrement;
y+=yincrement;
putpixel(ROUND(x),ROUND(y),5);
}
}


NOTE : . I am sharing knowledge for education purpose only
Read more...

Walking Man C program


Walking Man C program



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,c=200,i=0,x=40,l=15,h=15,ht=0;
initgraph(&gd,&gm,"c:\tc\bgi");
cleardevice();
setcolor(5);
cont:
while(!kbhit())
{
setcolor(14);
circle(x-20,115,15);
line(x-20,130,x-20,175);
line(x-20,175,x-20-l,200);
line(x-20,175,x-20+l,200);
line(x-20,140,x-20+h,160);
line(x-20,140,x-20-h,160);
for(i=0;i<620;i+=20)
{
 }
c++;
if(c==0)
c=100;
setcolor(0);
delay(50);
ellipse(x,100,0,180,50,30);
line(x-50,100,x+50,100);
line(x,100,x,150);
circle(x-20,115,15);
line(x-20,175,x-20-l,200);
line(x-20,175,x-20+l,200);
line(x-20,140,x-20-h,160);
line(x-20,140,x-20+h,160);
x++;
l--;
if(l==-15)
l=15;
if(ht==1)
h++;
else
h--;
if(h==15)
 ht=0;
else if(h==-15)
 ht=1;
if(getch()==' ')
 {
 while(!kbhit())
 getch();
 goto cont;
 }
}
}
Read more...