[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ellipse code



PureBytes Links

Trading Reference Links

>Anyone have any code to draw ellipses (really hexagons or octagons or
>bleep-a-gons) that they would be willing to share?

For an ellipse with horizontal axis 2a and vertical axis 2b, the
radius r from the center at an angle theta from horizontal is

ct = cos(theta)
st = sin(theta)
r = SquareRoot(b*b*a*a / (b*b*ct*ct + a*a*st*st))

Then the x and y coordinates to plot are simply

x = x0 + r*ct
y = y0 + r*st

where x0 and y0 are the origin.

So, we can use trendlines to plot an ellipse having an axis of 2a bars
horizontally and 2b price units vertically.  The downside is that you
can't draw trendlines on future dates, so all points on the ellipse
would have to exist on historical bars, not future bars.  A function to
plot an ellipse might look something like this (untested):

===================================================================
{function ellipse - untested}
inputs: bars_ago(NumericSimple),    {bars ago for center of ellipse}
        centerprice(NumericSimple), {price for center of ellipse}
        a(NumericSimple),           {half of horizontal axis in bars}
        b(NumericSimple),           {half of vertical axis in bars}
        anglestep(NumericSimple);   {ellipse angle step (divides into 360)}

var: theta(0), r(0), ct(0), st(0), xl(0),yl(0), x(0), y(0), TL(0);

xl = bars_ago+a;
yl = centerprice;
for theta = anglestep to 360 begin
   ct = cosine(theta);
   st = sine(theta);
   r = SquareRoot(b*b*a*a / (b*b*ct*ct + a*a*st*st));
   x = date(int(bars_ago + 0.5 + r*ct));
   y = centerprice + r*st;
   TL = TL_New(date(xl),time(xl),yl,date(x),time(x),y);
   xl = x;
   yl = y;
   theta = theta+anglestep-1; {force theta increment by anglestep each loop}
end;
===================================================================

-Alex