Tuesday, October 9, 2018

c program for Sutherland Hodgemen Polygon clipping

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void dda(int,int,int,int,int,int,int,int);
void dda(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
float dx,dy;
float steps,x=x1,y=y1;
int i=0;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else
steps=abs(dy);
dx=dx/steps;
dy=dy/steps;
while(i++<=steps)
{
if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
{
line(x,y,x2,y2);
getch();
return;
}
x=x+dx;
y=y+dy;
}
}

void main()
{
int n,gd,gm,x1,x2,y1,y2,xRec,yRec,b1,b2,b3,b4,l,b,yMin,yMax,xMin,xMax;
float m;
int a[10][4],i,j, flag=0,in=0;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the length and breadth of the clipping window:\n");
scanf("%d%d",&l,&b);
printf("Enter the starting co-ord of the rectangle\n");
scanf("%d%d",&xRec,&yRec);
cleardevice();
rectangle(xRec,yRec,xRec+l,yRec+b);
getch();
cleardevice();
printf("Enter the no of lines\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the co-ord of line %d\n",i+1);
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
a[i][2]=a[0][0];
a[i][3]=a[0][1];
if(i!=0)
{
a[i-1][2]=a[i][0];
a[i-1][3]=a[i][1];
}
}
cleardevice();
rectangle(xRec,yRec,xRec+l,yRec+b);
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i][2],a[i][3]);
}
getch();
cleardevice();
rectangle(xRec,yRec,xRec+l,yRec+b);
xMin=xRec;
yMin=yRec;
xMax=xRec+l;
yMax=yRec+b;
for(i=0;i<n;i++)
{
flag=0;
x1=a[i][0];
x2=a[i][2];
y1=a[i][1];
y2=a[i][3];
if(x1>=xMin && x1<=xMax && y1>=yMin && y1<=yMax)
flag++;
if(x2>=xMin && x2<=xMax && y2>=yMin && y2<=yMax)
flag++;
switch(flag)
{
case 0: break;
case 1:
if(x2>=xMin && x2<=xMax && y2>=yMin && y2<=yMax)
{
dda(x1,y1,x2,y2,xMin,yMin,xMax,yMax);
}
else
{
dda(x2,y2,x1,y1,xMin,yMin,xMax,yMax);
}
break;
case 2:
line(x1,y1,x2,y2);
break;
}
}
getch();
closegraph();
}

c program for Hilbert's Curve computer graphics

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
#include <conio.h>

void move(int j, int h, int &x, int &y)
{
if(j==1)
y-=h;
else if(j==2)
x+=h;
else if(j==3)
y+=h;
else if(j==4)
x-=h;
lineto(x,y);
delay(10);
}

void hilbert(int r,int d,int l,int u,int i,int h,int &x,int &y)
{
if(i>0)
{
i--;
hilbert(d,r,u,l,i,h,x,y);
move(r,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(d,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(l,h,x,y);
hilbert(u,l,d,r,i,h,x,y);
}
}

void main()
{
int n,x1,y1;
int x0=50,y0=150,x,y,h=10,r=2,d=3,l=4,u=1;
printf("\nGive the value of n: ");
scanf("%d",&n);
x=x0;y=y0;
int gm,gd=DETECT;
initgraph(&gd,&gm,NULL);
moveto(x,y);
hilbert(r,d,l,u,n,h,x,y);
getch();
closegraph();
}

C program for Koch curve Computer graphics

#include<graphics.h>
#include<conio.h>
#include<math.h>

void koch(int x1, int y1, int x2, int y2, int it)
{
 float angle = 60*M_PI/180;
 int x3 = (2*x1+x2)/3;
 int y3 = (2*y1+y2)/3;

 int x4 = (x1+2*x2)/3;
 int y4 = (y1+2*y2)/3;

 int x = x3 + (x4-x3)*cos(angle)+(y4-y3)*sin(angle);
 int y = y3 - (x4-x3)*sin(angle)+(y4-y3)*cos(angle);

 if(it > 0)
 {
  koch(x1, y1, x3, y3, it-1);
  koch(x3, y3, x, y, it-1);
  koch(x, y, x4, y4, it-1);
  koch(x4, y4, x2, y2, it-1);
 }
 else
 {

  line(x1, y1, x3, y3);
  line(x3, y3, x, y);
  line(x, y, x4, y4);
  line(x4, y4, x2, y2);
 }
}

int main(void)
{
 int gd = DETECT, gm,i,n, x1 = 100, y1 = 100, x2 = 400, y2 = 400;
 initgraph(&gd,&gm,"");
 printf("Enter number of interations");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 cleardevice();
 koch(x1, y1, x2, y2, i);
 getch();
 }
 return 0;
}

C Program for Bezier Curve (computer graphics)

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
int maxx,maxy;
float xxx[4][2];

void line1(float x2,float y2)
{
line(xxx[0][0],xxx[0][1],x2,y2);
delay(10);
xxx[0][0]=x2;
xxx[0][1]=y2;
}

void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if (n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab = (xxx[0][0]+xb)/2;
yab = (xxx[0][1]+yb)/2;
xbc = (xb+xc)/2;
ybc = (yb+yc)/2;
xcd = (xc+xd)/2;
ycd = (yc+yd)/2;
xabc = (xab+xbc)/2;
yabc = (yab+ybc)/2;
xbcd = (xbc+xcd)/2;
ybcd = (ybc+ycd)/2;
xabcd = (xabc+xbcd)/2;
yabcd = (yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
}
}
main()
{
int gd=DETECT,gm,i;
float temp1,temp2;
initgraph(&gd,&gm,"");
for(i=0;i<4;i++)
{
printf("Enter (x,y) coordinates of point%d : ",i+1);
scanf("%f%f",&temp1,&temp2);
putpixel(temp1,temp2,15);
xxx[i][0] = temp1;
xxx[i][1] = temp2;
}
bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8);
getch();
closegraph();
}