Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
373 views

Computer Graphics Practical File

The document contains solutions to 8 questions related to computer graphics programming in C language. Each question provides a code snippet to draw or manipulate graphical elements like lines, circles, triangles etc. using functions like line(), putpixel(), outtextxy() etc. The questions cover topics like drawing basic shapes, implementing drawing algorithms like DDA, Bresenham and midpoint circle algorithm, animating objects like a clock and performing transformations like rotation.

Uploaded by

robin singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
373 views

Computer Graphics Practical File

The document contains solutions to 8 questions related to computer graphics programming in C language. Each question provides a code snippet to draw or manipulate graphical elements like lines, circles, triangles etc. using functions like line(), putpixel(), outtextxy() etc. The questions cover topics like drawing basic shapes, implementing drawing algorithms like DDA, Bresenham and midpoint circle algorithm, animating objects like a clock and performing transformations like rotation.

Uploaded by

robin singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Q1: - WAP in C to draw a hut using line() function.

Solution: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main(){
int gm;
int gd=DETECT;
int x;int y;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
x=getmaxx();
y=getmaxy();
printf("value of x=%d",x);
printf("value of y=%d",y);
line(170,170,400,170);
line(170,170,250,250);
line(400,170,450,250);
line(250,250,450,250);
line(170,170,100,250);
line(100,250,250,250);
line(450,250,450,350);
line(250,250,250,350);
line(100,250,100,350);
line(100,350,250,350);
line(250,350,450,350);
line(150,300,150,350);
line(200,300,200,350);
line(175,300,175,350);
line(150,300,200,300);
getch();
closegraph();
return 0; }

Shivansh Gupta
170110010
Output: -

Shivansh Gupta
170110010
Q2: - WAP in C to draw lines pixel by pixel using putpixel() function.
Solution: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int x,y,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for( i=0;i<300;i++){
delay(50);
putpixel(i,i,RED);
}
for (i=0;i<300;i++){
delay(50);
putpixel(i,50,GREEN);
}
for(i=0;i<300;i++){
delay(50);
putpixel(50,i,YELLOW);
}
getch();
closegraph();
}

Shivansh Gupta
170110010
Output: -

Shivansh Gupta
170110010
Q3: - WAP in C to display string “Welcome to Computer Graphics” using outtextxy()
function and use settextstyle() function for different styles.
Solution: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int i,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=0,j=0;i<6;i++,j=j+30){
settextstyle(i,0,2);
outtextxy(20,120+j,"Welcome to Computer");
settextstyle(i,1,2);
outtextxy(500-j,50,"Welcom to computer");
delay(100);
}
getch();
closegraph();
}
Output: -

Shivansh Gupta
170110010
Q4: - WAP in C to implement DDA algorithm.
Solution: -
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int Sign(float x){
if(x>0){
return 1;
}
else if(x==0){
return 0;
}
else{
return -1;
}
}
void main(){
int gm,gd=DETECT,i=0,a,b;
float xin,yin,x,signx,signy,x1,y1,x2,y2,dx,dy,LENGTH,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial coordinates:\n");
scanf("%f %f",&x1,&y1);
printf("Enter the final coordinates:\n");
scanf("%f %f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
printf("dx=%f\ndy=%f\n",dx,dy);
if(dx>=dy) {
LENGTH=dx;
printf("LENGTH=%f\n",LENGTH);
}

Shivansh Gupta
170110010
else{
LENGTH=dy;
printf("LENGTH=%f\n",LENGTH); }
xin=dx/LENGTH;
yin=dy/LENGTH;
signx=Sign(xin);
signy=Sign(yin);
x=x1+(0.5*signx); y=y1+(0.5*signy);
a=x+0.5; b=y+0.5;
printf("X Y\n");
printf("%f %f\n",x1,y1);
while(i<LENGTH){
putpixel(x,y,GREEN);
printf("%d %d\n",a,b);
x=x+xin; y=y+yin;
a=x+0.5; b=y+0.5;
i++;
}
getch();
closegraph(); }
Output: -

Shivansh Gupta
170110010
Q5: - WAP in C to implement Bresenham’s algorithm.
Solution: -
#include<stdio.h>

#include<conio.h>

void main(){

int x1,x2,y1,y2,dx,dy,x,y;

float t,c, p0;

int i;

clrscr();

printf("\nEnter x1 y1: ");

scanf("%d %d",&x1,&y1);

printf("\nEnter x2 y2: ");

scanf("%d %d",&x2,&y2);

x=x1; y=y1;

dx=x2-x1;

dy=y2-y1;

t=(2*dy)-(2*dx);

c=2*dy;

p0=(2*dy)-dx;

printf("\nX Y\n");

printf("%d %d\n",x,y);

for(i=0;i<dx;i++){

if(p0<0){

x++;

p0=p0+(2*dy);

printf("%d %d",x,y);

printf("\n");

else{
Shivansh Gupta
170110010
x++; y++;

p0=p0+(2*dy)-(2*dx);

printf("%d %d",x,y);

printf("\n");

getch();

Output: -

Shivansh Gupta
170110010
Q6: - WAP in C to draw a circle using mid-point algorithm.
Solution: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main() {
int gd=DETECT;
int gm;
int r,xc,yc,x,y,p;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the radius of the circle:\n");
scanf("%d",&r);
printf("Enter the coordinates of the circle:\n");
scanf("%d%d",&xc,&yc);
p=1-r; x=0; y=r;
printf("X\tY\tP\t\n");
printf("%d\t %d\t %d\n",x,y,p);
while(x<=y) {
putpixel(x+xc,y+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
if(p<0) {
x++;
p=p+(2*x)+1;
printf("%d %d %d\n",x,y,p);
}

Shivansh Gupta
170110010
else {
x++; y--;
p=p+(2*x)+1-(2*(y+2));
printf("%d %d %d\n",x,y,p);
}
}
getch();
closegraph();
}

Output: -

Shivansh Gupta
170110010
Q7: - Perform rotation of a right angle triangle ABC by θ in either clockwise/anti-clockwise
direction as given by user.
Solution: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main(){
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

x1=200; y1=200;
x2=200; y2=300;
x3=300; y3=300;

printf("x1=%d y1=%d\n",x1,y1);
printf("x2=%d y2=%d\n",x2,y2);
printf("x3=%d y3=%d\n",x3,y3);
Rotate(x1,y1,x2,y2,x3,y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3){
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

Shivansh Gupta
170110010
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3){
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2,choice;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
Angle=(Angle*3.14)/180;
printf("\nSelect 1 for Clockwise, 2 for anticlockwise:");
scanf("%d",&choice);
if(choice==2){ Angle=Angle*(-1); }
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("a1=%d b1=%d\n",a1,b1);
printf("a2=%d b2=%d\n",a2,b2);
printf("a3=%d b3=%d\n",a3,b3);
triangle(a1,b1,a2,b2,a3,b3);
}

Shivansh Gupta
170110010
Output: -

Shivansh Gupta
170110010
Q8: - WAP to make an analog clock (running) which shows current time.
Solution: -
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<time.h>

#define PI 3.147

void clockLayout();
void secHand();
void hrHand();
void minHand();
int maxx,maxy;

void main(){
int gdriver=DETECT,gmode,error;
initgraph(&gdriver,&gmode," C:\\TURBOC3\\BGI ");
error=graphresult();
if(error!=grOk) {
printf("Error in graphics, code= %d",grapherrormsg(error));
exit(0);
}

while(1) {
clockLayout();
secHand();
minHand();
hrHand();
sleep(1);
cleardevice();
}
}

void clockLayout(){
int i,x,y,r;
float j;
maxx=getmaxx();
maxy=getmaxy();
Shivansh Gupta
170110010
for(i=1;i<5;i++) {
setcolor(YELLOW);
circle(maxx/2,maxy/2,120-i);
}

pieslice(maxx/2,maxy/2,0,360,5);
x=maxx/2+100;y=maxy/2;
r=100;
setcolor(BLUE);

for(j=PI/6;j<=(2*PI);j+=(PI/6)) {
pieslice(x,y,0,360,4);
x=(maxx/2)+r*cos(j);
y=(maxy/2)+r*sin(j);
}

x=maxx/2+100;y=maxy/2;
r=100;
setcolor(RED);

for(j=PI/30;j<=(2*PI);j+=(PI/30)) {
pieslice(x,y,0,360,2);
x=(maxx/2)+r*cos(j);
y=(maxy/2)+r*sin(j);
}
}

void secHand(){
struct time t;
int r=80,x=maxx/2,y=maxy/2,sec;
float O;

maxx=getmaxx();maxy=getmaxy();
gettime(&t); sec=t.ti_sec;
O=sec*(PI/30)-(PI/2);
setcolor(YELLOW);
line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}

void hrHand(){
int r=50,hr,min;
int x,y;
struct time t;
float O;

maxx=getmaxx();
Shivansh Gupta
170110010
maxy=getmaxy();
x=maxx/2,y=maxy/2;
gettime(&t);
hr=t.ti_hour;
min=t.ti_min;

if(hr<=12)O=(hr*(PI/6)-(PI/2))+((min/12)*(PI/30));
if(hr>12) O=((hr-12)*(PI/6)-(PI/2))+((min/12)*(PI/30));
setcolor(BLUE);
line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}

void minHand(){
int r=60,min;
int x,y;
float O;
struct time t;
maxx=getmaxx();
maxy=getmaxy();
x=maxx/2;
y=maxy/2;
gettime(&t);
min=t.ti_min;
O=(min*(PI/30)-(PI/2));
setcolor(RED);
line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}

Output: -

Shivansh Gupta
170110010
Q9: - WAP to depict rising rainbow
Solution: -
#include<stdio.h>
#include<graphics.h>
void main() {
int gd=DETECT,gm,i=0,a=0;
float r=20;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
for(i=1;i<100;i++) {
if(a!=i/15)
delay(300);
a=i/15;
setcolor(a);
arc(200,150,10,170,r+i);
delay(200);
}
getch();
}

Output: -

Shivansh Gupta
170110010

You might also like