/* 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.statics;

import java.util.ArrayList;
import java.util.Arrays;
import org.hecl.*;
import net.sf.drawj2d.cmd.API;
import no.geosoft.cc.geometry.Geometry;

/**
 * StatMove2Cmd implements the "stat.move2" command, which moves the
 * force along the its action line, until its arrow head intersects the extension of a line. 
 * If parallel the input force is returned.
 */

public class StatMove2Cmd implements Command {
    API api;
    boolean debug = false;
    
    public StatMove2Cmd (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();
        }
        
        String answer;
        
        switch (argd.length) {
            case 8: // x0 y0 Fx Fy xA yA xB yB
                double[] intersection = new double[2];
                int intersectionType;
                boolean intersectionfound = false;
                double x0 = argd[0];
                double y0 = argd[1];
                double Fx = argd[2];
                double Fy = argd[3];
                double x1 = x0 + Fx;
                double y1 = y0 + Fy;
                intersectionType = Geometry.findLineSegmentIntersection(x0, y0, x1, y1, argd[4], argd[5], argd[6], argd[7], intersection);
                switch (intersectionType) {
                    case 1: // Linienabschnitte schneiden sich
                    case 0: // Verlängerung der Abschnitte schneiden sich (i.O).
                        intersectionfound = true;
                        break;
                    case -1: // Linien parallel
                        if (debug) System.out.println("Kein Schnittpunkt, da Linien parallel");
                        intersection[0] = Double.POSITIVE_INFINITY; // not used
                        intersection[1] = Double.POSITIVE_INFINITY; // not used
                        break;
                    case -2: // Linien liegen aufeinander
                        if (debug) System.out.println("Linien aufeinander liegend");
                        intersection[0] = Double.NaN; // not used
                        intersection[1] = Double.NaN; // not used
                        break;
                    default:
                        assert false;
                }
                String xstr, ystr;
                if (intersectionfound) {
                    double x0new = intersection[0] - api.fu(Fx);
                    double y0new = intersection[1] - api.fu(Fy);
                    xstr = StringThing.get(DoubleThing.create(x0new));
                    ystr = StringThing.get(DoubleThing.create(y0new));
                } else {
                    // We just return the initial force, if its action line does not intersect the line AB
                    xstr = StringThing.get(DoubleThing.create(x0));
                    ystr = StringThing.get(DoubleThing.create(y0));
                }
                String Fxstr = StringThing.get(DoubleThing.create(Fx));
                String Fystr = StringThing.get(DoubleThing.create(Fy));
                answer = xstr + " " + ystr + " " + Fxstr + " " + Fystr;
                break;
            default:
                throw new HeclException ("Illegal nb of arguments.");
        }
	return new Thing(answer);
    }
    
    /** 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.