/* 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.*;
import net.sf.drawj2d.cmd.API;
import no.geosoft.cc.geometry.Geometry;
/**
* GeomParallelCmd implements the "geom.parallel" command, which
* returns a parallel line, defined by two points.
*/
public class GeomParallelCmd implements Command {
API api;
boolean debug = false;
public GeomParallelCmd (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;
double L;
double dist;
double dx, dy;
switch (argd.length) {
case 5: // x1 y1 x2 y2 dist
dist = argd[4];
break;
case 4: // x1 y1 x2 y2. Through current position
double[] here = api.here();
dist = Geometry.distance(argd[0], argd[1], argd[2], argd[3], here[0], here[1]);
L = Math.hypot(argd[2]-argd[0], argd[3]-argd[1]);
double tmpPx = argd[0] - (argd[3]-argd[1]) / L * dist; // P, but might be on the wrong side
double tmpPy = argd[1] + (argd[2]-argd[0]) / L * dist;
if (Geometry.sameSide(argd[0], argd[1], argd[2], argd[3], here[0], here[1], tmpPx, tmpPy) < 0) dist *= -1;
break;
default:
throw new HeclException ("Illegal nb of arguments.");
}
assert argd.length > 3;
L = Math.hypot(argd[2]-argd[0], argd[3]-argd[1]);
dx = -(argd[3]-argd[1]) / L * dist;
dy = (argd[2]-argd[0]) / L* dist;
double xP = argd[0] + dx;
double yP = argd[1] + dy;
double xQ = argd[2] + dx;
double yQ = argd[3] + dy;
String xPstr = StringThing.get(DoubleThing.create(xP));
String yPstr = StringThing.get(DoubleThing.create(yP));
String xQstr = StringThing.get(DoubleThing.create(xQ));
String yQstr = StringThing.get(DoubleThing.create(yQ));
answer = xPstr + " " + yPstr + " " + xQstr + " " + yQstr;
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.