PROGRAMS ON GRAPHICS BASED ON PROGRAMMING LANGUAGE C & CPP (C++) . DOC CONSIST 8 PRACTICALS .
EACH PRACTICAL CONSIST MIN OF 2 QUESTION ALONG WITH SOLUTION i.e CODES
EG: MOVING FISH , MOVING CAR, STAR, GENERATION OF STARS,HOUSE,FLAG,ICE CREAM CONE,SMILEY FACE GENERATION OF BUBBLES , CONCENTRIC CIRCLES,DDA LINE ALGORITHM ,Bresenham’s line drawing algorithm,MIDPOINT CIRCLE DRAWING ALGORITHM,Bresenham’x circle drawing algorithm,TRANSLATE A POLYGON, REFLECT A POLYGON , TRANSLATE A CIRCLE , To scale a polygon about a reference point,ROTATE A POLYGON, Cohen sutherland line clipping algorithm,TEXT CLIPPING, Character Generation,FLYING KITE
Automate your Kamailio Test Calls - Kamailio World 2024
Graphics programs
1. [1]
Practical-01
Q1) Drawing star
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
line(320,100,220,300);
line(220,300,420,300);
line(420,300,320,100); // straight triangle
line(320,350,220,150);
line(220,150,420,150);
line(420,150,320,350); // reverse triangle
setfillstyle(SOLID_FILL,1);
floodfill(320,102,15); //top triangle
setfillstyle(SOLID_FILL,2);
floodfill(225,152,15); //top left
setfillstyle(SOLID_FILL,3);
floodfill(225,298,15); //bottom left
setfillstyle(SOLID_FILL,4);
floodfill(320,348,15); //bottom
setfillstyle(SOLID_FILL,5);
floodfill(415,298,15); //bottom right
setfillstyle(SOLID_FILL,6);
floodfill(415,152,15); //top right
setfillstyle(SOLID_FILL,7);
floodfill(320,200,15); //middle
getch();
}
Q2)//House
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm, "C:TCBGI");
rectangle(100,200,400,400); //big wall
line(150,100,100,200); //left side of triangle
line(150,100,200,200); //right side of triangle
line(150,100,350,100); //top side of roof
line(200,200,400,200); //bottom side of roof
line(350,100,400,200); //right side of roof
line(200,200,200,400); //dividing wall into two parts
circle(150,160,20); //drawing circle within triangle
rectangle(120,250,180,400); //door
rectangle(225,225,375,300); //window
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(102,202,WHITE); //filling first wall
setfillstyle(SOLID_FILL,BLUE);
floodfill(202,202,WHITE); //filling second wall
setfillstyle(SOLID_FILL,CYAN);
floodfill(122,252,WHITE); //filling door
setfillstyle(XHATCH_FILL,GREEN);
2. [2]
floodfill(252,252,WHITE); //filling window
setfillstyle(SOLID_FILL,MAGENTA);
floodfill(150,102,WHITE); //filling triangle
setfillstyle(SOLID_FILL,RED);
floodfill(150,160,WHITE); //filling circle
setfillstyle(BKSLASH_FILL,BROWN);
floodfill(200,150,WHITE); //filling roof
getch();
closegraph();
}
Q3)//Flag
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm, "C:TCBGI");
rectangle(100,50,300,200); //outer rectangle
rectangle(100,100,300,150); //inner rectangle
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(102,52,WHITE); //top side filled
setfillstyle(SOLID_FILL,WHITE);
floodfill(102,102,WHITE); //middle side filled
setfillstyle(SOLID_FILL,GREEN);
floodfill(102,152,WHITE); //bottom side filled
setcolor(BLUE); //setting color for circle and lines
circle(200,125,25);
line(175,125,225,125);
line(200,100,200,150);
line(182,107,218,143);
line(218,107,182,143);
setcolor(BROWN);
line(100,200,100,400);
getch();
}
Q4) Ice cream cone
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
line(320,450,270,200);
line(270,200,370,200);
line(370,200,320,450);
line(270,200,260,180);
line(260,180,380,180);
line(380,180,370,200);
ellipse(320,180,0,180,60,100); //ice-cream
circle(320,70,10); //cherry
setfillstyle(SLASH_FILL,BROWN);
floodfill(320,430,15); //filled bottom part of cone
setfillstyle(XHATCH_FILL,BROWN);
floodfill(320,198,15); //filled top part of cone
3. [3]
setfillstyle(SOLID_FILL,LIGHTMAGENTA);
floodfill(320,170,15); //filled ice-cream
setfillstyle(SOLID_FILL,RED);
floodfill(320,70,15); //filled cherry
getch();
}
Q5)//Smiley face
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm, "C:TCBGI");
ellipse(300,250,0,360,100,150); //drawing face
circle(250,200,20); //left eye
circle(350,200,20); //right eye
arc(200,200,60,275,20); //left ear
arc(400,200,265,480,20); //rigth ear
line(300,230,275,275);
line(300,230,325,275);
line(275,275,325,275); //drawing nose
setfillstyle(SOLID_FILL,BROWN);
floodfill(300,172,WHITE); //setting brown color for face
setfillstyle(SOLID_FILL,BLUE);
floodfill(350,200,WHITE);
setfillstyle(SOLID_FILL,BLUE);
floodfill(250,200,WHITE); //setting blue color for eyes
setfillstyle(SOLID_FILL,RED);
floodfill(300,235,WHITE); //setting red color for nose
setfillstyle(SOLID_FILL,GREEN);
floodfill(200,200,WHITE);
setfillstyle(SOLID_FILL,GREEN);
floodfill(400,200,WHITE); //setting green color for ears
setcolor(RED); //setting color for smile
arc(300,300,180,360,50); //drawing smile
setcolor(BLACK); //setting color for hair
line(300,100,300,170);
line(300,100,260,140);
line(300,100,340,140); //drawing hair
getch();
}
Q6)//Generation of sky
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
int x, y, c;
initgraph(&gd, &gm, "C:TCBGI");
while(!kbhit()) //checks for currently available keystrokes
{
x=rand()%639;
y=rand()%479;
putpixel(x, y, rand()%14+1);
4. [4]
delay(15);
}
getch();
closegr,aph();
}
Q7)//Generation of Bubbles
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
int x, y, c;
initgraph(&gd, &gm, "C:TCBGI");
while(!kbhit()) //checks for currently available keystrokes
{
x=rand()%639;
y=rand()%479;
c=rand()%14+1;
setcolor(c);
circle(x, y, rand()%10+10);
setfillstyle(SOLID_FILL, rand()%14+1);
floodfill(x, y, c);
delay(15);
}
getch();
closegr,aph();
}
Q8)//Concentric circles
#include<graphics.h>
void main()
{
//request auto detection
int gd=DETECT, gm;
int x, y, maxr;
//initializes the graphics system and put the system into graphics gm
initgraph(&gd, &gm, "C:TCBGI");
x=getmaxx()/2;
y=getmaxy()/2;
maxr= x<y ? x : y;
for(r=10; r<=maxr; r+=10)
{
setcolor(rand()%14+1);
circle(x, y, r);
delay(200); //suspends execution for interval (milliseconds)
}
//clean up
getch();
closegraph(); //shuts down the graphics system
}
Practical-02
5. [5]
Q1)//DDA line drawing algorithm
#include <graphics.h>
#include <math.h>
void main( )
{
float x1,y1,x2,y2;
float x,y,dx,dy,xinc,yinc,length;
int i;
int gd=DETECT,gm;
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y1 : ");
scanf("%f",&y2);
initgraph(&gd,&gm,"C:TCBGI");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
length=dx;
else
length=dy;
xinc=(x2-x1)/length;
yinc=(y2-y1)/length;
x=x1;
y=y1;
for(i=1;i<=length;i++)
{
putpixel((int)(x+0.5),(int)(y+0.5),15);
x=x+xinc;
y=y+yinc;
delay(100);
}
getch();
}
Q2)//Bresenham’s line drawing algorithm
#include <graphics.h>
#include <math.h>
int main( )
{
int x1,y1,x2,y2,gd=DETECT,gm;
int x,y,dx,dy,s1,s2;
int i,dp,temp,swap=0;
printf("Enter the value of x1 : ");
scanf("%d",&x1);
printf("Enter the value of y1 : ");
scanf("%d",&y1);
printf("Enter the value of x2 : ");