# fit data in the form of random ellipse

from java.awt import *
from jhplot import *
from jhplot.shapes  import *
from java.util import Random
from math import *
from jhpro.fit import *

c1 =HPlot("Canvas",500,500)
c1.setAntiAlias(1)
c1.setLegend(0)
c1.setRange(-10.0,10.0,-10,10.0)
c1.visible(1)

p1=P1D("Random ellipse")
rand = Random()
# a, b are the radius on the x and y axes respectively
a=8  # semi-major axis 
b=3  # semi-minor axis
xshift=0 # (X,Y) position
yshift=-1
theta=3.14/1 # rotation
step=0.04
ra=[v*step for v in range(0,int(6.28/step))]
for t in ra:    # fill random ellipse.
   x= a*cos(t)*cos(theta) - b*sin(t)*sin(theta)+xshift
   y = b*cos(theta)*sin(t) + a*sin(theta)*cos(t)+yshift
   p1.add(x+0.4*rand.nextGaussian(), y+0.3*rand.nextGaussian())
efit= FitEllipse2D(p1) # initialize fitter
print "Average distance=", efit.getDistance()
ele1= efit.getEllipse()
ele1.setFill(0)
ele1.setColor(Color.red)
ele1.setTransparency(0.6)
c1.add(ele1)

# now show all objects
c1.draw(p1)
# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png");


