/src/geos/src/noding/Octant.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2006 Refractions Research Inc. |
7 | | * |
8 | | * This is free software; you can redistribute and/or modify it under |
9 | | * the terms of the GNU Lesser General Licence as published |
10 | | * by the Free Software Foundation. |
11 | | * See the COPYING file for more information. |
12 | | * |
13 | | ********************************************************************** |
14 | | * |
15 | | * Last port: noding/Octant.java rev. 1.2 (JTS-1.7) |
16 | | * |
17 | | **********************************************************************/ |
18 | | |
19 | | #include <cmath> |
20 | | #include <sstream> |
21 | | |
22 | | #include <geos/util/IllegalArgumentException.h> |
23 | | #include <geos/noding/Octant.h> |
24 | | #include <geos/geom/Coordinate.h> |
25 | | |
26 | | // |
27 | | using namespace geos::geom; |
28 | | |
29 | | namespace geos { |
30 | | namespace noding { // geos.noding |
31 | | |
32 | | /*public static*/ |
33 | | int |
34 | | Octant::octant(double dx, double dy) |
35 | 421M | { |
36 | 421M | if(dx == 0.0 && dy == 0.0) { |
37 | 0 | std::ostringstream s; |
38 | 0 | s << "Cannot compute the octant for point ( " << dx << ", " << dy << " )"; |
39 | 0 | throw util::IllegalArgumentException(s.str()); |
40 | 0 | } |
41 | | |
42 | 421M | double adx = std::fabs(dx); |
43 | 421M | double ady = std::fabs(dy); |
44 | | |
45 | 421M | if(dx >= 0) { |
46 | 241M | if(dy >= 0) { |
47 | 151M | if(adx >= ady) { |
48 | 89.3M | return 0; |
49 | 89.3M | } |
50 | 61.8M | else { |
51 | 61.8M | return 1; |
52 | 61.8M | } |
53 | 151M | } |
54 | 89.8M | else { // dy < 0 |
55 | 89.8M | if(adx >= ady) { |
56 | 34.3M | return 7; |
57 | 34.3M | } |
58 | 55.4M | else { |
59 | 55.4M | return 6; |
60 | 55.4M | } |
61 | 89.8M | } |
62 | 241M | } |
63 | 180M | else { // dx < 0 |
64 | 180M | if(dy >= 0) { |
65 | 108M | if(adx >= ady) { |
66 | 75.7M | return 3; |
67 | 75.7M | } |
68 | 32.3M | else { |
69 | 32.3M | return 2; |
70 | 32.3M | } |
71 | 108M | } |
72 | 72.2M | else { // dy < 0 |
73 | 72.2M | if(adx >= ady) { |
74 | 39.4M | return 4; |
75 | 39.4M | } |
76 | 32.8M | else { |
77 | 32.8M | return 5; |
78 | 32.8M | } |
79 | 72.2M | } |
80 | 180M | } |
81 | | |
82 | 421M | } |
83 | | |
84 | | /*public static*/ |
85 | | int |
86 | | Octant::octant(const CoordinateXY& p0, const CoordinateXY& p1) |
87 | 421M | { |
88 | 421M | double dx = p1.x - p0.x; |
89 | 421M | double dy = p1.y - p0.y; |
90 | | |
91 | 421M | return octant(dx, dy); |
92 | 421M | } |
93 | | |
94 | | } // namespace geos.noding |
95 | | } // namespace geos |