/*---------------- FILE HEADER --------------------------------------- This file is part of Geoide. Copyright (C) 2005-2006 by: IDgis B.V. http://www.idgis.nl This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Herman Assink IDgis bv P.O. Box 15 7450 AA Holten The Netherlands E-Mail: herman.assink@idgis.nl * @version 1.4.0 * @author IDgis team * ------------------------------------------------------------------------*/ import nl.idgis.giclient.geoma.Envelope; import nl.idgis.giclient.geoma.Geometry; import nl.idgis.giclient.geoma.Point; import nl.idgis.giclient.geoma.NullGeometry; import nl.idgis.giclient.geoma.GeometryTools; class nl.idgis.giclient.geoma.LineSegment extends Geometry { private var point0:Point = null; private var point1:Point = null; private var n:Number = -1; function LineSegment(srsName:String, point0:Point, point1:Point, n:Number) { super(srsName); this.point0 = point0; this.point1 = point1; this.n = n; } function move(dx:Number,dy:Number, permanent:Boolean):Void {} function getCoords():Array { return new Array(point0, point1); } function getPoints():Array { return new Array(point0, point1); } function getCenterPoint():Point { return point0; // TODO } function getNearestPoint(point:Point):Point { return point0; // TODO } function getEnvelope():Envelope { var minX:Number = point0.getX(); var minY:Number = point0.getY(); var maxX:Number = point0.getX(); var maxY:Number = point0.getY(); if (minX > point1.getX()) { minX = point1.getX(); } else { maxX = point1.getX(); } if (minY > point1.getY()) { minY = point1.getY(); } else { maxY = point1.getY(); } return new Envelope(getSRS(), minX, minY, maxX, maxY); } function clip(envelope:Envelope):Geometry { var point0IsWithin:Boolean = GeometryTools.isWithin(point0, envelope); var point1IsWithin:Boolean = GeometryTools.isWithin(point1, envelope); if ((point0IsWithin) && (point1IsWithin)) { return clone(); } var eMinX:Number = envelope.getMinX(); var eMinY:Number = envelope.getMinY(); var eMaxX:Number = envelope.getMaxX(); var eMaxY:Number = envelope.getMaxY(); // Calculates the function y = ax + b for the linestring. var a:Number = -1; if ((point1.getX() - point0.getX()) == 0) { a = 99999999; } else { a = (point1.getY() - point0.getY()) / (point1.getX() - point0.getX()); } if (a == 0) { a = 0.0000001; } var b:Number = point0.getY() - a * point0.getX(); // Calculates the intersection points of the linestring and the given envelope. var yMinX = a * eMinX + b; var yMaxX = a * eMaxX + b; var xMinY = (eMinY - b) / a; var xMaxY = (eMaxY - b) / a; var intersectionPoints:Array = new Array(); if ((yMinX >= eMinY) && (yMinX <= eMaxY)) { intersectionPoints.push(new Point(getSRS(), eMinX, yMinX)); } if ((yMaxX >= eMinY) && (yMaxX <= eMaxY)) { intersectionPoints.push(new Point(getSRS(), eMaxX, yMaxX)); } if ((xMinY >= eMinX) && (xMinY <= eMaxX)) { intersectionPoints.push(new Point(getSRS(), xMinY, eMinY)); } if ((xMaxY >= eMinX) && (xMaxY <= eMaxX)) { intersectionPoints.push(new Point(getSRS(), xMaxY, eMaxY)); } if (intersectionPoints.length == 0) { return new NullGeometry(); } if (intersectionPoints.length == 1) { trace("NUM POINTS IS 1!!!"); return new NullGeometry(); } var intersectionPoint0:Point = Point(intersectionPoints[0]); var intersectionPoint1:Point = Point(intersectionPoints[1]); if ((point0IsWithin) || (point1IsWithin)) { var distance0:Number = point0.getDistance(intersectionPoint0) + point1.getDistance(intersectionPoint0); var distance1:Number = point0.getDistance(intersectionPoint1) + point1.getDistance(intersectionPoint1); if (distance0 < distance1) { if (point0IsWithin) { return new LineSegment(getSRS(), Point(point0.clone()), intersectionPoint0, n); } return new LineSegment(getSRS(), intersectionPoint0, Point(point1.clone()), n); } if (point0IsWithin) { return new LineSegment(getSRS(), Point(point0.clone()), intersectionPoint1, n); } return new LineSegment(getSRS(), intersectionPoint1, Point(point1.clone()), n); } if ((point1.getX() - point0.getX()) * (intersectionPoint1.getX() - intersectionPoint0.getX()) > 0) { // Either: both positive or both negative. return new LineSegment(getSRS(), intersectionPoint0, intersectionPoint1, n); } return new LineSegment(getSRS(), intersectionPoint1, intersectionPoint0, n); } function clone():Geometry { var clone0:Point = Point(point0.clone()); var clone1:Point = Point(point1.clone()); return new LineSegment(getSRS(), clone0, clone1, n); } function toString():String { return("LineSegment (" + point0.toString() + "," + point1.toString() + ")"); } function getN():Number { return n; } }