Statistics
| Revision:

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 / UnitVector.java @ 40559

History | View | Annotate | Download (2.64 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 java.awt.geom.Point2D;
27

    
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
30
import org.gvsig.fmap.geom.GeometryLocator;
31
import org.gvsig.fmap.geom.GeometryManager;
32
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.geom.operation.GeometryOperation;
34
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
35
import org.gvsig.fmap.geom.operation.GeometryOperationException;
36
import org.gvsig.fmap.geom.primitive.Point;
37

    
38

    
39
/**
40
 * Returns an unit vector from two points.
41
 * 
42
 * @author gvSIG Team
43
 * @version $Id$
44
 *
45
 */
46
public class UnitVector extends GeometryOperation{
47
    public static final String NAME = "unitvector";
48
    private static GeometryManager geomManager = GeometryLocator.getGeometryManager();
49
    public static final int CODE = geomManager.getGeometryOperationCode(NAME);
50

    
51
    public Object invoke(Geometry geom, GeometryOperationContext ctx)
52
        throws GeometryOperationException {
53
        Point point1 = (Point)geom;
54
        Point point2 = (Point)ctx.getAttribute(PerpendicularOperationContext.SECOND_POINT);
55

    
56
        Point2D paux = new Point2D.Double(point2.getX() - point1.getX(),
57
            point2.getY() - point1.getY());
58

    
59
        double v = Math.sqrt(Math.pow(paux.getX(), 2d) +
60
            Math.pow(paux.getY(), 2d));
61

    
62
        paux = new Point2D.Double(paux.getX() / v, paux.getY() / v);
63

    
64
        try{
65
            return  geomManager.createPoint(paux.getX() / v, paux.getY() / v, SUBTYPES.GEOM2D);
66
        } catch (CreateGeometryException e) {
67
            throw new GeometryOperationException(e);
68
        }
69
    }
70

    
71
    public int getOperationIndex() {      
72
        return CODE;
73
    }    
74
}