/* Copyright (C) 2014 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;

/**
 * DimangleCmd implements the "dimangle" command, which draws
 * an arc between three points and labels the angle in degrees.
 */

public class DimangleCmd implements Command {
    API api;
    boolean debug = false;
    
    public DimangleCmd (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());
        
        String lb = "";
        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++) {
            Thing currentThing = singleargv.get(i);
            try { // "if number else label"
                argd[i] = NumberThing.asNumber(currentThing).doubleValue();
            } catch (NumberFormatException ex) {
                if (lb.equals("")) lb = StringThing.get(currentThing);
                else throw new HeclException ("Illegal arguments.");
            }
        }
        double x0 = api.here()[0];
        double y0 = api.here()[1];
        
        switch (nbsingleargs) {
            case 7: // x0, y0, xP, yP, xQ, yQ, text
                if (lb.equals("")) lb = StringThing.get(singleargv.get(6));
                api.dimangle(argd[0], argd[1], argd[2], argd[3], argd[4], argd[5], false, lb);
                break;
            case 6: // x0, y0, xP, yP, xQ, yQ
                api.dimangle(argd[0], argd[1], argd[2], argd[3], argd[4], argd[5], false);
                break;
            case 5: // at current pos: xP, yP, xQ, yQ, text
                if (lb.equals("")) lb = StringThing.get(singleargv.get(4));
                api.dimangle(x0, y0, argd[0], argd[1], argd[2], argd[3], false, lb);
                break;
            case 4: // at current pos: xP, yP, xQ, yQ
                api.dimangle(x0, y0, argd[0], argd[1], argd[2], argd[3], false);
                break;
            case 3: // at current pos: startangle, endangle (in degrees), text
                if (lb.equals("")) lb = StringThing.get(singleargv.get(2));
                double xP = x0 + 10d*Math.cos(argd[0]*Math.PI/180d);
                double yP = y0 + 10d*Math.sin(argd[0]*Math.PI/180d);
                double xQ = x0 + 10d*Math.cos(argd[1]*Math.PI/180d);
                double yQ = y0 + 10d*Math.sin(argd[1]*Math.PI/180d);
                api.dimangle(x0, y0, xP, yP, xQ, yQ, false, lb);
                break;
            case 2: // at current pos: startangle, endangle (in degrees)
                double x_P = x0 + 10d*Math.cos(argd[0]*Math.PI/180d);
                double y_P = y0 + 10d*Math.sin(argd[0]*Math.PI/180d);
                double x_Q = x0 + 10d*Math.cos(argd[1]*Math.PI/180d);
                double y_Q = y0 + 10d*Math.sin(argd[1]*Math.PI/180d);
                api.dimangle(x0, y0, x_P, y_P, x_Q, y_Q, false);
                break;
            case 1: // Set the number of decimal digits
                Thing currentThing = singleargv.get(0);
                NumberThing digits = NumberThing.asNumber(currentThing);
                if (digits.isIntegral()) {
                    api.decdigitsangle(digits.intValue());
                } else {
                    throw new HeclException ("Illegal argument. Number of digits (integer) expected, but got: " +
                            StringThing.get(currentThing));
                }
                break;
            default:
                throw new HeclException ("Illegal nb of arguments.");
        }
	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.