0% found this document useful (0 votes)
138 views13 pages

CG Practical

The document contains C code for drawing 3D shapes using graphics functions. It defines a function draw3d() that takes coordinates of a polygon and draws the 3D shape by connecting the coordinates with lines. The main() function gets input coordinates and depth, calls draw3d() to draw the 3D shape, and additionally draws top, side and 3D views of the shape by connecting specific coordinates with lines.

Uploaded by

Rohit Sabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views13 pages

CG Practical

The document contains C code for drawing 3D shapes using graphics functions. It defines a function draw3d() that takes coordinates of a polygon and draws the 3D shape by connecting the coordinates with lines. The main() function gets input coordinates and depth, calls draw3d() to draw the 3D shape, and additionally draws top, side and 3D views of the shape by connecting specific coordinates with lines.

Uploaded by

Rohit Sabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

#include<stdio.

h>
#include<graphics.h>
#include<math.h> void
main()
{
float x,y,x1,y1,x2,y2,dx,dy,length; int
i,gd,gm;
clrscr();
printf("Enter the value of x1 :\t");
scanf("%f",&x1);
printf("Enter the value of y1 :\t");
scanf("%f",&y1);
printf("Enter the value of x2 :\t");
scanf("%f",&x2);
printf("Enter the value of y2 :\t");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"\\tc\\BGI");
dx=abs(x2-x1);
dy=abs(y2-y1);if
(dx >= dy)
{
length = dx;
}
else
{
length = dy;
}
dx = (x2-x1)/length; dy =
(y2-y1)/length;
x = x1 + 0.5; /* Factor 0.5 is added to round the values */ y = y1 +
0.5; /* Factor 0.5 is added to round the values */ putpixel (x, y, 15);
i = 1; while(i <= length)
{
x = x + dx;y
= y + dy;
putpixel (x, y, 15);i =
i + 1;
delay(100);

}
getch();
closegraph();
}

 Output
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
int i,gd,gm;
clrscr();
printf("Enter the value of x1 :\t");
scanf("%f",&x1);
printf("Enter the value of y1 :\t");
scanf("%f",&y1);
printf("Enter the value of x2 :\t");
scanf("%f",&x2);
printf("Enter the value of y2 :\t");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\BGI");

dx=abs(x2-x1);
dy=abs(y2-y1);
x = x1;
y = y1;
putpixel (x, y, 15) ;
e = 2 * dy-dx;
i = 1;do
{
while(e >= 0)
{
y = y + 1;
e = e - 2 * dx;
}
x = x + 1;
e = e + 2 * dy;
i = i + 1;
putpixel (x, y, 15);
}
while( i <= dx);

getch();
closegraph();
}
#include<stdio.h>
#include<graphics.h>
#include<conio.h>

void flood(int, int, int, int);


void main()
{int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"\\tc\\bgi");
rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}

void flood(int seed_x,int seed_y,int foreground_col,int background_col)


{
if(getpixel(seed_x,seed_y)!= background_col &&
getpixel(seed_x,seed_y)!= foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
flood(seed_x+1,seed_y,foreground_col,background_col);
flood(seed_x-1,seed_y,foreground_col,background_col);
flood(seed_x,seed_y+1,foreground_col,background_col);
flood(seed_x,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y+1,foreground_col,background_col);
flood(seed_x-1,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y-1,foreground_col,background_col);
flood(seed_x-1,seed_y+1,foreground_col,background_col);
}
}

 output
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>

int main()
{
int i, x, y, tx, ty, sx, sy, angle=10, xmax, ymax, xmid, ymid, op;int
gd,gm;
float p1[10]= { 50,50,
100,50,
100,100,
50,100,
50,50,
};
int pi[10];
float b[3][3]={ 1,0,0,
0,1,0,
0,0,1
};
int c[1][1];
float a[1][1];
printf("\nSelect the transformation : ");
printf("\n1 : Traslation");
printf("\n2 : Rotation");
printf("\n3 : Scaling");
printf("\n4 : Rotation about arbitrary point");
printf( "\nEnter the option : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter x traslation : ");
scanf("%d",&tx);
printf("\nEnter y traslation : ");
scanf("%d",&ty);
b[0][0] = 1;
b[0][1] = 0;
b[0][2] = 0;

b[1][0] = 0;
b[1][1] = 1;
b[1][2] = 0;

b[2][0] = tx;
b[2][1] = ty;
b[2][2] = 1;
break;
case 2: printf("\nEnter Rotation angle : ");
scanf("%d",&angle);
b[0][0] =cos(angle*3.142/180);
b[0][1] =sin(angle*3.142/180);
b[0][2] = 0;
b[1][0] =-sin(angle*3.142/180);
b[1][1] = cos(angle*3.142/180);
b[1][2] = 0;

b[2][0] = 0;
b[2][1] = 0;
b[2][2] = 1;

break;
case 3: printf("\nEnter x scaling : ");
scanf("%d",&sx);
printf("\nEnter y scaling : ");
scanf("%d",&sy);

b[0][0] = sx;
b[0][1] = 0;
b[0][2] = 0;

b[1][0] = 0;
b[1][1] = sy;
b[1][2] = 0;

b[2][0] = 0;
b[2][1] = 0;
b[2][2] = 1;

break;
case 4: printf("\nEnter x coordinate of arbitrary point : ");
scanf("%d",&x);
printf("\nEnter y coordinate of arbitrary point : ");
scanf("%d",&y);
printf("\nEnter Rotation angle : ");
scanf("%d",&angle);

tx = x;
ty = y;

b[0][0] =cos(angle*3.142/180);
b[0][1] =sin(angle*3.142/180);
b[0][2] = 0;

b[1][0] =-sin(angle*3.142/180);
b[1][1] = cos(angle*3.142/180);
b[1][2] = 0;

b[2][0] = -tx* cos(angle*3.142/180) + ty*sin(angle*3.142/180)+tx;


b[2][1] = -tx* sin(angle*3.142/180) - ty*cos(angle*3.142/180)+ty;
b[2][2] = 1;

}
detectgraph(&gd,&gm); initgraph(&gd,&gm,"\\tc\\bgi"); //
Initialize graphics
xmax = getmaxx(); // Get maximum x coordinate
ymax = getmaxy(); // Get maximum y coordinate
xmid = xmax/2; // Get the center x coordinate
ymid = ymax/2; // Get the center y coordinate

setcolor(1);
line(xmid,0,xmid,ymax); // Draw y coordinate
line(0, ymid, xmax, ymid); // Draw x coordinate

setcolor(4);
for (i=0; i<8;i=i+2)
{
line(p1[i]+xmid,ymid-p1[i+1],xmid+p1[i+2],ymid-p1[i+3]);
}
for(i=0;i<9;i=i+2)
{ a[0][0]=p1[i];
a[0][1]=p1[i+1];
c[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]+b[2][0];
c[0][1] =
a[0][0]*b[0][1]+a[0][1]*b[1][1]+b[2][1];
pi[i]=c[0][0];
pi[i+1]=c[0][1];
}
setcolor(15);
for (i=0; i<8;i=i+2)
{
line(xmid+pi[i],ymid-pi[i+1],xmid+pi[i+2],ymid-pi[i+3]);
}
getch();
closegraph();
return 0;
}
 Output
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
int gd,gm,maxx,maxy;
float xxx[4][2];

void line1(float x2,float y2)


{
line(xxx[0][0],xxx[0][1],x2,y2);
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);
}
}
void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("CANNOT DETECT A GRAPHICS CARD");
exit(1);
}
initgraph(&gd,&gm,"\\tc\\bgi");
}
void main()
{
int i;
float temp1,temp2;
igraph();
for(i=0;i<4;i++)
{
printf("Enter (x,y) coordinates of point%d : ",i+1);
scanf("%f,%f",&temp1,&temp2);
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();
}

 Output
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int s,int x[20],int y[20],int d);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the No of sides : ");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();
setcolor(14);
for(i=0;i<s-1;i++)
{
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
}
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();//top view
for(i=0;i<s-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i<s-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();
}
void draw3d(int s,int x[20],int y[20],int d)
{
int i,j,k=0;
for(j=0;j<2;j++)
{
for(i=0;i<s-1;i++)
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
}
for(i=0;i<s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);
}

 Output

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