/* 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 no.geosoft.cc.geometry.Geometry;
/**
* GeomIntersectlinepathCmd implements the "geom.intersectlinepath" command, which returns
* the first intersection point of the extensions of a line with a path.
* If the extension of the line does not intersect the path, an exception is raised.
*/
public class GeomIntersectlinepathCmd implements Command {
boolean debug = false;
public GeomIntersectlinepathCmd () {
}
@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;
if (argd.length >= 8 && argd.length % 2 == 0) {
assert (argd.length - 4) % 2 == 0: "nb of arguments not even";
int n = (argd.length - 4) / 2;
double[] pathx = new double[n];
double[] pathy = new double[n];
for (int i = 0; i < n; i++) {
pathx[i] = argd[4 + 2 * i];
pathy[i] = argd[5 + 2 * i];
}
double[] intersection = new double[2];
int intersectionType;
boolean intersectionfound = false;
for (int i = 1; i < n; i++) {
if (intersectionfound) break;
intersectionType = Geometry.findLineSegmentIntersection(argd[0], argd[1], argd[2], argd[3], pathx[i-1], pathy[i-1], pathx[i], pathy[i], intersection);
switch (intersectionType) {
case 1: // Linienabschnitte schneiden sich
intersectionfound = true;
break;
case 0: // Verlängerung der Abschnitte schneiden sich.
double[] ponline = Geometry.computePointOnLine(argd[0], argd[1], intersection[0], intersection[1], 1.1);
int intersectionTypePath = Geometry.findLineSegmentIntersection(argd[0], argd[1], ponline[0], ponline[1], pathx[i-1], pathy[i-1], pathx[i], pathy[i], intersection);
if (intersectionTypePath == 1) intersectionfound = true;
else {
assert intersectionTypePath == 0;
}
break;
case -1: // Linien parallel
break;
case -2: // Linien liegen aufeinander
if (debug) System.out.println("Linien aufeinander liegend");
break;
default:
assert false;
}
}
if (intersectionfound) {
String xstr = StringThing.get(DoubleThing.create(intersection[0]));
String ystr = StringThing.get(DoubleThing.create(intersection[1]));
answer = xstr + " " + ystr;
} else {
answer = "none none"; // not used
// We prefer to raise an error messages over undefined behaviour
throw new HeclException ("No intersection point.");
}
} else {
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.