Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src-test / com / iver / cit / gvsig / graphtests / TestAngle.java @ 39203

History | View | Annotate | Download (3.11 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.graphtests;
42

    
43
import junit.framework.TestCase;
44

    
45
import org.gvsig.graph.core.TurnUtil;
46

    
47
import com.vividsolutions.jts.algorithm.CGAlgorithms;
48
import com.vividsolutions.jts.geom.Coordinate;
49
import com.vividsolutions.jts.geom.LineSegment;
50

    
51
public class TestAngle extends TestCase {
52
        
53
        public void testAngle() {
54
                Coordinate c1, c2, c3;
55
                double grados;
56
                
57
                // 0 grados (no hay giro
58
                c1 = new Coordinate(-1, 0);
59
                c2 = new Coordinate(0, 0);
60
                c3 = new Coordinate(1, 0);
61
                grados = TurnUtil.angle(c1, c2, c3);
62
                assertEquals(0.0, grados, 0.0);
63
                
64
                
65
                // 90 grados (giro a la izquierda)
66
                c1 = new Coordinate(0, -1);
67
                c2 = new Coordinate(0, 0);
68
                c3 = new Coordinate(-1, 0);
69
                grados = TurnUtil.angle(c1, c2, c3);
70
                assertEquals(90.0, grados, 0.0);
71

    
72
                
73
                // 180 grados (360 grados) giro completo
74
                c1 = new Coordinate(-1, 0);
75
                c2 = new Coordinate(0, 0);
76
                c3 = new Coordinate(-1, 0);
77
                grados = TurnUtil.angle(c1, c2, c3);
78
                assertEquals(180.0, grados, 0.0);
79
                
80
                // 270 grados (giro a la derecha)
81
                c1 = new Coordinate(-1, 0);
82
                c2 = new Coordinate(0, 0);
83
                c3 = new Coordinate(0, 1);
84
                grados = TurnUtil.angle(c1, c2, c3);
85
                assertEquals(270.0, grados, 0.0);
86
                
87
        }
88
        
89
        private double angle(Coordinate c1, Coordinate c2, Coordinate c3) {
90
                double resul = 0.0;
91
                // Normalizamos:
92
                Coordinate origin = new Coordinate(0.0, 0.0);
93
                Coordinate cAux1 = new Coordinate(c2.x-c1.x, c2.y-c1.y);
94
                Coordinate cAux2 = new Coordinate(c3.x-c2.x, c3.y-c2.y);
95
                LineSegment v1 = new LineSegment(origin, cAux1);
96
                LineSegment v2 = new LineSegment(origin, cAux2);
97
                double jtsResul = CGAlgorithms.orientationIndex(c1, c2, c3);
98
                double prodEscalar = cAux1.x*cAux2.x + cAux1.y*cAux2.y;
99
                double cosAlpha = prodEscalar / (v1.getLength() * v2.getLength()) ; 
100
                resul = Math.toDegrees(Math.acos(cosAlpha));
101
                
102
        if (cAux1.x * cAux2.y > cAux1.y * cAux2.x) {
103
            resul = 360 - resul;
104
        }
105

    
106
                System.out.println(resul);
107
                return resul;
108
        }
109

    
110
}
111

    
112