0% found this document useful (0 votes)
10 views26 pages

CG Assignment 2

Uploaded by

royal.kingz1707
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)
10 views26 pages

CG Assignment 2

Uploaded by

royal.kingz1707
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/ 26

NAME = ROHAN SHINDE

ROLL NO = 91
DIV = B (B1 BATCH)
SUBJECT = CG

• Aim:- 1. Menu driven program using bresenham & mid-point circle


algorithm.

#include<iostream>
#include <graphics.h>
using namespace std;
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
void circleBres(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircle(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircle(xc,yc,x,y);
}
};
void Midpoint(int xc,int yc,int r)
{
int p,x,y;
x=0;
y=r;
p=1-r;
drawCircle(xc,yc,x,y);
while(x<y)
{
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
drawCircle(xc,yc,x,y);
}
};
int main()
{
int gd = DETECT, gm;
int ch,xc,yc,r1;
cout<<"\n1.Bresenham circle\n2.Mid-point circle\n3.Exit\nEnter
your choice:\n";
cin>>ch;
if(ch==1)
{
cout<<"\nEnter center of the circle:\n";
cin>>xc>>yc;
cout<<"\nEnter radius of the circle:\n";
cin>>r1;
initgraph(&gd, &gm,(char)*"");
circleBres(xc,yc,r1);
getch();
closegraph();
return 0;
}
if(ch==2)
{
cout<<"\nEnter center of the circle:\n";
cin>>xc>>yc;
cout<<"\nEnter radius of the circle:\n";
cin>>r1;
initgraph(&gd, &gm,(char)*"");
Midpoint(xc,yc,r1);
getch();
closegraph();
return 0;
}
if(ch==3)
{
exit(0);
}
}

OUTPUT –
Conclusion:- By using Bresenham & Mid-point circle algorithm we
can draw Circle!

• AIM:- 2)Draw shapes using bresenham & mid-point circle


algorithms.

#include<iostream>
#include <graphics.h>
using namespace std;
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
void circleBres(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircle(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircle(xc,yc,x,y);
}
}
int main()
{
int xc,yc,r;
int gd=DETECT,gm;
initgraph(&gd,&gm,(char)*"");
outtextxy(100,10,"Doremon");
circleBres(300,300,150);
circleBres(300,75,75);
circleBres(210,30,28);
circleBres(390,30,28);
circleBres(300,90,40);
circleBres(300,80,15);
circleBres(260,65,10);
circleBres(340,65,10);
getch();
closegraph();
return 0;
}

OUTPUT –
//OLYMPIC SYMBOL
#include<iostream>
#include <graphics.h>
using namespace std;
void drawCircleB(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,BLUE);
putpixel(xc-x, yc+y,BLUE);
putpixel(xc+x, yc-y,BLUE);
putpixel(xc-x, yc-y,BLUE);
putpixel(xc+y, yc+x,BLUE);
putpixel(xc-y, yc+x,BLUE);
putpixel(xc+y, yc-x,BLUE);
putpixel(xc-y, yc-x,BLUE);
}
void drawCircleR(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,RED);
putpixel(xc-x, yc+y,RED);
putpixel(xc+x, yc-y,RED);
putpixel(xc-x, yc-y,RED);
putpixel(xc+y, yc+x,RED);
putpixel(xc-y, yc+x,RED);
putpixel(xc+y, yc-x,RED);
putpixel(xc-y, yc-x,RED);
}
void drawCircleG(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,2);
putpixel(xc-x, yc+y,2);
putpixel(xc+x, yc-y,2);
putpixel(xc-x, yc-y,2);
putpixel(xc+y, yc+x,2);
putpixel(xc-y, yc+x,2);
putpixel(xc+y, yc-x,2);
putpixel(xc-y, yc-x,2);
}
void drawCircleY(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,YELLOW);
putpixel(xc-x, yc+y,YELLOW);
putpixel(xc+x, yc-y,YELLOW);
putpixel(xc-x, yc-y,YELLOW);
putpixel(xc+y, yc+x,YELLOW);
putpixel(xc-y, yc+x,YELLOW);
putpixel(xc+y, yc-x,YELLOW);
putpixel(xc-y, yc-x,YELLOW);
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
void circleBresR(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleR(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleR(xc,yc,x,y);
}
}
void circleBresB(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleB(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleB(xc,yc,x,y);
}
}
void circleBresG(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleG(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleG(xc,yc,x,y);
}
}
void circleBresY(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleY(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleY(xc,yc,x,y);
}
}
void circleBres(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircle(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircle(xc,yc,x,y);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,(char)*"");
circleBresB(100,100,50);
circleBres(220,100,50);
circleBresR(340,100,50);
circleBresY(160,150,50);
circleBresG(280,150,50);
outtextxy(150,20,"OLYMPIC SYMBOL");
getch();
closegraph();
return 0;
}
//CONCENTRIC CIRCLES
#include<iostream>
#include <graphics.h>
using namespace std;
void drawCircleB(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,BLUE);
putpixel(xc-x, yc+y,BLUE);
putpixel(xc+x, yc-y,BLUE);
putpixel(xc-x, yc-y,BLUE);
putpixel(xc+y, yc+x,BLUE);
putpixel(xc-y, yc+x,BLUE);
putpixel(xc+y, yc-x,BLUE);
putpixel(xc-y, yc-x,BLUE);
}
void drawCircleR(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,RED);
putpixel(xc-x, yc+y,RED);
putpixel(xc+x, yc-y,RED);
putpixel(xc-x, yc-y,RED);
putpixel(xc+y, yc+x,RED);
putpixel(xc-y, yc+x,RED);
putpixel(xc+y, yc-x,RED);
putpixel(xc-y, yc-x,RED);
}
void drawCircleG(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,2);
putpixel(xc-x, yc+y,2);
putpixel(xc+x, yc-y,2);
putpixel(xc-x, yc-y,2);
putpixel(xc+y, yc+x,2);
putpixel(xc-y, yc+x,2);
putpixel(xc+y, yc-x,2);
putpixel(xc-y, yc-x,2);
}
void drawCircleY(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y,YELLOW);
putpixel(xc-x, yc+y,YELLOW);
putpixel(xc+x, yc-y,YELLOW);
putpixel(xc-x, yc-y,YELLOW);
putpixel(xc+y, yc+x,YELLOW);
putpixel(xc-y, yc+x,YELLOW);
putpixel(xc+y, yc-x,YELLOW);
putpixel(xc-y, yc-x,YELLOW);
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
void circleBresR(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleR(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleR(xc,yc,x,y);
}
}
void circleBresB(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleB(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleB(xc,yc,x,y);
}
}
void circleBresG(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleG(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleG(xc,yc,x,y);
}
}
void circleBresY(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircleY(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircleY(xc,yc,x,y);
}
}
void circleBres(int xc, int yc, int r)
{
int x=0,y=r;
int d=3-2*r;
drawCircle(xc,yc,x,y);
while (y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
drawCircle(xc,yc,x,y);
}
}
int main()
{
int i,r;
r=40;
int gd = DETECT, gm;
initgraph(&gd, &gm,(char)*"");
circleBres(200,200,50);
circleBresY(200,200,80);
circleBresG(200,200,110);
circleBresR(200,200,140);
circleBresB(200,200,170);
outtextxy(150,20,"CONCENTRIC CIRCLES");
getch();
closegraph();
return 0;
}

OUTPUT –

You might also like