Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / perpendicular / UnitVector.java @ 39313

History | View | Annotate | Download (2.59 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.fmap.geom.operation.perpendicular;
23

    
24
import java.awt.geom.Point2D;
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
 * Returns an unit vector from two points.
39
 * 
40
 * @author gvSIG Team
41
 * @version $Id$
42
 *
43
 */
44
public class UnitVector extends GeometryOperation{
45
    public static final String NAME = "unitvector";
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

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

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

    
60
        paux = new Point2D.Double(paux.getX() / v, paux.getY() / v);
61

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

    
69
    public int getOperationIndex() {      
70
        return CODE;
71
    }    
72
}