1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.math; 18 19 import java.io.Serializable; 20 21 /** A point in a 2D grid, with integer x and y coordinates 22 * 23 * @author badlogic */ 24 public class GridPoint2 implements Serializable { 25 private static final long serialVersionUID = -4019969926331717380L; 26 27 public int x; 28 public int y; 29 30 /** Constructs a new 2D grid point. */ 31 public GridPoint2 () { 32 } 33 34 /** Constructs a new 2D grid point. 35 * 36 * @param x X coordinate 37 * @param y Y coordinate */ 38 public GridPoint2 (int x, int y) { 39 this.x = x; 40 this.y = y; 41 } 42 43 /** Copy constructor 44 * 45 * @param point The 2D grid point to make a copy of. */ 46 public GridPoint2 (GridPoint2 point) { 47 this.x = point.x; 48 this.y = point.y; 49 } 50 51 /** Sets the coordinates of this 2D grid point to that of another. 52 * 53 * @param point The 2D grid point to copy the coordinates of. 54 * 55 * @return this 2D grid point for chaining. */ 56 public GridPoint2 set (GridPoint2 point) { 57 this.x = point.x; 58 this.y = point.y; 59 return this; 60 } 61 62 /** Sets the coordinates of this 2D grid point. 63 * 64 * @param x X coordinate 65 * @param y Y coordinate 66 * 67 * @return this 2D grid point for chaining. */ 68 public GridPoint2 set (int x, int y) { 69 this.x = x; 70 this.y = y; 71 return this; 72 } 73 74 /** 75 * @param other The other point 76 * @return the squared distance between this point and the other point. 77 */ 78 public float dst2 (GridPoint2 other) { 79 int xd = other.x - x; 80 int yd = other.y - y; 81 82 return xd * xd + yd * yd; 83 } 84 85 /** 86 * @param x The x-coordinate of the other point 87 * @param y The y-coordinate of the other point 88 * @return the squared distance between this point and the other point. 89 */ 90 public float dst2 (int x, int y) { 91 int xd = x - this.x; 92 int yd = y - this.y; 93 94 return xd * xd + yd * yd; 95 } 96 97 /** 98 * @param other The other point 99 * @return the distance between this point and the other vector. 100 */ 101 public float dst (GridPoint2 other) { 102 int xd = other.x - x; 103 int yd = other.y - y; 104 105 return (float)Math.sqrt(xd * xd + yd * yd); 106 } 107 108 /** 109 * @param x The x-coordinate of the other point 110 * @param y The y-coordinate of the other point 111 * @return the distance between this point and the other point. 112 */ 113 public float dst (int x, int y) { 114 int xd = x - this.x; 115 int yd = y - this.y; 116 117 return (float)Math.sqrt(xd * xd + yd * yd); 118 } 119 120 /** 121 * Adds another 2D grid point to this point. 122 * 123 * @param other The other point 124 * @return this 2d grid point for chaining. 125 */ 126 public GridPoint2 add (GridPoint2 other) { 127 x += other.x; 128 y += other.y; 129 return this; 130 } 131 132 /** 133 * Adds another 2D grid point to this point. 134 * 135 * @param x The x-coordinate of the other point 136 * @param y The y-coordinate of the other point 137 * @return this 2d grid point for chaining. 138 */ 139 public GridPoint2 add (int x, int y) { 140 this.x += x; 141 this.y += y; 142 return this; 143 } 144 145 /** 146 * Subtracts another 2D grid point from this point. 147 * 148 * @param other The other point 149 * @return this 2d grid point for chaining. 150 */ 151 public GridPoint2 sub (GridPoint2 other) { 152 x -= other.x; 153 y -= other.y; 154 return this; 155 } 156 157 /** 158 * Subtracts another 2D grid point from this point. 159 * 160 * @param x The x-coordinate of the other point 161 * @param y The y-coordinate of the other point 162 * @return this 2d grid point for chaining. 163 */ 164 public GridPoint2 sub (int x, int y) { 165 this.x -= x; 166 this.y -= y; 167 return this; 168 } 169 170 /** 171 * @return a copy of this grid point 172 */ 173 public GridPoint2 cpy () { 174 return new GridPoint2(this); 175 } 176 177 @Override 178 public boolean equals (Object o) { 179 if (this == o) return true; 180 if (o == null || o.getClass() != this.getClass()) return false; 181 GridPoint2 g = (GridPoint2)o; 182 return this.x == g.x && this.y == g.y; 183 } 184 185 @Override 186 public int hashCode () { 187 final int prime = 53; 188 int result = 1; 189 result = prime * result + this.x; 190 result = prime * result + this.y; 191 return result; 192 } 193 194 @Override 195 public String toString () { 196 return "(" + x + ", " + y + ")"; 197 } 198 } 199