Computer Graphics (Lab File) - Satyam
Computer Graphics (Lab File) - Satyam
24 Basics of Blender
1. Putpixel
Purpose:-Putpixel function is to draw the pixel on the screen. Pixel is a small dot on the
Example: – putpixel(100,100,BLUE);
2. SetbkColor
screen. Syntax:-setbkcolor(COLOR);
Example:-setbkcolor(RED);
3. Setlinestyle
Purpose:-setlinestyle function is used to set the current line style, width and
Example:-setlinestyle(SOLID_LINE,1,2);
4. Setcolor
Purpose:-setcolor is to set the color of the objects which is to be drawn after this
Example:-setcolor(RED);
5. Rectangle
Purpose:- Rectangle function is used to draw the rectangle on the screen. X1,y1 are the
lower left coordinates of the rectangle and the x2,y2 are the upper right coordinates of
the rectangle.
Syntax:– rectangle(x1,,y1,x2,y2);
Example:– rectangle(100,100,200,200);
6. Textheight
Syntax:-textheight(STRING);
Example:-i=textheight(“HELLO”);
7. Textwidth
pixels Syntax:-textwidth(STRING);
Example:-i=textwidth(“HELLO”);
8. Getx
ordinate Syntax:-getx();
Example:-x=getx();
9. Gety
coordinate Syntax:-gety();
Example:-y=gety();
10. Getmaxx
screen Syntax:-getmaxx();
Example:-maxx=getmaxx();
11. Getmaxy
screen Syntax:-getmaxy();
Example:-maxy=getmaxy();
12. Line
Example:-line(100,100,200,100);
13. Closegraph
Syntax:-closegraph();
Example:-closegraph();
14. Moveto
Example:-moveto(getmaxx/2, getmaxy/2);
15. Settextstyle
Example:-settextstyle(1,1,10);
Font 1 DEFAULT
2 TRIPLEX
3 SMALL
4 SANS SERIF
5 GOTHIC
Direction 0 HORIZ_DIR
VERT_DIR
Size 0 SMALL
10 large
16. Circle
Example:circle(100,100,50);
17. Cleardevice
Purpose: cleardevice function is used to clear the contents or graphic images on the
screen in graphics mode.
Syntax:cleardevice();
Example:cleardevice();
18. Outtextxy
Example:-outtextxy(100,100,”HELLO”);
19. Sector
20. Arc
Purpose:arc draws the arc on the screen, arc is a part of the circle
Example:arc( 100,100,90,180,50);
21. Setfillstyle
Purpose: setfillstyle is used to set the color and style to be filled in the object using
the flood fill method.
Syntax:setfillstyle(STYLE, COLOR);
Example:setfillstyle(1,RED)
22. Floodfill
Purpose:floodfill function is used to fill the color in the object, object may be circle,
rectangle or any other closed image.
Syntax:floodfill(x,y,boundary color);
Example:floodfill(100,100,BLUE);
23. Ellipse
Example:ellipse(100,100,90,200,20,20);
24. Outtext
Purpose:outtext function is used to display the text on the screen, using this
function text is display in the current position.
Syntax:outtext(STRING);
Example:outtex(“HELLO”);
25. Getcolor
Syntax:getcolor();
Example:intclr = getcolor();
26. Getpixel
Syntax:getpixel(x,y);
Example: color=getpixel(100,100);
Q2. WAP to find the biggest of two numbers using text functions graphics.
Sol: #include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int a,b;
clrscr();
textmode(C80);
textbackground(WHITE);
textcolor(BLUE);
gotoxy(20,10);
scanf("%d",&a);
gotoxy(20,12);
scanf("%d",&b);
textcolor(DARKGRAY);
if(a>b)
gotoxy(20,15);
printf("%d",a);
else
gotoxy(20,15);
printf("%d",b);
restorecrtmode();
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
int arr[]={320,150,400,250,250,350,320,150};
initgraph(&gd,&gm,"c://turboc3//bgi");
//polygon
setcolor(RED);
drawpoly(4,arr);
outtextxy(320,130,"polygn");
//Line
setcolor(RED);
setbkcolor(LIGHTGREEN);
line(50,40,190,40);
outtextxy(30,30,"Line");
//Triangle
line(150,100,100,200);
line(100,200,200,200);
line(200,200,150,100);
outtextxy(160,100,"Triangl
e"); //Square
line(400,300,550,300);
line(400,300,400,450);
line(550,300,550,450);
line(400,450,550,450);
outtextxy(420,350,"Square
"); //Rectangle
line(400,100,600,100);
line(400,100,400,200);
line(400,200,600,200);
line(600,100,600,200);
outtextxy(420,120,"Rectangl
e"); //Circle
circle(120,280,30);
outtextxy(150,250,"Circl
e"); //Arc
arc(120,210,180,0,30);
outtextxy(120,210,"Arc");
//Pieslice
pieslice(250,450,30,60,90);
outtextxy(250,450,"Pieslice");
//Ellipse
ellipse(120,350,0,360,30,20);
outtextxy(150,330,"Ellipse");
//Sector
sector(150,450,0,135,50,60);
outtextxy(150,460,"Sector");
getch();
closegraph();
#include <conio.h>
int main()
int graphicdriver=DETECT,graphicmode;
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//Printing message for user
y = y + 20;
getch();
return 0;
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
/* Declaring a variable */
gdriver = DETECT;
initgraph(&gdriver, &gmode,
"c:\\turboc3\\bgi");
poly[1] = 100;
poly[2] = 120;
poly[4] = 240;
poly[6] = 120;
drawpoly(5, poly);
getch();
closegraph();
return 0;
}
#include<conio.h>
#include<graphics.h>
int main(){
int gd = DETECT,gm;
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,2
50,300);
rectangle(250,180,420
,300);
rectangle(180,250,220
,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL
, BROWN);
floodfill(152, 182,
WHITE); floodfill(252,
182, WHITE);
setfillstyle(SLASH_FILL
, BLUE);
floodfill(182, 252,
WHITE);
setfillstyle(HATCH_FILL
, RED); floodfill(200,
105, WHITE);
floodfill(210, 105,
WHITE);
getch();
closegraph();
return 0;
int main() {
/* request auto detection */
int gdriver = DETECT, gmode;
int errcode, i;
if (errcode != grOk) {
/* error occurred */
getch();
printf("Error in graphics!!\n");
return 0;
}
/* drawing tail */
setcolor(DARKGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
pieslice(90 + i, getmaxy()/2,
135, 225, 50); floodfill(90 + i,
getmaxy()/2, DARKGRAY);
setcolor(LIGHTGRAY);
setfillstyle(SOLID_FILL, LIGHTGRAY);
ellipse(150 + i, getmaxy()/2, 0,
getmaxy()/2, 7);
setcolor(DARKGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
pieslice(170 + i, getmaxy()/2,
getmaxy()/2, DARKGRAY);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
setcolor(BLACK);
delay(120);
cleardevice();
setcolor(DARKGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
setcolor(LIGHTGRAY);
setfillstyle(SOLID_FILL, LIGHTGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
pieslice(170 + i, getmaxy()/2,
getmaxy()/2, DARKGRAY);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
setcolor(BLACK);
delay(120);
cleardevice();
setcolor(DARKGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
pieslice(90 + i, getmaxy()/2,
getmaxy()/2, DARKGRAY);
setcolor(LIGHTGRAY);
setfillstyle(SOLID_FILL, LIGHTGRAY);
setfillstyle(SLASH_FILL, DARKGRAY);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
setcolor(BLACK);
cleardevice();
return 0;
}
Q8. WAP to implement DDA line drawing algorithm.
Sol: #include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
#include<conio.h>
void main( )
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cin>>x1>>y1;
cin>>x2>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
getch();
closegraph();
}
#include<graphics.h>
#include<conio.h>
dx=x1-x0
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
if(p>=0)
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
else
putpixel(x,y,7);
p=p+2*dy;
x=x+1;
int main()
#include<graphics.h>
#include<conio.h>
int radius) {
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
y += 1;
err += 2*y + 1;
if (err > 0)
x -= 1;
err -= 2*x + 1;
int main()
initgraph(&gdriver, &gmode,
"c:\\turboc3\\bgi"); cout<<"Enter
cin>>r;
drawcircle(x, y, r);
getch();
}
Q11. WAP to implement Bresenhem’s circle drawing algorithm.
Sol: #include<stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
putpixel(x+xc,y+yc,YELLOW);
putpixel(x+xc,-y+yc,WHITE);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-
x+xc,y+yc,DARKGRAY);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
yc,int r) {
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
if(d<=0)
d=d+(4*x)+6;
else
d=d+(4*x)-(4*y)+10;
y=y-1;
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
int main(void)
errorcode = graphresult();
scanf("%d%d",&xc,&yc);
scanf("%d",&r);
BresenhamCircle(xc,yc,r);
getch();
closegraph();
return 0;
}
Q12. WAP to implement Midpoint Ellipse algorithm.
Sol: #include<stdio.h>
#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x,y;
float d1,d2,dx,dy;
dy = 2 * pow(rx,2) * y;
do // region one
display(xs1,ys1,x,y);
if(d1<0)
x++;
dx = dx + (2 * (pow(ry,2)));
d1 = d1 + dx +(pow(ry,2));
else
x++;
y--;
dx = dx + (2 * (pow(ry,2)));
dy = dy - (2 * (pow(rx,2)));
d1 = d1 + dx - dy + (pow(ry,2));
region two
display(xs1,ys1,x,y);
if(d2>0)
x = x;
y--;
dy = dy - (2 * (pow(rx,2)));
d2 = d2 - dy + pow(rx,2);
else
x++;
y--;
dy = dy - (2 * (pow(rx,2)));
dx = dx + (2 * (pow(ry,2)));
d2 = d2 +dx - dy + pow(rx,2);
}while(y>0);
putpixel(xs-x,ys-y,WHITE);
putpixel(xs+x,ys-y,WHITE);
putpixel(xs-x,ys+y,WHITE);
int main(void)
int xs1,ys1;
float rx1,ry1;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cout<<"xc = \t";
cin>>xs1;
cout<<"yc = \t";
cin>>ys1;
cin>>rx1;
cin>>ry1;
ellips1(xs1,ys1,rx1,ry1);
getch();
closegraph();
return 0;
}
Q13. WAP of 2D Translation, Rotation, Scaling.
Sol: #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void main()
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(1);
scanf("%d%d%d%d%d
%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
choice:");
scanf("%d",&c);
switch(c)
case 1:
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
rotation"); scanf("%d,&r");
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
case 3:
factor"); scanf("%d
%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 4:
break;
default:
closegraph();
Sol:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
Void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
int a;
clrscr();
initgraph(&gd,&gm,c:\\turboc3\\bgi”);
scanf(“%d%d%d%d%d%d”,&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
line(320,0,320,430);
line(0,240,640,240);
scanf(“%d”,&a);
if(a==1)
X1=x1;
X2=x2;
X3=x3;
Y1=y1+240;
Y2=y2+240;
Y3=y3+240;
Else
If(a==2)
{
Y1=y1;
Y2=y2;
Y3=y3;
X1+=320;
X2+=320;
X3+=320;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}
Q17. WAP of 3d Translation, Rotation, Scaling of an object.
Sol: /* 3d Translation */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
cleardevice();
line(midx,0,midx,midy);
line(0,midy,maxx,midy);
void main()
getch();
{
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setfillstyle(9,30);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"Original object");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-
90,20,5); axis();
outtextxy(100,20,"Translation");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x+100),midy-(y+20),midx+(x+60),midy-
(y+90),20,5); axis();
outtextxy(100,20,"Scaling");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x*100),midy-(y*20),midx+(x*60),midy-
(y*90),20*z,5); axis();
outtextxy(100,20,"Rotation");
x1=100*cos(ang*3.14/180)-
20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.
14/180); x2=100*cos(ang*3.14/180)-
90*sin(ang*3.14/180);
y2=100*sin(ang*3.14/180)+90*sin(ang*3.
14/180); axis();
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph();
}
Q18. WAP using the Floodfill function.
Sol: #include<iostream.h>
#include<graphics.h>
#include<dos.h>
if(getpixel(x,y) == oldcolor)
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
int main()
int gm,gd=DETECT,radius;
int x,y;
circle\n"; cin>>x>>y;
cin>>radius;
initgraph(&gd,&gm,"c:\\turboc3\
\bgi"); circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph();
return 0;
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
for (int
i=0;i<500;i++) {
line(50+i,370,90+i,3
70);
arc(110+i,370,0,180,
20);
line(130+i,370,220+i,3
70);
arc(240+i,370,0,180,20
);
line(260+i,370,300+i,3
70);
line(300+i,370,300+i,3
50);
line(300+i,350,240+i,3
30);
line(240+i,330,200+i,3
00);
line(200+i,300,110+i,3
00);
line(110+i,300,80+i,33
0);
line(80+i,330,50+i,340
);
line(50+i,340,50+i,370
);
/***CAR Windows***/
line(165+i,305,165+i,3
30);
line(165+i,330,230+i,3
30);
line(230+i,330,195+i,3
05);
line(195+i,305,165+i,3
05);
line(160+i,305,160+i,3
30);
line(160+i,330,95+i,33
0);
line(95+i,330,120+i,30
5);
line(120+i,305,160+i,305);
/**Wheels**/
circle(110+i,370,17);
circle(240+i,370,17);
delay(10);
cleardevice();
line(0,390,639,390); //ROAD
getch();
}
Q20. WAP to fill polygon using Fillpoly Function.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
// driver code
int main(){
file
int arr[] = {320, 150, 400, 250, 250, 350, 320, 150};
initgraph(&gd,&gm,"c://turboc3//bgi");
// fillpoly function
fillpoly(4, arr);
getch();
// graphics system .
closegraph();
return 0;
main()
setfillstyle(XHATCH_FILL, RED);
getch();
closegraph();
return 0;
#include<graphics.h>
#include<dos.h>
#include<conio.h>
int current;
current=getpixel(x,y);
if(current==old)
delay(5);
putpixel(x,y,newcol);
floodfill(x+1,y,old,newcol);
floodfill(x-1,y,old,newcol);
floodfill(x,y+1,old,newcol);
floodfill(x,y-1,old,newcol);
floodfill(x+1,y+1,old,newcol);
floodfill(x-1,y+1,old,newcol);
floodfill(x+1,y-1,old,newcol);
floodfill(x-1,y-1,old,newcol);
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\B
GI"); rectangle(50,50,150,150);
floodfill(70,70,0,15);
closegraph();
}
Q23. WAP for 8 connected Flood Fill.
Sol: #include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
int current;
current=getpixel(x,y);
if(current==old)
delay(5);
putpixel(x,y,newcol);
floodfill(x+1,y,old,newcol);
floodfill(x-1,y,old,newcol);
floodfill(x,y+1,old,newcol);
floodfill(x,y-1,old,newcol);
floodfill(x+1,y+1,old,ne
wcol); floodfill(x-
1,y+1,old,newcol);
floodfill(x+1,y-
1,old,newcol);
floodfill(x-1,y-
1,old,newcol);
void main()
int gm,gd=DETECT;
initgraph(&gd,&gm,"c\\turbo3\\
bgi");
rectangle(50,50,150,150);
floodfill(70,70,0,15);
getch();
closegraph();
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
int x[4],y[4],i;
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,"c:\\turboc3\\bgi");
for(i=0;i<4;i++)
scanf("%d%d", &x[i],&[i]);
getch();
closegraph();
}
Q25 Basics of Blender Software
RENDERING:
A rendering is a pictorial output of a 3D scene or object. Features like materials,
lighting, oversampling and shadows control the effects and quality of the
rendering. The more of these features you add, the more realistic your scene
become, but also lengthens rendering times.
Lighting:
Lighting provides the realism to your scene through reflections and shadows. You
can control the type of light, intensity and color. Some lights can give a “fog” or
“dusty” look with a halo or volume lighting effect. Illumination distances can also
be set.
Cameras:
Your camera is your point-of-view for the scene. Just like a real camera, you can
control lens length to achieve close-ups or wide angles. Clipping distance can
also be set to control how far and near the camera sees. Depth-of-field can
be controlled using nodes.
ANIMATION:
An animation is a series of rendered images that form a movie. The quality of
your movie is controlled by all of the above mentioned features including frames
per second (fps), output size, file type and compression. The most common
The method of animation is called key-framing. Key frames are created at various
points in the animation while the computer generates all of the transition frames
between the two keys. Basic animation options include changing size, rotation
and location of objects.
10. go to material and select the color which you want to apply over
the object
11. Finally render it by pressing F12 or from the menu bar button
4. click on center point on the top of the mug and delete faces
11. render it
6. click left mouse button to the centre dot of the left handler
8. press A to deselect
11. subdivide the curve by pressing W and give the shape to the