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.

5 comments:

tcl said...

Hi Alex, nice post.

I just saw your messages posted in my blog early this year, http://scilab-imageprocessing.blogspot.com/

I am still not used to read the comments, hence I've disable it now and thinking of adding some gadget to sent the email direct to my account.

yes, I am still using Scilab but life is too busy for me to keeping bloging. I would try to spent some time to share my experience in scilab.

thanks for your sharing.

CL

Anonymous said...

Nice thanks :p

AP said...

Itz really helpfull

I teach, therefore I am. said...

Thank you for posting this .. I was using the polar function all this time to plot a circle.

william said...


Every person has the passion in any aspects or things.
If you love something and it came in front of you it completes your day and your mood turns into something you won't expected.
I love your work and I want to read more about it. Visit my site if you have time . Thank you.

triciajoy.com

www.triciajoy.com