/* DDA Techinique to Draw a striaght Line */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2,xinc,yinc;
int i,step;
float dx,dy;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter 1st End Points x1 & y1 : \n");
scanf("%d %d",&x1,&y1);
printf("\nEnter 2nd End points x2 & y2 : \n ");
scanf("%d %d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx) > abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
putpixel(x1,y1,BLUE);
for(i=1; i<=step; i++)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,BLUE);
}
getch();
}
/*MID POINT CIRCLE ALGORITHM IMPLEMENTED IN C*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
void Drawcircle(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"h:\\tc1\\bgi");
Drawcircle(x,y,r);
getch();
closegraph();
}
/* Function for MID-POINT CIRCLE algorithm */
void Drawcircle(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
/* function to plot point obtained from algorithm */
cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,7);
putpixel(xctr -x,yctr +y,7);
putpixel(xctr +x,yctr -y,7);
putpixel(xctr -x,yctr -y,7);
putpixel(xctr +y,yctr +x,7);
putpixel(xctr -y,yctr +x,7);
putpixel(xctr +y,yctr -x,7);
putpixel(xctr -y,yctr -x,7);
// getch();}
/* OUTPUT SCREEN FOR MID-POINT ALGORITHM FOR CIRCLE-DRAWING
ALGORITHM */
/*Bresenhams Techinique to Draw a striaght Line */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter 1st End Points x1 & y1 : \n");
scanf("%d %d",&x1,&y1);
printf("\nEnter 2nd End points x2 & y2 : \n");
scanf("%d %d",&x2,&y2);
bresenhams(x1,y1,x2,y2);
getch();
closegraph();
}
bresenhams(int x1,int y1,int x2,int y2)
{
int dx,dy,x,y;
int xc,c1,c2,p;
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
c1=2*dy;
c2=2*(dy-dx);
if(x1 > x2)
{
x=x2;
y=y2;
xc=x1;
}
else
{
x=x1;
y=y1;
xc=x2;
}
putpixel(x,y,2);
while(x<xc)
{
x=x+1;
if(p<0)
p=p+c1;
else
{
y=y+1;
p=p+c2;
}
putpixel(x,y,2);
}
return;
}
/*PROGRAM FOR LIANG BASWKY ALGORITHM IMPLEMENTION */
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||( ( p3 == 0.0 )
&& ( q3 < 0.0 ) ) ||( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"h:\\tc1\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d
",wxmin,wymin,wxmax,wymax);
printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"h:\\TC1\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}
/* OUTPUT SCREEN FOR LIANG BASKEY ALGORITHM OF LINE CLIPPING */
/* conversion of window co-ordinate to viewport co-ordinates */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"g:\\tc1\\bgi");
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
/* OUTPUT OF WINDOW CO-ORDINATE TO VIEWPORT CO-ORDINATE */
Windowing coordinate
Viewport
Enter the co-ordinate of figure:100 200200 300100 300
/* PROGGRAM FOR 2-D TRANSFORMATION IMPLEMETATION (TRANSLATION,
ROTATION, SCALING) */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"g:\\tc1\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf("******2D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Exit\nEnter your
choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"g:\\tc1\\BGI");
rectangle(x,y,az,w);
printf("*******Translation*******\n\n");
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"g:\\tc1\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value of
fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"g:\\tc1\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1);
break;
case 4:
exit(0);
}
}
getch();
}
/* OUTPUT FOR TRANSLATION TRANSFORMATION */
/* OUTPUT FOR ROTATION TRANSFORMATION */
/* OUTPUT SCREEN FOR SCALING TRANSFORMATION */