Jsoftware
High-Performance Development Platform

Cosine cycle for ellipse rather than circle

View: New views
7 Messages — Rating Filter:   Alert me  

Cosine cycle for ellipse rather than circle

by PackRat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've already worked with this kind of thing:

   load 'plot'
   pi=: o. 1    NB. definition of PI

   cos2=: 4 : 0
     NB. syntax:  cyclelength cos2 timespan
     2 o. (i. y) * +: pi % x
   )

   plot 100 cos2 100

However, my math skills from 45 years ago aren't enough to figure out
what the cos2 routine should look like for an ellipse instead of a
circle.  (I don't even know exactly what the cyclical curve would look
like!)  I'm sure the two semidiameters somehow would have to come into
play in the formula (and in the arguments to the verb).  I'm presuming
a "vertical" or "horizontal" orientation of the ellipse should make no
difference; if it does, I'll need to know how to deal with both.  
Thanks in advance for your assistance!

Harvey

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: PackRat
>
> However, my math skills from 45 years ago aren't enough to figure out
> what the cos2 routine should look like for an ellipse instead of a
> circle.  (I don't even know exactly what the cyclical curve would look
> like!)  I'm sure the two semidiameters somehow would have to come into
> play in the formula (and in the arguments to the verb).  I'm presuming
> a "vertical" or "horizontal" orientation of the ellipse should make no
> difference; if it does, I'll need to know how to deal with both.
> Thanks in advance for your assistance!
>

Here's a simple (rather than concise) solution for plotting an ellipse, does that help?

load 'plot'
require 'numeric trig'

NB.*getEllipsePoints v Returns X,:Y coords for ellipse
NB. eg: getEllipsePoints 0 0 8 2 90
NB. y is: 5-item numeric list
NB.     0 1{ x and y coords of ellipse center
NB.     2 3{ lengths of semimajor and semiminor axes
NB.       4{ Angle (degrees) of the semimajor axis from horizontal
NB. x is: optional number of points to plot (default 36)
getEllipsePoints=: verb define
  36 getEllipsePoints y
:
  'Xc Yc a b angle'=. y
  nsteps=. x
  'sinbeta cosbeta'=. (sin , cos) rfd - angle
  alpha=. rfd steps 0 360 , nsteps
  'sinalpha cosalpha'=. (sin ,: cos) alpha
  X=. Xc + (a * cosbeta * cosalpha) - b * sinbeta * sinalpha
  Y=. Yc + (a * sinbeta * cosalpha) + b * cosbeta * sinalpha
  X,:Y
)

Note 'Usage'
 plot ;/ getEllipsePoints 0 0 8 2 0    NB. horizontal ellipse
 plot ;/ getEllipsePoints 0 0 8 2 30   NB. ellipse on angle
 plot ;/ getEllipsePoints 2 3 8 2 30   NB. ellipse on angle, center 2 3
 plot ;/ getEllipsePoints 0 0 10 10 0  NB. circle
 plot ;/ 1 0 2|:(getEllipsePoints 0 0 10 10 0) ,: getEllipsePoints 2 2 8 2 20
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by Raul Miller-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 11, 2009 at 7:39 PM, PackRat <packrat@...> wrote:

> I've already worked with this kind of thing:
>
>   load 'plot'
>   pi=: o. 1    NB. definition of PI
>
>   cos2=: 4 : 0
>     NB. syntax:  cyclelength cos2 timespan
>     2 o. (i. y) * +: pi % x
>   )
>
>   plot 100 cos2 100
>
> However, my math skills from 45 years ago aren't enough to figure out
> what the cos2 routine should look like for an ellipse instead of a
> circle.

Essentially, for the way you have things set up, nothing changes.

If you think of a circle as having points with x and y coordinates,
your plot would only show the y component of those coordinates.

If you think of an ellipse in the same frame of reference, the thing
difference between a circle and an ellipse is the scale factor used
for the x and y coordinates.

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by PackRat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ric Sherlock wrote:
> Here's a simple (rather than concise) solution for plotting an
> ellipse, does that help?

As interesting as that is (and perhaps useful in the future), I'm NOT
looking for code to PLOT an ellipse.  What I'm wanting is the cosine as
the degree angle at the point in the very center of an ellipse varies
from 0 degrees to 360 degrees (and the degree line intersects the curve
of the ellipse), just as you would going from 0 to 360 degrees at the
center of a circle, the degree line intersecting the circle (as
indicated in the sample code I included in my original query).  I hope
this clarifies what I'm seeking.  Thanks!

Harvey

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by Boyko Bantchev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/13 PackRat <packrat@...>:
> ......  What I'm wanting is the cosine as
> the degree angle at the point in the very center of an ellipse varies
> from 0 degrees to 360 degrees (and the degree line intersects the curve
> of the ellipse), just as you would going from 0 to 360 degrees at the
> center of a circle, the degree line intersecting the circle (as
> indicated in the sample code I included in my original query).  I hope
> this clarifies what I'm seeking.  Thanks!

Harvey,

An ellipse with its major and minor axes parallel to the axes
of the coordinate system, and centred at the coordinate system's
origin has the equations
        x = a (cos t)
        y = b (sin t)
where a and b are the lengths of the ellipse's semi-axes and t
is an angle parameter taking any value within [0,2pi).
A ray from the centre towards a point (x,y) on the ellipse will
therefore make an angle u with the positive horizontal direction
such that
        cos u = x/d = (a (cos t))/d
        sin u = y/d = (b (sin t))/d
where d = x^2 + y^2 = (a (cos t))^2 + (b (sin t))^2
is the length of the position vector of (x,y).
I.e., if t is the angle parameter that characterizes a point
as in the ellipse equations, then u is the actual angle of
the position vector of the same point.

Is that what you are interested in?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by Boyko Bantchev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/13 Boyko Bantchev <boykobb@...>:
> ...
> where d = x^2 + y^2 = (a (cos t))^2 + (b (sin t))^2
> ...

.... Of course, I should have written
  d = sqrt(x^2 + y^2) = sqrt((a (cos t))^2 + (b (sin t))^2)
instead.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Cosine cycle for ellipse rather than circle

by PackRat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Boyko Bantchev wrote:
> An ellipse with its major and minor axes parallel to the axes
> of the coordinate system, and centred at the coordinate system's
> origin has the equations ...
> Is that what you are interested in?

Yes, that is exactly what I was looking for.  I do have a question
regarding the meaning of t vs. u, but I'll contact you privately about
that, since it's not related to J.  Thanks again!

Harvey

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm