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

Graphic Project

The document describes three algorithms: 1) The midpoint circle drawing algorithm for drawing circles using Bresenham's algorithm. 2) Bresenham's line drawing algorithm for drawing lines on a discrete coordinate system like a display. 3) The DDA line drawing algorithm which uses incremental steps along the x and y axes to calculate and plot pixels of a line on a 2D graph.

Uploaded by

Preeti singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Graphic Project

The document describes three algorithms: 1) The midpoint circle drawing algorithm for drawing circles using Bresenham's algorithm. 2) Bresenham's line drawing algorithm for drawing lines on a discrete coordinate system like a display. 3) The DDA line drawing algorithm which uses incremental steps along the x and y axes to calculate and plot pixels of a line on a 2D graph.

Uploaded by

Preeti singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Midpoint circle drawing algorithm

#include<stdio.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:\\turboc3\\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);

return 0;

}
Bresenham’s line drawing algorithm
#include<stdio.h>

#include<graphics.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:\\turboc3\\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);

return 0;

}
Indian Flag
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\Turboc3\\BGI"); // Write the path of BGI folder

//Flag Rectangle

setfillstyle(1,LIGHTRED);

bar(200,50,450,100);

setfillstyle(1,WHITE);

bar(200,100,450,150);

setfillstyle(1,GREEN);

bar(200,150,450,200);

//Ashoka Chakra

setcolor(BLUE);

circle(320,125,25);

setfillstyle(1,BLUE);

fillellipse(320,125,15,15);

//Hoist(To Support a Flag)

setfillstyle(1,BROWN);

bar(190,40,200,430);

//Ground Part

setfillstyle(1,BROWN);

bar(170,430,220,450);

bar(140,450,250,480);
getch();

closegraph();

}
Sierpinski triangle Fractal
#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

void sierpinski(void)

char d;

int i;

int x1,y1,x2,y2;

x1=x2=320;

y1=y2=0;

for(i=0;i<10000;i++)

d=random(3);

if(d==0)

x1=(x2+320)/2;

y1=(y2+0)/2;

else

if(d==1)

x1=(x2+0)/2;

y1=(y2+480)/2;

else

if(d==2)

x1=(x2+640)/2;

y1=(y2+480)/2;
}

putpixel(x1,y1,WHITE);

x2=x1;

y2=y1;

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); //Write the Path of BGI folder

sierpinski(); // Call Sierpinski Function

getch();

closegraph();

}
Rotating wheel
#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <time.h>

#include <graphics.h>

#include <dos.h>

void calcPoints(int radius, int midx, int midy, int x[12], int y[12]) {

int x1, y1;

x[0] = midx, y[0] = midy - radius;

x[6] = midx, y[6] = midy + radius;

x[3] = midx + radius, y[3] = midy;

x[9] = midx - radius, y[9] = midy;

x1 = (int) ((radius / 2) * sqrt(3));

y1 = (radius / 2);

x[2] = midx + x1, y[2] = midy - y1;

x[4] = midx + x1, y[4] = midy + y1;

x[8] = midx - x1, y[8] = midy + y1;

x[10] = midx - x1, y[10] = midy - y1;

x1 = radius / 2;

y1 = (int) ((radius / 2) * sqrt(3));


x[1] = midx + x1, y[1] = midy - y1;

x[5] = midx + x1, y[5] = midy + y1;

x[7] = midx - x1, y[7] = midy + y1;

x[11] = midx - x1, y[11] = midy - y1;

return;

int main() {

int gdriver = DETECT, gmode, err;

int i, j, midx, midy, radius;

int x[12], y[12];

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

err = graphresult();

if (err != grOk) {

printf("Graphics Error: %s",

grapherrormsg(err));

return 0;

/* mid position at x-axis & y-axis correspondingly */

midx = getmaxx() / 2;

midy = getmaxy() / 2;

/* radius of rim */
radius = 200;

/* finds the position of spokes */

calcPoints(radius, midx, midy, x, y);

/* drawing rim of the wheel */

setlinestyle(SOLID_LINE, 1, 3);

circle(midx, midy, radius);

while (!kbhit()) {

setcolor(WHITE);

/* spokes at angle 30, 90, 150, 210, 270, 330 */

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

if (i % 2 == 0) {

line(midx, midy, x[i], y[i]);

delay(100);

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

setcolor(BLACK);

if (i % 2 == 0) {

line(midx, midy, x[i], y[i]);

setcolor(WHITE);

putpixel(x[i], y[i], WHITE);

}
setcolor(WHITE);

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

if (i % 2 != 0) {

line(midx, midy, x[i], y[i]);

delay(100);

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

setcolor(BLACK);

if (i % 2 != 0) {

line(midx, midy, x[i], y[i]);

setcolor(WHITE);

putpixel(x[i], y[i], WHITE);

getch();

closegraph();

return 0;

}
SUNFLOWER
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");

setfillstyle(HATCH_FILL,BROWN);
circle(300,230,100);
floodfill(300,230,WHITE);

setfillstyle(SOLID_FILL,YELLOW);
arc(455,230,215,145,80);
floodfill(445,230,WHITE);

setfillstyle(SOLID_FILL,YELLOW);
arc(300,85,313,230,80);
floodfill(300,85,WHITE);

setfillstyle(SOLID_FILL,YELLOW);
arc(150,235,40,325,80);
floodfill(150,235,WHITE);

setfillstyle(SOLID_FILL,YELLOW);
arc(295,380,125,50,80);
floodfill(295,380,WHITE);
setfillstyle(SOLID_FILL,GREEN);
arc(372,495,270,89,200);
arc(355,515,270,90,200);
floodfill(510,380,WHITE);

getch();
closegraph();
}
DDA_line Drawing
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int graphdriver, graphmode, i;
float x,y,x1,y1,x2,y2,dx,dy,delx,dely,length;

/* Input end points (coordinates) of line segment*/


printf("Enter the coordinates of end points:");
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);

/* Initialize graphics mode*/


detectgraph(&graphdriver,&graphmode);
initgraph(&graphdriver,&graphmode,"C:\\TURBOC3\\BGI");

dx = x2-x1; /*calculate dx and dy */


dy = y2-y1;

if(dx >= dy) /*calculate line length estimate*/


length = dx;
else
length = dy;

delx=dx/length; /*calculate delta x and y*/


dely=dy/length;
x=x1+0.5; /* initialize initial points to round the values*/
y=y1+0.5;
i=1;

/* Obtain the next pixel and paint*/


while(i<=length)
{
putpixel(x,y,15);
x=x+delx;
y=y+dely;
i=i+1;
delay(50);
}
getch();
closegraph(); /*close graphics mode */
}
2D transformation
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <dos.h>
#define PI 3.14

float cord[10][3], ans[10][3]; /* storage for coordinate vertices*/


int n; /* number for defining coordinate vertices*/
float tMat[3][3]={1,0,0,
0,1,0,
0,0,1 };

/*obtain the defining vertices of the figure for transformation*/


void getPoints()
{
int i;
printf("\nEnter the no of defining vertices: ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter coordinate (%d)of the defining figure: ",i+1);
scanf("%f %f",&cord[i][0],&cord[i][1]);
cord[i][2]=1;
}
}
/* draw the polygon with coordinates*/
void drawPolygon(float ans[][3])
{
int i;
line(getmaxx()/2, 0, getmaxx()/2, getmaxy());
line(0,getmaxy()/2, getmaxx(), getmaxy()/2);
for(i=0;i<n-1;i++)
line(getmaxx()/2+ans[i][0],getmaxy()/2-ans[i][1],getmaxx()/2+ans[i+1][0],
getmaxy()/2-ans[i+1][1]);
line(getmaxx()/2+ans[0][0],getmaxy()/2-ans[0][1],getmaxx()/2+ans[i][0],getmaxy()/2-
ans[i][1]);
}

/* Routine for mattrix multiplication*/


void mul(float mat1[][3],int n,float mat2[][3],float ans[][3])
{
int i,j,k;
float sum;
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
sum+=mat1[i][k]*mat2[k][j];
ans[i][j]=sum;
}
}
}

/* Perform translation with factors tx and ty*/


void translate(int tx,int ty,int draw)
{
int i;
tMat[2][0]=tx;
tMat[2][1]=ty;

mul(cord,n,tMat,ans);
if(draw) drawPolygon(ans);
}

/*Perform scaling with factors sx and sy*/


void scale(float sx,float sy)
{
tMat[0][0]=sx;
tMat[1][1]=sy;

mul(cord,n,tMat,ans);
drawPolygon(ans);
}
/*Perform reflection about x-axis/ y-axis*/
void reflect(char axis)
{
if(axis=='x')
tMat[1][1]=-1;
else
tMat[0][0]=-1;
mul(cord,n,tMat,ans);
drawPolygon(ans);
}

/* Perform rotation with reference coord by an angle ‘angle’*/


void rotate(float angle)
{
int i;

angle*=PI/180;
tMat[0][0]=cos(angle);
tMat[0][1]=sin(angle);
tMat[1][0]=-sin(angle);
tMat[1][1]=cos(angle);

mul(cord,n,tMat,ans);
drawPolygon(ans);
}

/* main routine integrating transformations scaling, translation, reflection and rotation */


void main()
{
/* define storage variables*/
float sx,sy,tx,ty,r;
int ctr,i, choice, gdriver = DETECT, gmode;
char axis;
fflush(stdin);
/* detect an initialize graphics mode*/
detectgraph(&gdriver,&gmode);
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Choose from the following 2D transformations\n");


printf("1- Translation\n");
printf("2- Scaling\n");
printf("3- Rotation \n");
printf("4- Reflection about x-axis\n");
printf("5- Reflection about y-axis\n");
printf("0- Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
getPoints(); /* obtain coordinates for defining figure*/
printf("\nEnter the translating co-ordinates: Tx Ty");
scanf("%f %f",&tx,&ty);
cleardevice();
drawPolygon(cord);
delay(10000); /* draw the polygon*/
translate(tx,ty,1);
getch();
break;
case 2 : getPoints(); /* obtain coordinates for defining figure*/
printf("\nEnter the Scaling factors: Sx Sy");
scanf("%f %f",&sx,&sy);
cleardevice();
drawPolygon(cord); /* draw the polygon*/
delay(10000);
cleardevice();
scale(sx,sy);
getch();
break;
case 3: getPoints(); /* obtain coordinates for defining figure*/
printf("\nEnter the Rotating angle:");
scanf("%f",&r);
cleardevice();
drawPolygon(cord); /* draw the figure*/
delay(10000);
cleardevice();
rotate(r);
getch();
break;
case 4: getPoints();
cleardevice();
drawPolygon(cord); /* draw the figure */
axis='x';
delay(10000);
reflect(axis);
getch();
break;
case 5: getPoints();
cleardevice();
drawPolygon(cord); /* draw the figure*/
axis='y';
delay(10000);
reflect(axis);
getch();
break;
case 0 : exit(0);
}
}
RAINBOW

#include<stdio.h>
#include<graphics.h>;
#include<dos.h>;

void rainbow()
{
int gdriver = DETECT ,gmode;
int x,y,i;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
x=getmaxx()/2;
y=getmaxx()/2;
for(i=30; i<200; i++)
{
delay(100);
setcolor(i/10);
arc(x,y,0,180,i-10);
}
}
int main()
{rainbow();
return 0;
}
Institute of
Mathematics
& Application
Computer Graphic project

Submitted By-
Pritesh kumar
Roll no-2017b012

You might also like