GEOS  3.14.0dev
Quadrant.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2005-2006 Refractions Research Inc.
7  * Copyright (C) 2001-2002 Vivid Solutions Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************
15  *
16  * Last port: geom/Quadrant.java rev. 1.8 (JTS-1.10)
17  *
18  **********************************************************************/
19 
20 
21 #pragma once
22 
23 #include <geos/export.h>
24 #include <geos/geom/Quadrant.h>
25 #include <geos/geom/Coordinate.h>
26 #include <geos/util/IllegalArgumentException.h>
27 
28 #include <string>
29 #include <sstream>
30 
31 // Forward declarations
32 namespace geos {
33 namespace geom {
34 class Coordinate;
35 }
36 }
37 
38 namespace geos {
39 namespace geom { // geos.geom
40 
52 class GEOS_DLL Quadrant {
53 
54 public:
55 
56  static const int NE = 0;
57  static const int NW = 1;
58  static const int SW = 2;
59  static const int SE = 3;
60 
67  static int quadrant(double dx, double dy)
68  {
69  if(dx == 0.0 && dy == 0.0) {
70  std::ostringstream s;
71  s << "Cannot compute the quadrant for point ";
72  s << "(" << dx << "," << dy << ")" << std::endl;
73  throw util::IllegalArgumentException(s.str());
74  }
75  if(dx >= 0) {
76  if(dy >= 0) {
77  return NE;
78  }
79  else {
80  return SE;
81  }
82  }
83  else {
84  if(dy >= 0) {
85  return NW;
86  }
87  else {
88  return SW;
89  }
90  }
91  };
92 
98  static int quadrant(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1)
99  {
100  if(p1.x == p0.x && p1.y == p0.y) {
101  throw util::IllegalArgumentException("Cannot compute the quadrant for two identical points " + p0.toString());
102  }
103 
104  if(p1.x >= p0.x) {
105  if(p1.y >= p0.y) {
106  return NE;
107  }
108  else {
109  return SE;
110  }
111  }
112  else {
113  if(p1.y >= p0.y) {
114  return NW;
115  }
116  else {
117  return SW;
118  }
119  }
120  };
121 
125  static bool isOpposite(int quad1, int quad2)
126  {
127  if(quad1 == quad2) {
128  return false;
129  }
130  int diff = (quad1 - quad2 + 4) % 4;
131  // if quadrants are not adjacent, they are opposite
132  if(diff == 2) {
133  return true;
134  }
135  return false;
136  };
137 
138  /*
139  * Returns the right-hand quadrant of the halfplane defined by
140  * the two quadrants,
141  * or -1 if the quadrants are opposite, or the quadrant if they
142  * are identical.
143  */
144  static int commonHalfPlane(int quad1, int quad2);
145 
150  static bool isInHalfPlane(int quad, int halfPlane);
151 
155  static bool isNorthern(int quad)
156  {
157  return quad == NE || quad == NW;
158  };
159 
160 };
161 
162 
163 } // namespace geos.geom
164 } // namespace geos
Utility functions for working with quadrants.
Definition: Quadrant.h:52
static int quadrant(double dx, double dy)
Definition: Quadrant.h:67
static bool isOpposite(int quad1, int quad2)
Definition: Quadrant.h:125
static int quadrant(const geom::CoordinateXY &p0, const geom::CoordinateXY &p1)
Definition: Quadrant.h:98
static bool isInHalfPlane(int quad, int halfPlane)
static bool isNorthern(int quad)
Definition: Quadrant.h:155
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:33
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25