svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.operation / src / main / java / org / gvsig / fmap / geom / operation / perpendicular / Perpendicular.java @ 40559
History | View | Annotate | Download (3.36 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.geom.operation.perpendicular; |
25 |
|
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
28 |
import org.gvsig.fmap.geom.GeometryLocator; |
29 |
import org.gvsig.fmap.geom.GeometryManager; |
30 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
31 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
32 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
33 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
34 |
import org.gvsig.fmap.geom.primitive.Point; |
35 |
|
36 |
|
37 |
/**
|
38 |
* Gets a couple of points that defines a line that is perpendicular to other line
|
39 |
* and intersects with a point.
|
40 |
* @author gvSIG Team
|
41 |
* @version $Id$
|
42 |
*
|
43 |
*/
|
44 |
public class Perpendicular extends GeometryOperation{ |
45 |
public static final String NAME = "perpendicular"; |
46 |
private static GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
47 |
public static final int CODE = geomManager.getGeometryOperationCode(NAME); |
48 |
|
49 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) |
50 |
throws GeometryOperationException {
|
51 |
Point point1 = (Point)geom; |
52 |
Point point2 = (Point)ctx.getAttribute(PerpendicularOperationContext.SECOND_POINT); |
53 |
Point perpendicularPoint = (Point)ctx.getAttribute(PerpendicularOperationContext.PERPENDICULAR_POINT); |
54 |
|
55 |
if ((point2.getY() - point1.getY()) == 0) { |
56 |
try {
|
57 |
return new Point[] { |
58 |
geomManager.createPoint(perpendicularPoint.getX(), 0, SUBTYPES.GEOM2D),
|
59 |
geomManager.createPoint(perpendicularPoint.getX(), 1, SUBTYPES.GEOM2D)
|
60 |
}; |
61 |
} catch (CreateGeometryException e) {
|
62 |
throw new GeometryOperationException(e); |
63 |
} |
64 |
} |
65 |
|
66 |
//Pendiente de la recta perpendicular
|
67 |
double m = (point1.getX() - point2.getX()) / (point2.getY() - point1.getY());
|
68 |
|
69 |
//b de la funcion de la recta perpendicular
|
70 |
double b = perpendicularPoint.getY() - (m * perpendicularPoint.getX());
|
71 |
|
72 |
//Obtenemos un par de puntos
|
73 |
try {
|
74 |
return new Point[] { |
75 |
geomManager.createPoint(0, (m * 0) + b, SUBTYPES.GEOM2D), |
76 |
geomManager.createPoint(1000, (m * 1000) + b, SUBTYPES.GEOM2D) |
77 |
}; |
78 |
} catch (CreateGeometryException e) {
|
79 |
throw new GeometryOperationException(e); |
80 |
} |
81 |
} |
82 |
|
83 |
public int getOperationIndex() { |
84 |
return CODE;
|
85 |
} |
86 |
} |