/* Copyright (C) 2014-2015 A.Vontobel  
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The license is in LICENCE-GPL.txt.
 * If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 */

package net.sf.drawj2d.cmd;

import org.hecl.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * FillsectorCmd implements the "fillsector" command, which fills
 * a sector (arc).
 */

public class FillsectorCmd implements Command {
    API api;
    boolean debug = false;
    
    public FillsectorCmd (API api) {
        this.api = api;
    }
    
    @Override
    public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
        assert argv.length > 0;
        if (debug) System.out.println(argv[0].toString());
        
        ArrayList singleargv = splitArgv(argv);
        // Convert the arguments from thing to double.
        int nbsingleargs = singleargv.size(); // without command from argv[0]
        double[] argd = new double[nbsingleargs];
        for (int i = 0; i < nbsingleargs; i++) {
            argd[i] = NumberThing.asNumber(singleargv.get(i)).doubleValue();
        }
        
        double x1, y1;
        double radius;
        double startangle;
        double endangle;
        double[] arcstart;
        double[] arcend;
        double[] X = new double[3];
        double[] Y = new double[3];
        
        switch (argd.length) {
            case 5: // x1 y1 radius startangle endangle
                x1 = argd[0];
                y1 = argd[1];
                radius = argd[2];
                startangle = argd[3];
                endangle = argd[4];
                break;
            case 3: // radius startangle endangle
                x1 = api.here()[0];
                y1 = api.here()[1];
                radius = argd[0];
                startangle = argd[1];
                endangle = argd[2];
                break;
            default:
                throw new HeclException ("Illegal nb of arguments.");
        }
        
        double extent = endangle - startangle;
        while (extent < 0) extent += 360;
        while (extent > 360) extent -= 360;
        
        api.moveto(x1, y1);
        api.movepolar(radius, startangle); arcstart = api.here();
        api.moveto(x1, y1);
        api.movepolar(radius, endangle); arcend = api.here();
        X[0] = x1; Y[0] = y1;
        X[2] = arcend[0];
        Y[2] = arcend[1];
        
        api.moveto(x1, y1);
        if (extent <= 180) {
            if (extent < 180) {
                X[1] = arcstart[0];
                Y[1] = arcstart[1];
                api.fillpolygon(X, Y); // moves the cursor to x1,y1
            }
            api.fillsegment(radius, startangle, endangle);
        } else { // extent > 180°
            double oppositeangle = startangle + 180;
            api.movepolar(radius, oppositeangle);
            double[] opposite = api.here();
            X[1] = opposite[0];
            Y[1] = opposite[1];
            api.fillpolygon(X, Y); // moves the cursor to x1,y1
            api.fillsegment(radius, startangle, oppositeangle);
            api.fillsegment(radius, oppositeangle, endangle);
        }
        
	return null;
    }
    
    /** Make a list of arguments (without command in argv[0]). 
     * Split List arguments to single arguments. */
    private ArrayList splitArgv(Thing[] argv) throws HeclException {
        ArrayList singleargv = new ArrayList(argv.length);
        for (int i = 1; i < argv.length; i++) {
            Thing[] currentargv = ListThing.getArray(argv[i]);
            singleargv.addAll(Arrays.asList(currentargv));
        }
        return singleargv;
    }
}

 

Ads help maintain this website.