[go: up one dir, main page]

0% found this document useful (0 votes)
80 views11 pages

Graphics Algorithms in C

The document contains code snippets demonstrating various computer graphics algorithms: 1) Bresenham's line drawing algorithm for drawing lines on a raster display. 2) DDA circle drawing algorithm using incremental steps to plot points on a circle. 3) Mid-point circle drawing algorithm using midpoint calculations. 4) Code to display a pie chart showing voting percentages for different parties. 5) Functions demonstrating geometric transformations like scaling, rotation, and translation of graphical objects.

Uploaded by

VARSHA K U
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)
80 views11 pages

Graphics Algorithms in C

The document contains code snippets demonstrating various computer graphics algorithms: 1) Bresenham's line drawing algorithm for drawing lines on a raster display. 2) DDA circle drawing algorithm using incremental steps to plot points on a circle. 3) Mid-point circle drawing algorithm using midpoint calculations. 4) Code to display a pie chart showing voting percentages for different parties. 5) Functions demonstrating geometric transformations like scaling, rotation, and translation of graphical objects.

Uploaded by

VARSHA K U
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/ 11

Bresenham’s Line Drawing Algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
float x1, y1, x2, y2, dx, dy, d, x, y, incre, incrne, xend;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");
printf("enter the x1 and y1 values");
scanf("%f %f", &x1, &y1);
printf("enter the x2 and y2 values");
scanf("%f %f", &x2, &y2);
dx=x2-x1;
dy=y2-y1;
d=2*dy-dx;
incre=2*dy;
incrne=2*(dy-dx);
if(x2>x1)
{
x=x1;
y=y1;
xend=x2;
}
else
{
x=x2;
y=y2;
xend=x1;
}
while(x<xend)
{
if(d>0)
{
d=d+incre;
x=x+1;
}
else
{
d=d+incrne;
y=y+1;
x=x+1;
}
putpixel(x,y,6);
}
getch();
closegraph();
}
DDA Circle Drawing Algorithm:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float xc, yc, x, y, r, pi=3.14, t;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");
printf("enter xc, yc and radius");
scanf("%f %f %f", &xc, &yc, &r);
for(t=0; t<(2*pi); t=t+0.01)
{
x=xc+r*cos(t);
y=yc+r*sin(t);
putpixel(x,y,4);
}
getch();
closegraph();
}
Mid-Point Circle Drawing Algorithm:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float xc, yc, d, r, x, y;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");
printf("enter xc, yc and radius");
scanf("%f %f %f", &xc, &yc, &r);
x=0;
y=r;
d=1-r;
while(y>x)
{
if(d<0)
{
d=d+(2*x)+3;
}
else
{
d=d+2*(x-y)+5;
y--;
}
x++;
putpixel(x+xc, y+yc,7);
putpixel(-x+xc, y+yc,6);
putpixel(-x+xc, -y+yc,5);
putpixel(x+xc, -y+yc,5);
putpixel(y+xc, x+yc,4);
putpixel(-y+xc, x+yc,3);
putpixel(-y+xc, -x+yc,3);
putpixel(y+xc, -x+yc,2);
}
putpixel(x,y,4);
getch();
closegraph();
}
Pie-Chart Display of Voting Percentage obtained by four parties:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
piechart(int,int,int,int data[]);
static char *party[4]={"ABC", "XYZ", "PQR", "MNO"};

void main()
{
int i,xc,yc,r, gd=DETECT, gm,data[4];
clrscr();
initgraph(&gd, &gm, "C:\\TC\\BGI");
xc=getmaxx()/2;
yc=getmaxy()/2;
for(i=0;i<4;i++)
{
printf("party %d of %s\n", i+1, party[i]);
printf("Enter the number of votes\n");
scanf("%d",&data[i]);
}
printf("\n Enter the radius of pic having");
scanf("%d", &r);
cleardevice();
piechart(xc,yc,r,data);
getch();
closegraph();
}
Program to Demonstrate basic Geometric Transformations:

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

void scale(int *, int *, float, float, int, int);


void rotate(int *, int *, float, int, int);
void translate(int *, int *, int, int);

void main()
{
int gd=DETECT, gm, ch;
int x1, y1, x2, y2, x3, y3, tx, ty;
float refx, refy, theta, sx, sy;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
do
{
x1=150, y1=300, x2=250, y2=300, x3=200, y3=200, refx=200, refy=250;
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

setcolor(YELLOW);
outtextxy(140,30,"Main menu");
outtextxy(140,50,"1. Scale");
outtextxy(140,70,"2. Rotate");
outtextxy(140,90,"3. Translate");
outtextxy(140,110,"4. Exit");
outtextxy(100,150,"Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1 :
outtextxy(10,10,"enter sx and sy : ");
scanf("%f %f", &sx, &sy);
cleardevice();
//before scaling
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

//scaling to get new points


scale(&x1, &y1, sx, sy, refx, refy);
scale(&x2, &y2, sx, sy, refx, refy);
scale(&x3, &y3, sx, sy, refx, refy);
//triangle after scaling
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

getch();
break;

case 2 :
outtextxy(100,180,"Enter angle of rotation : ");
scanf("%f", &theta);
cleardevice();

//before rotation
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

//new co-ordinates
rotate(&x1, &y1, theta, refx, refy);
rotate(&x2, &y2, theta, refx, refy);
rotate(&x3, &y3, theta, refx, refy);

//after rotation
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

getch();
break;

case 3 :
outtextxy(100, 180, "Enter tx and ty : ");
scanf("%d %d", &tx, &ty);
cleardevice();

//before translation
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

//translated co-ordinates
translate(&x1, &y1, tx, ty);
translate(&x2, &y2, tx, ty);
translate(&x3, &y3, tx, ty);

//after translation
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

getch();
break;

case 4 :
break;

}
} while(ch!=4);
getch();
closegraph();
}

void scale(int *xnew, int *ynew, float sx, float sy, int refx, int refy)
{
translate(xnew, ynew, -refx, -refy);
*xnew=*xnew*sx;
*ynew=*ynew*sy;
translate(xnew, ynew, refx, refy);
}

void translate(int *xnew, int *ynew, int tx, int ty)


{
*xnew=*xnew+tx;
*ynew=*ynew+ty;
}

void rotate(int *xnew, int *ynew, float angl, int refx, int refy)
{
int *xold, *yold;
translate(xnew, ynew, -refx, -refy);
*xold=*xnew;
*yold=*ynew;
angl=angl*3.14/180; // angle converted to radians
*xnew=*xold*cos(angl)-*yold*sin(angl);
*ynew=*yold*cos(angl)+*xold*sin(angl);
translate(xnew, ynew, refx, refy);
}
Program to display Flag Movement:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flag1();
void flag2();
void flag3();

void main()
{
int gd=DETECT, gm;
int i, stangle, endangle;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
while(!kbhit())
{
cleardevice();
flag1();
delay(250);
cleardevice();
flag3();
delay(300);
cleardevice();
flag2();
delay(350);
}
getch();
closegraph();
//return();
}

void flag1()
{
int i;
setfillstyle(SOLID_FILL,6);
bar(50,50,300,100);
setfillstyle(SOLID_FILL, WHITE);
bar(50,100,300,150);
setfillstyle(SOLID_FILL, GREEN);
bar(50,150,300,200);
setfillstyle(SOLID_FILL, 8);
bar(40,20,50,460);
for(i=0;i<=360;i+=15)
pieslice(175,125,i,i+15,25);
}

void flag2()
{
int i;
int r1[8]={50,50,300,75,300,125,50,100};
int r2[8]={50,100,300,125,300,175,50,150};
int r3[8]={50,150,300,175,300,225,50,200};

setcolor(0);
setfillstyle(SOLID_FILL,6);
fillpoly(4,r1);
setfillstyle(SOLID_FILL,WHITE);
fillpoly(4,r2);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(4,r3);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);
setcolor(9);
setfillstyle(SOLID_FILL, WHITE);
for(i=0;i<=360;i+=15)
pieslice(175,137,i,i+15,25);
}

void flag3()
{
int i;
int r1[8]={50,50,300,25,300,75,50,100};
int r2[8]={50,100,300,75,300,125,50,150};
int r3[8]={50,150,300,125,300,175,50,200};

setcolor(0);
setfillstyle(SOLID_FILL,6);
fillpoly(4,r1);
setfillstyle(SOLID_FILL,WHITE);
fillpoly(4,r2);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(4,r3);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);
setcolor(9);
setfillstyle(SOLID_FILL, WHITE);
for(i=0;i<=360;i+=15)
pieslice(175,113,i,i+15,25);
}
Scan-line Area Filling:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int n,i,j,k,gd=DETECT,gm,dx,dy,x,y,temp;
int a[20][2],xi[20];
float slope[20];
clrscr();
initgraph(&gd,&gm,"C:\\tc\\bgi");
printf("enter the number of edges of polygon: \n");
scanf("%d",&n);
printf("enter the coordinate values of polygon \n");
for(i=0;i<n;i++)
scanf("%d %d",&a[i][0],&a[i][1]);
a[n][0]=a[0][0];
a[n][1]=a[0][1];

for(i=0;i<n;i++)
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
getch();

for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0)
slope[i]=1.0;
if(dx==0)
slope[i]=0.0;
if((dy!=0)&&(dx!=0))
slope[i]=(float)dx/dy;
}
for(y=0;y<400;y++)
{
k=0;
for(i=0;i<n;i++)
{
if( ((a[i][1]<=y)&&(a[i+1][1]>y)) || ((a[i][1]>y)&&(a[i+1][1]<=y)) )
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
for(j=0;j<k-1;j++)
for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(RED);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
closegraph();
}

You might also like