/* 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 no.geosoft.cc.geometry.Geometry;

/**
 * StatFequilibriumCmd implements the "stat.fequi", which 
 * equilibrates a known force F by two other forces A and B, of which the azimuth are known.
 * The command returns the force A.
 * Equilibrium is defined F + A + B = 0, thus B is [--- "[XY $F] 0 0" $F $A] resp. [stat.equi $F $B]
 * 
 * Kraftdreieck für das Kräftepolygon F+A+B=0:
 * Bekannt sind F (effektiv genügt F.kraft), sowie die Richtungen von A und B [in Degrees].
 * Gesucht sind die Kraftkomponenten von A (und B).
 * Die Funktion gibt die Kraft A zurück. B ergibt sich aus [--- "[XY $F] 0 0" $F $A] resp. [stat.equi $F $B]
 */

public class StatFequilibriumCmd implements Command {
    boolean debug = false;
    
    public StatFequilibriumCmd () {
    }
    
    @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();
        }
        
        if (argd.length < 6) throw new HeclException ("Illegal nb of arguments.");
        
        assert (argd.length >= 6);
        double x0 = argd[0];
        double y0 = argd[1];
        double Fx = argd[2];
        double Fy = argd[3];
        
        if (Math.hypot(Fx, Fy) == 0) throw new HeclException ("Illegal arguments. Force length must not be 0.");
        double α, β;
        switch (argd.length) {
            case 6: // x0 y0 Fx Fy  α β
                α = argd[4];
                β = argd[5];
                if (α == β) throw new HeclException ("Illegal arguments in x0 y0 Fx Fy alpha beta. Alpha and beta must differ.");
                break;
            default:
                throw new HeclException ("Illegal nb of arguments.");
        }
        
        // asy equivalent: pair A = extension((0,0),dir(degA), -F.kraft, -F.kraft+dir(degB)); 
        
        double[] intersection = new double[2];
        int intersectionType;
        boolean intersectionfound;
        double dAx = Math.cos(Math.toRadians(α));
        double dAy = Math.sin(Math.toRadians(α));
        double dBx = Math.cos(Math.toRadians(β));
        double dBy = Math.sin(Math.toRadians(β));
        intersectionType = Geometry.findLineSegmentIntersection(0, 0, dAx, dAy, -Fx, -Fy, -Fx+dBx, -Fy+dBy, 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
            case -2: // Linien liegen aufeinander
                intersectionfound = false;
                break;
            default:
                assert false;
                intersectionfound = false;
        }
        if (!intersectionfound) {
            // We prefer to raise an error messages over undefined behaviour
            throw new HeclException("No intersection point.");
        }
        
        double Ax = intersection[0];
        double Ay = intersection[1];
        
        String xstr = StringThing.get(DoubleThing.create(x0)); // xA == x0
        String ystr = StringThing.get(DoubleThing.create(y0)); // yA == y0
        String Fxstr = StringThing.get(DoubleThing.create(Ax));
        String Fystr = StringThing.get(DoubleThing.create(Ay));
        String answer = xstr + " " + ystr + " " + Fxstr + " " + Fystr;
        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.