0% found this document useful (0 votes)
11 views16 pages

CG 8

The document discusses circle drawing techniques in computer graphics, detailing the mathematical concepts of circles, including their equations in Cartesian and polar coordinates. It provides algorithms and sample code for drawing circles using both methods, highlighting the inefficiencies of the Cartesian approach. Additionally, it introduces the ellipse function for drawing ellipses with specified parameters.

Uploaded by

Fazil Nasrat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

CG 8

The document discusses circle drawing techniques in computer graphics, detailing the mathematical concepts of circles, including their equations in Cartesian and polar coordinates. It provides algorithms and sample code for drawing circles using both methods, highlighting the inefficiencies of the Cartesian approach. Additionally, it introduces the ellipse function for drawing ellipses with specified parameters.

Uploaded by

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

Computer Graphics

Circle Drawing Techniques


Circle
 circle is the set of points in a plane that are
equidistant from a given point O.
 The distance r from the center is called the
radius, and the point O is called the center.
 Twice the radius is known as the diameter
d=2r.The angle a circle subtends from its
center is a full angle, equal to 360° or radians.
 The perimeter C of a circle is called the
circumference.
Circle Drawing Techniques
 First of all for circle drawing we need
to know what the input will be. Well
the input will be one center
point (x, y) and radius r.
 Therefore, using these two inputs there are a
number of ways to draw a circle. They involve
understanding level very simple to complex and
reversely time complexity inefficient to efficient. We
see them one by one giving comparative study.
Circle drawing using Cartesian coordinates
 This technique uses the equation for a circle on
radius r centered at (0, 0) given as:

x2 + y2 = r2

 an obvious choice is to plot

y = ± sqrt(r2 - x2)
Cont…
 Obviously in most of the cases the circle is
not centered at (0, 0), rather there is a center
point (xc, yc); other than (0, 0). Therefore the
equation of the circle having center at point
(xc, yc):
(x-xc)2 + (y-yc)2 =r2
Y= yc ± sqrt(r2 –(x-xc)2)
 Using above equation a circle can easily be
drawn. The value of x varies from r-xc to r+xc.
and y will be calculated using above formula.
Algorithm
Circle1 (xcenter, ycenter, radius)
for x = radius - xcenter to radius + xcenter
y= yc + sqrt(r2 –(x-xc)2)
Drawpixel(x,y)
y= yc - sqrt(r2 –(x-xc)2)
Drawpixel(x,y)
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
int x=0,y=0, r=30;
int x1=200,y1=200;
for(x=r-x1;x<=r+x1;x++)
{

y=y1+sqrt((r*r)- pow((x-x1),2));
putpixel(x,y,8);
y=y1-sqrt((r*r)- pow((x-x1),2));
putpixel(x,y,8);
}
getch();
return 0;
}
 Using Cartesian coordinate inefficient
because of the multiplications and square
root operations.
 It also creates large gaps in the circle.
Circle drawing using Polar
coordinates
A better approach, to draw circle using
polar coordinates r and θ.
 Expressing the circle equation in
parametric polar form yields the pair of
equations
x = xc + r cos θ
y = yc + r sin θ
 Using above equation circle can be plotted
by calculating x and y coordinates as θ
takes values from 0 to 360 degrees or 0 to
2π.
Circle2 (xcenter, ycenter, radius)
for θ = 0 to 2π step 1
x = xc + r * cos θ
y = yc + r * sin θ
drawPixel (x, y)
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
int x=0,y=0, r=100;
int x1=200,y1=200;
for(double Q=0;Q<=360;++Q)///r))
{
x=x1 + round(r*cos(Q));
y=y1 + round(r*sin(Q));
putpixel(x,y,8);
}
getch();
return 0;
}
The ellipse Function
 Declarations of ellipse function :-
void ellipse(int x, int y, int stangle,
int endangle, int xradius, int yradius);
 Ellipse is used to draw an ellipse (x,y) are
coordinates of center of the ellipse, stangle is
the starting angle, end angle is the ending angle,
and fifth and sixth parameters specifies the X
and Y radius of the ellipse. To draw a complete
ellipse strangles and end angle should be 0 and
360 respectively
Example
#include<math.h>
#include<iostream.h>
int main()
{
int d = DETECT, m;
initgraph(&d, &m, "");
ellipse(200, 200, 0, 360, 100, 50);

getch();
closegraph();
return 0;
}
#include<graphics.h>
#include<conio.h>

main()
{
int d = DETECT,m,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;

initgraph(&d, &m, "");

rectangle(left, top, right, bottom);


circle(x, y, radius);
bar(left + 300, top, right + 300, bottom);
line(left - 10, top + 150, left + 410, top + 150);
ellipse(x, y + 200, 0, 360, 100, 50);
outtextxy(left + 100, top + 325, "My Graphics Program");

getch();
closegraph();
return 0;
}

You might also like