Bresan Harmalgorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

C PROGRAM FOR BRESENHAMS LINE ALGORITHM

# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

C PROGRAM FOR BRESENHAMS CIRCLE ALGORITHM

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();

printf("Enter the radius ");


scanf("%d",&r);

x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}

C PROGRAM FOR 2D TRANSFORMATIONS

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate();
void scale();
void rotate();
void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,150,150,100);
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1: translate();
break;
case 2: scale();
break;
case 3: rotate();
break;
default: printf("you have enterd wrong choice");
break;
}
getch();
closegraph();
}
void translate()
{
int tx,ty;
setcolor(2);

outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
printf("\nEnter tx: ");
scanf("%d",&tx);
printf("\nEnter ty: ");
scanf("%d",&ty);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Translation");
rectangle(100+tx,150+ty,150+tx,100+ty);
}
void scale()
{
int sx,sy;
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
printf("\nEnter sx: ");
scanf("%d",&sx);
printf("\nEnter sy: ");
scanf("%d",&sy);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Scaling");
rectangle(100*sx,150*sy,150*sx,100*sy);
}
void rotate()
{
float theta;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
theta=theta*(3.14/180);
cleardevice();
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;

x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);

C PROGRAM FOR 3D TRANSFORMATIONS

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);
void main()
{
int gd=DETECT,gm;
int ch,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<2;i++)
{
printf("\nEnter X-coordinate of vertex %d : ",i+1);
scanf("%d",&xp[i]);
printf("\nEnter Y-coordinate of vertex %d : ",i+1);
scanf("%d",&yp[i]);
}
printf("\nEnter The Z-axis For 3d Figure : ");
scanf("%d",&z);
clrscr();
cleardevice();
display(xp,yp);
getche();
do
{
printf("----- MENU -----");
printf("\n1.TRANSLATION.");
printf("\t2.SCALING.");
printf("\n3.ROTATION.");
printf("\t4.EXIT.");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
clrscr();
cleardevice();
display(xp,yp);
switch(ch)

{
case 1 : translate();
break;
case 2 : scaling();
break;
case 3 : rotation();
break;
case 4 : exit(0);
default:
outtextxy(1,66,"-PLEASE SELECT THE RIGHT OPTION-");
}
}
while(ch!=4);
getch();
closegraph();
}
void translate()
{
int p[4][4];
int tx,ty,tz,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Translating Factor tx : ");
scanf("%d",&tx);
printf("\nEnter The Translating Factor ty : ");
scanf("%d",&ty);
printf("\nEnter The Translating Factor tz : ");
scanf("%d",&tz);
clrscr();
cleardevice();
display();
p[0][3]=tx;
p[1][3]=ty;
p[2][3]=tz;
matrixmul(p);
}
void scaling()
{
int p[4][4];
int sx,sy,sz,i,j;

for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Scaling Factor sx : ");
scanf("%d",&sx);
printf("\nEnter The Scaling Factor sy : ");
scanf("%d",&sy);
printf("\nEnter The Scaling Factor sz : ");
scanf("%d",&sz);
if(sx==0)
sx=1;
if(sy==0)
sy=1;
if(sz==0)
sz=1;
clrscr();
cleardevice();
p[0][0]=sx;
p[1][1]=sy;
p[2][2]=sz;
p[3][3]=1;
matrixmul(p);
}
void rotation()
{
float res[4][1],p[4][4],t[4][1];
int ang,i,j,k,l,rch;
float rad;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Rotating Angle : ");
scanf("%d",&ang);
rad=ang*0.0174;
printf("\nChoose the axis of roration ");
printf("\n1.X-axis");
printf("\n2.Y-axis");
printf("\n3.Z-axis");
printf("\nEnter Your Choice : ");
scanf("%d",&rch);
switch(rch)
{
case 1 : p[1][1]=cos(rad);
p[1][2]=(-1)*sin(rad);
p[2][1]=sin(rad);
p[2][2]=cos(rad);

break;
case 2 :
p[0][0]=cos(rad);
p[2][0]=(-1)*sin(rad);
p[0][2]=sin(rad);
p[2][2]=cos(rad);
break;
case 3 :
p[0][0]=cos(rad);
p[0][1]=(-1)*sin(rad);
p[1][0]=sin(rad);
p[1][1]=cos(rad);
break;
default :
printf("\nInvalid Choice !");
}
clrscr();
cleardevice();
for(i=0;i<2;i++)
{
t[0][0]=xp[i];
t[1][0]=yp[i];
t[2][0]=z;
t[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(p[j][l]*t[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
z=res[2][0];
}
display(xp,yp);
}
void display(int xp[2],int yp[2])
{

int x3,y3,x4,y4;
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
outtextxy(getmaxx()/2+5,getmaxy()/2+5,"(0,0)");
outtextxy(getmaxx()-50,getmaxy()/2+10,"X-Axis");
outtextxy(getmaxx()/2+10,20,"Y-Axis");
outtextxy(10,getmaxy()/2+10,"X'-Axis");
outtextxy(getmaxx()/2+10,getmaxy()-20,"Y'-Axis");
rectangle(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+xp[1],getmaxy()/2-yp[1]);
if(z>=xp[0])
{
x3=z+xp[0];
y3=z+yp[0];
x4=z+xp[1];
y4=z+yp[1];
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
else
{
x3=xp[0]-z;
y3=yp[0]-z;
x4=xp[1]-z;
y4=yp[1]-z;
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
}
void matrixmul(int a[4][4])
{
float res[4][1],b[4][1];
int i,j,k,l;
for(i=0;i<2;i++)
{
b[0][0]=xp[i];
b[1][0]=yp[i];
b[2][0]=z;
b[3][0]=1;
for(j=0;j<4;j++)
{

for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(a[j][l]*b[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
}
z=res[2][0];
display(xp,yp);
}

C PROGRAM FOR DRAWING A BEZIER CURVE

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];

for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }

printf("\n\n Enter four points : \n\n");


for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{

u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}
C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;

return(c);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy