/* 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.geom;
import java.util.ArrayList;
import java.util.Arrays;
import org.hecl.*;
/**
* GeomExtendCmd implements the "geom.extend" command, which returns
* two points on the line.
*/
public class GeomExtendCmd implements Command {
boolean debug = false;
public GeomExtendCmd () {
}
@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 < 4) throw new HeclException ("Illegal nb of arguments.");
assert (argd.length >= 5);
double x0 = argd[0];
double y0 = argd[1];
double x1 = argd[2];
double y1 = argd[3];
double dx = x1 - x0;
double dy = y1 - y0;
double L = Math.hypot(dx, dy);
double[] A = new double[2];
double[] B = new double[2];
switch (argd.length) {
case 6: // x0 y0 x1 y1 dLA dLB
double dLA = argd[4];
double dLB = argd[5];
B[0] = x1 + dLB * dx/L;
B[1] = y1 + dLB * dy/L;
A[0] = x0 - dLA * dx/L;
A[1] = y0 - dLA * dy/L;
break;
case 5: // x0 y0 x1 y1 dL
double dL = argd[4];
B[0] = x1 + dL * dx/L;
B[1] = y1 + dL * dy/L;
A[0] = x0 - dL * dx/L;
A[1] = y0 - dL * dy/L;
break;
default:
throw new HeclException ("Illegal nb of arguments.");
}
String xAstr = StringThing.get(DoubleThing.create(A[0]));
String yAstr = StringThing.get(DoubleThing.create(A[1]));
String xBstr = StringThing.get(DoubleThing.create(B[0]));
String yBstr = StringThing.get(DoubleThing.create(B[1]));
String answer = xAstr + " " + yAstr + " " + xBstr + " " + yBstr;
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.