1 /* 2 * Copyright (c) 2016 Vivid Solutions. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. 7 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html 8 * and the Eclipse Distribution License is available at 9 * 10 * http://www.eclipse.org/org/documents/edl-v10.php. 11 */ 12 package org.locationtech.jts.geom; 13 14 /** 15 * Utility functions for working with quadrants of the Euclidean plane. 16 * <p> 17 * Quadrants are referenced and numbered as follows: 18 * <pre> 19 * 1 - NW | 0 - NE 20 * -------+------- 21 * 2 - SW | 3 - SE 22 * </pre> 23 * 24 * @version 1.7 25 */ 26 public class Quadrant 27 { 28 public static final int NE = 0; 29 public static final int NW = 1; 30 public static final int SW = 2; 31 public static final int SE = 3; 32 33 /** 34 * Returns the quadrant of a directed line segment (specified as x and y 35 * displacements, which cannot both be 0). 36 * 37 * @throws IllegalArgumentException if the displacements are both 0 38 */ quadrant(double dx, double dy)39 public static int quadrant(double dx, double dy) 40 { 41 if (dx == 0.0 && dy == 0.0) 42 throw new IllegalArgumentException("Cannot compute the quadrant for point ( "+ dx + ", " + dy + " )" ); 43 if (dx >= 0.0) { 44 if (dy >= 0.0) 45 return NE; 46 else 47 return SE; 48 } 49 else { 50 if (dy >= 0.0) 51 return NW; 52 else 53 return SW; 54 } 55 } 56 57 /** 58 * Returns the quadrant of a directed line segment from p0 to p1. 59 * 60 * @throws IllegalArgumentException if the points are equal 61 */ quadrant(Coordinate p0, Coordinate p1)62 public static int quadrant(Coordinate p0, Coordinate p1) 63 { 64 if (p1.x == p0.x && p1.y == p0.y) 65 throw new IllegalArgumentException("Cannot compute the quadrant for two identical points " + p0); 66 67 if (p1.x >= p0.x) { 68 if (p1.y >= p0.y) 69 return NE; 70 else 71 return SE; 72 } 73 else { 74 if (p1.y >= p0.y) 75 return NW; 76 else 77 return SW; 78 } 79 } 80 81 /** 82 * Returns true if the quadrants are 1 and 3, or 2 and 4 83 */ isOpposite(int quad1, int quad2)84 public static boolean isOpposite(int quad1, int quad2) 85 { 86 if (quad1 == quad2) return false; 87 int diff = (quad1 - quad2 + 4) % 4; 88 // if quadrants are not adjacent, they are opposite 89 if (diff == 2) return true; 90 return false; 91 } 92 93 /** 94 * Returns the right-hand quadrant of the halfplane defined by the two quadrants, 95 * or -1 if the quadrants are opposite, or the quadrant if they are identical. 96 */ commonHalfPlane(int quad1, int quad2)97 public static int commonHalfPlane(int quad1, int quad2) 98 { 99 // if quadrants are the same they do not determine a unique common halfplane. 100 // Simply return one of the two possibilities 101 if (quad1 == quad2) return quad1; 102 int diff = (quad1 - quad2 + 4) % 4; 103 // if quadrants are not adjacent, they do not share a common halfplane 104 if (diff == 2) return -1; 105 // 106 int min = (quad1 < quad2) ? quad1 : quad2; 107 int max = (quad1 > quad2) ? quad1 : quad2; 108 // for this one case, the righthand plane is NOT the minimum index; 109 if (min == 0 && max == 3) return 3; 110 // in general, the halfplane index is the minimum of the two adjacent quadrants 111 return min; 112 } 113 114 /** 115 * Returns whether the given quadrant lies within the given halfplane (specified 116 * by its right-hand quadrant). 117 */ isInHalfPlane(int quad, int halfPlane)118 public static boolean isInHalfPlane(int quad, int halfPlane) 119 { 120 if (halfPlane == SE) { 121 return quad == SE || quad == SW; 122 } 123 return quad == halfPlane || quad == halfPlane + 1; 124 } 125 126 /** 127 * Returns true if the given quadrant is 0 or 1. 128 */ isNorthern(int quad)129 public static boolean isNorthern(int quad) 130 { 131 return quad == NE || quad == NW; 132 } 133 } 134