package montecarlo;

import ptolemy.plot.Plot;

/**
 * xy plot of a function r(theta), where r is the radial distance from the origin 
 * given as a function of the angle theta (radians) with respect to the x-axis
 */
    
    public class PolarPlot extends Plot {
        
        private int nTheta = 1000;
        private double thetaMin = 0.0;
        private double thetaMax = 2.0*Math.PI;
        private double dTheta;  //increment in theta for plot
        private Function radius = new Function() {public double f(double x) {return 1.0;}}; //default r(theta) is constant
        private double X, Y;
        private boolean fixedRange = false;  //scale plot to range of data if false; use input fixed range if true
        
        public PolarPlot() {
            setTitle("Polar Plot");
            setYRange(-0.5,0.5);
            setXRange(-0.5,0.5);
//            setMarksStyle("none");
            setNTheta(1000);
            setConnected(true);
        }
        
        public void setFunction(Function func) {radius = func;}
        public Function getFunction() {return radius;}
        
        public int getNTheta() {return nTheta;}
        public void setNTheta(int n) {
            if(nTheta < 2) {nTheta = 2;}
            nTheta = n;
            dTheta = (thetaMax - thetaMin)/(nTheta-1);
        }
        
        private void setXY(double theta) { //set the value of X and Y for the given theta
            double r = radius.f(theta);
            X = r*Math.cos(theta);
            Y = r*Math.sin(theta);
        }
        
        public void setRange(double xmin, double xmax, double ymin, double ymax) {
            setXRange(xmin,xmax);
            setYRange(ymin,ymax);
            fixedRange = true;
        }
        public void unsetRange() {fixedRange = false;}
                
        public void display() {
            clear(false);
            repaint();
            for(int i=0; i<nTheta; i++) {
                double theta = thetaMin + i*dTheta;
                setXY(theta);
                addPoint(0,X,Y,i>0);  //don't connect first point (i==0) to "previous" one
            }
            if(!fixedRange) fillPlot();
        }
            
        
    }  //end of PolarPlot
