Monday, July 1, 2013

Plotting conical curves - circles

Conical curves are very known geometric spaces based on section of cones. There are four conical curves:
  • circle;
  • ellipse;
  • hyperbole;
  • parabola.

All of them are very used in computer graphics and, in this post, I wish to present how to plot a circle.

The analytic equation of circles is: (x - xc)² + (y - yc)² = r² where (xc, yc) means the center of the circle and r is it radius.

We know cos²(a) + sin²(a) = 1, for any angle a, thus multiplying both sides of equation by we obtain (r cos(a))² + (r sin(a))² = r².

Comparing original equation with obtained equation, it's possible to verify that:

x - xc = r cos (a)
x = r cos(a) + xc

y - yc = r sin(a)
y = r sin(a) + yc


So, we have now the values for x and y depending only of the center of the circle, it radius and an angle a.

(xc, yc) and r are given by the circle and a is an angle that means a dummy variable in range [0, 2*%pi].


Now, let's write some code in Scilab.

By first, a circle of radius r = 2 and center (xc, yc) = (0, 0).


//center of the circle
xc = 0;
yc = 0;

//radius of the circle
r = 2;

//dummy variable for angle in range [0, 2*%pi]
a = linspace(0, 2*%pi, 100);

//x axis
x = xc + r*cos(a);

//y axis
y = yc + r*sin(a);

//plot the circle
plot(x, y);


The result of this code is the following picture.

Take attention the limits of the circle are (-2, 2) both for x and y axis, what means this circle is centered in (0,0) with radius 2.


And for finishing, a harder example, four circles with different centers and radius.

/////////////////////////////////////////
//center of the circle
xc = 6;
yc = 0;

//radius of the circle
r = 6;

//dummy variable for angle in range [0, 2*%pi]
a = linspace(0, 2*%pi, 100);

//x axis
x = xc + r*cos(a);

//y axis
y = yc + r*sin(a);

//plot the circle
plot(x, y);

////////////////////////////////////////
//center of the circle
xc = 6;
yc = 0;

//radius of the circle
r = 2;

//dummy variable for angle in range [0, 2*%pi]
a = linspace(0, 2*%pi, 100);

//x axis
x = xc + r*cos(a);

//y axis
y = yc + r*sin(a);

//plot the circle
plot(x, y);

////////////////////////////////////////
//center of the circle
xc = 2;
yc = 0;

//radius of the circle
r = 2;

//dummy variable for angle in range [0, 2*%pi]
a = linspace(0, 2*%pi, 100);

//x axis
x = xc + r*cos(a);

//y axis
y = yc + r*sin(a);

//plot the circle
plot(x, y);

////////////////////////////////////////
//center of the circle
xc = -3;
yc = 3;

//radius of the circle
r = 3;

//dummy variable for angle in range [0, 2*%pi]
a = linspace(0, 2*%pi, 100);

//x axis
x = xc + r*cos(a);

//y axis
y = yc + r*sin(a);

//plot the circle
plot(x, y);

And the result is:


Take attention the parameters of center and radius of each circle and the obtained picture.