0% found this document useful (0 votes)
34 views

cg

Uploaded by

rohitkoslia509
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)
34 views

cg

Uploaded by

rohitkoslia509
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/ 53

Practical No - l

Write a program to draw a line using Digital Differential Analyzer


(DDA) algorithm.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

{int input;

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

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\TC\\BGI");

printf("\nEnter the x-coordinates of the first point");

scanf("%d",&x1);

printf("\nEnter the y-coordinates of the first point");

scanf("%d",&y1);

printf("\nEnter the x-coordinates of the second point");

scanf("%d",&x2);

printf("\nEnter the y-coordinates of the second point");

scanf("%d",&y2);

x=x1; y=y1;

dx=x2-x1;

dy=y2-y1;

putpixel(x,y,2);

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

while(x<=x2)

if(p<2){

x=x+1;
p=2*x-dx;

else{

x=x+1;

y=y+1;

p=p+2*dy;

putpixel(x,y,7);

getch();

closegraph() ;

}
Output-
Practical No -2
Write a program to draw a line using Bresenham's Line (BLA) algorithm.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
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()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}
Output-
Practical No -3
Write a program to draw a circle using Bresenham's Circle Algorithm.

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

void EightWaySymmetricPlot(int xc,int yc,int x,int y)

putpixel(x+xc,y+yc,RED);

putpixel(x+xc,-y+yc,YELLOW);

putpixel(-x+xc,-y+yc,GREEN);

putpixel(-x+xc,y+yc,YELLOW);

putpixel(y+xc,x+yc,12);

putpixel(y+xc,-x+yc,14);

putpixel(-y+xc,-x+yc,15);

putpixel(-y+xc,x+yc,6);

void BresenhamCircle(int xc,int 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)

/* request auto detection */

int xc,yc,r,gdriver = DETECT, gmode, errorcode;

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* terminate with an error code */

printf("Enter the values of xc and yc :");

scanf("%d%d",&xc,&yc);

printf("Enter the value of radius :");


scanf("%d",&r);

BresenhamCircle(xc,yc,r);

getch();

closegraph();

return 0;

}
Output-
Practical No -4
Write a program to draw a circle using Mid Point Circle Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, 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()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\TC\\BGI");

printf("Enter radius of circle: ");


scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");


scanf("%d%d", &x, &y);
drawcircle(x, y, r);
getch();
return 0;
}
Output-
Practical No -5
Write a program to draw an ellipse using Mid Point Ellipse Algorithm.
#include<stdio.h>
#include<graphics.h>
void main(){
long x,y,x_center,y_center;
long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
int g_driver=DETECT,g_mode;
clrscr();

initgraph(&g_driver,&g_mode,"C:\\TC\\BGI");
printf("********* MID POINT ELLIPSE ALGORITHM *********");
printf("\n\n Enter coordinate x and y = ");
scanf("%ld%ld",&x_center,&y_center);
printf("\n Now enter constants a and b = ");
scanf("%ld%ld",&a,&b);
x=0;
y=b;
a_sqr=a*a;
b_sqr=b*b;
fx=2*b_sqr*x;
fy=2*a_sqr*y;
d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
do
{
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d<0)
{
d=d+fx+b_sqr;
}
else
{
y=y-1;
d=d+fx+-fy+b_sqr;
fy=fy-(2*a_sqr);
}
x=x+1;
fx=fx+(2*b_sqr);
delay(10);

}
while(fx<fy);
tmp1=(x+0.5)*(x+0.5);
tmp2=(y-1)*(y-1);
d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
do
{
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);

if(d>=0)
d=d-fy+a_sqr;
else
{
x=x+1;
d=d+fx-fy+a_sqr;
fx=fx+(2*b_sqr);
}
y=y-1;
fy=fy-(2*a_sqr);
}
while(y>0);
getch();
closegraph();
}
Output-
Practical No -6
Write a program to draw a rectangle shape using Flood Fill algorithm.
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
void floodfill(intx,inty,intold,intnewcol)
{
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()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(50,50,150,150);
floodfill(70,70,0,15);
getch();
closegraph();
}
Output-
Practical No -7
Write a program to draw a circle shape using Boundary Fill algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>

// Function for 4 connected Pixels


void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}

//driver code
int main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, "C:\\TC\\BGI");

int x = 250, y = 200, radius = 50;


circle(x, y, radius);
boundaryFill4(x, y, 6, 15);
getch();
closegraph();
return 0;
}
Output-
Practical No -8
Write a program to demonstrate window to view port mapping.

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main ()
{
int W_xmax, W_ymax, W_xmin, W_ymin;
int V_xmax, V_ymax, V_xmin, V_ymin;
float sx, sy;
int x, x1, x2, y, y1, y2;
int gr = DETECT, gm;
initgraph (&gr, &gm, "C:\\TC\\BGI");
printf ("\n****** Window to Viewport ***********\n");
printf ("Enter the coordinates for triangle \n x and y = ");
scanf ("%d %d", &x, &y);
printf ("\n x1 and y1 = ");
scanf ("%d %d", &x1, &y1);
printf ("\n x2 and y2 = ");
scanf ("%d %d", &x2, &y2);
printf ("Please enter Window coordinates \n First enter XMax, YMax =");
scanf ("%d %d", &W_xmax, &W_ymax);
printf ("\n Now, enter XMin, YMin =");
scanf ("%d %d", &W_xmin, &W_ymin);
cleardevice ();
delay (50);
//Window
rectangle (W_xmin, W_ymin, W_xmax, W_ymax);
outtextxy (W_xmin, W_ymin - 10, "Window");
//drawing a triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
// viewport
V_xmin = 300;
V_ymin = 30;
V_xmax = 550;
V_ymax = 350;
rectangle (V_xmin, V_ymin, V_xmax, V_ymax);
outtextxy (V_xmin, V_ymin - 10, "Viewport");
// calculatng Sx and Sy
sx = (float) (V_xmax - V_xmin) / (W_xmax - W_xmin);
sy = (float) (V_ymax - V_ymin) / (W_ymax - W_ymin);
x = V_xmin + (float) ((x - W_xmin) * sx);
x1 = V_xmin + (float) ((x1 - W_xmin) * sx);
x2 = V_xmin + (float) ((x2 - W_xmin) * sx);
y = V_ymin + (float) ((y - W_ymin) * sy);
y1 = V_ymin + (float) ((y1 - W_ymin) * sy);
y2 = V_ymin + (float) ((y2 - W_ymin) * sy);
// drawing triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
getch ();
closegraph ();
}
Output-
Practical No -9
Write a program to clip a line segment using 4-bit code algorithm.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

typedef struct coordinate


{
int x,y;
char code[4];
}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);

void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
printf("\nEnter x1 and y1\n");

scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:\\TC\\BGI");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}

void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

int visibility(PT p1,PT p2)


{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}

PT resetendpt(PT p1,PT p2)


{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return (temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Output-
Practical No -10
Write a program to draw a C-Curve of nth order.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
using namespace std;
void bezier_curve(const int[8]);
double nCr(int,int);
double factorial(int);
void dashed_line(const int,const int,const int,const int,const int=0);
int main(){
int gd=DETECT,gm;
int control_points[8]={0};
for(int count=0;count<=3;count++) {
cout<<“coordinates of point–“<<count<<” (x”<<count<<“,y”<<count<<“)
:”<<endl;
cout<<“IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII”<<endl;
cout<<“Enter tyhe value of x”<<count<<” = “<<endl;
cin>>control_points[(count*2)];
cout<<“Enter tyhe value of y”<<count<<” = “<<endl;
cin>>control_points[((count*2)+1)];
cout<<” “<<endl;
cout<<” “<<endl;
cout<<” “<<endl;}
initgraph(&gd,&gm,””);
setcolor(15);
bezier_curve(control_points);
getch();
closegraph();
return 0;}
void bezier_curve(const int cp[8]){
int color=getcolor();
setcolor(7);
for(int count=0;count<3;count++)
dashed_line(cp[(count*2)],cp[((count*2)+1)],cp[((count+1)*2)],cp[(((count+1)*2)+1)]
);
float x;
float y;
for(float u=0.0005;u<=1;u+=0.0005){
float x=0;
float y=0;
for(int k=0;k<=3;k++){
x+=(cp[(k*2)]*nCr(3,k)*pow(u,k)*pow((1-u),(3-k)));
y+=(cp[((k*2)+1)]*nCr(3,k)*pow(u,k)*pow((1-u),(3-k))); }
putpixel((int)(x+0.5),(int)(y+0.5),color);
}}
double nCr(int n,int r){
double nf,rf,nrf,ncr;
nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));
ncr=(nf/(rf*nrf));
return ncr;}
double factorial(int number){
double factorial=1;
if(number==0 || number==1);
else {
for(int count=1;count<=number;count++)
factorial=factorial*count;}
return factorial;}
void dashed_line(const int x_1,const int y_1,const int x_2,const int y_2,const int
line_type){
int count=0;
int color=getcolor();
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>=x_2){
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1; }
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy) {
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2) {
x++;
if(p<0)
p+=two_dy;
else {
y+=inc_dec;
p+=two_dy_dx; }
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 && (count%12)!=10 &&
(count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++; } }
else{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2) {
y+=inc_dec;
if(p<0)
p+=two_dx;
else{
x++;
p+=two_dx_dy; }
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 && (count%12)!=10 &&
(count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}}}
Output-
Practical No -11
Write a program to shows a scene of flying kite.

#include<stdio.h>

#include<time.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

#include<dos.h>

void main()

int gd=DETECT,gm;

int x=10,y=480;

initgraph(&gd,&gm,"..\\bgi");

while(!kbhit())

cleardevice();

if(y==0)

y=random(480);

x=random(640);

else
{

y=y-1;

x=x+1;

line(x-50,y,x,y-70);

line(x,y-70,x+50,y);

line(x+50,y,x,y+70);

line(x,y+70,x-50,y);

line(x,y-70,x,y+70);

line(x,y+70,x+10,y+140);

line(x,y+70,x-10,y+140);

line(x-50,y,x+50,y);

line(x,y,x+130,y+640);

delay(20);

closegraph();

restorecrtmode();

}
Output-
Practical No -12
Write a program to rotate a line about it's mid point.

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int gd=DETECT,gm;

int pivot_x,pivot_y,x,y;

double degree,radian;

int rotated_point_x,rotated_point_y;

initgraph(&gd,&gm,"C://TC//BGI");

cleardevice();

printf("\t\t*********** ROTATION *********** \n");

printf("\n Enter an initial coordinates of the line = ");

scanf("%d %d",&pivot_x,&pivot_y);

printf("\n Enter a final coordinates of the line = ");

scanf("%d %d",&x,&y);

line(pivot_x,pivot_y,x,y);

printf("\n\n Now, Enter a degree = ");

scanf("%lf",&degree);

radian=degree*0.01745;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));

rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));

setcolor(RED);

line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);

getch();

closegraph();

}
Output-
Practical No -13
Write a program that shows a scene of eclipse.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>

int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Initialize center of ellipse with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;

outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");


/* Draw ellipse on screen */
ellipse(x, y, 0, 360, 120, 60);

getch();
closegraph();
return 0;
}
Output-
Practical No -14
Write a program that translate and rotate a circle along a horizontal
line.

Output-
Practical No -15
Write a program to rotate an ellipse about its axis alternatively.
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


void drawEllipse(int xc, int yc, int a, float alpha, int color)
{
float t = 3.14 / 180;
alpha = 360 - alpha;
setcolor(color);
int theta;
for (int i = 0; i < 360; i += 1) {
theta = i;
int x = a * cos(t * theta) * cos(t * alpha)
+ b * sin(t * theta) * sin(t * alpha);

int y = b * sin(t * theta) * cos(t * alpha)


- a * cos(t * theta) * sin(t * alpha);

putpixel(xc + x, yc - y, color);
}
}

void slidePattern(int xc, int yc, int r, int a, int b,int alpha, float p, int color)
{
setcolor(color);
float t = 3.14 / 180;
float t1, t2, d;
float angle = (p * alpha);

t1 = cos(t * fmod(angle, 360));


t2 = sin(t * fmod(angle, 360));
t1 *= t1;
t2 *= t2;
t1 = t1 / (a * a);
t2 = t2 / (b * b);
d = sqrt(t1 + t2);
d = 1 / d;

int draw_x = xc + (r + d) * cos(t * alpha);


int draw_y = yc - (r + d) * sin(t * alpha);
int draw_ang = angle + alpha;

drawEllipse(draw_x, draw_y, a,
b, draw_ang, color);
}
void ellipseovercircle(int xc, int yc,
int r, int a, int b)
{
float theta = 0;
double h, p1;

h = (a * a) + (b * b);
h /= 2;
p1 = sqrt(h);
p1 /= r;
p1 = 1 / (p1);

// by decreasing theta we can


// move Ellipse clockwise
for (;; theta -= 1) {
slidePattern(xc, yc, r, a, b,
theta, p1, WHITE);

circle(xc, yc, r); // Drawing Circle


delay(25); // Introducing delay

// Erase the existing Ellipse


slidePattern(xc, yc, r, a, b,
theta, p1, BLACK);
}
}

// Driver code
int main()
{
// Initialize graphics function
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

// maximum X-coordinate for the window


int maxx = getmaxx();
// maximum Y-coordinate for the window
int maxy = getmaxy();

// Start drawing from the mid of the screen


ellipseovercircle(maxx / 2, maxy / 2,
100, 40, 28);

closegraph();
return 0;
}
Output-

You might also like