1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.javafx.experiments.utils3d.geom; 34 35 /** 36 * A 2-dimensional, single-precision, floating-point vector. 37 * 38 */ 39 public class Vec2f { 40 /** 41 * The x coordinate. 42 */ 43 public float x; 44 45 /** 46 * The y coordinate. 47 */ 48 public float y; 49 50 public Vec2f() { } 51 52 public Vec2f(float x, float y) { 53 this.x = x; 54 this.y = y; 55 } 56 57 public Vec2f(Vec2f v) { 58 this.x = v.x; 59 this.y = v.y; 60 } 61 62 63 /** 64 * Sets the location of this <code>Vec2f</code> to the same 65 * coordinates as the specified <code>Vec2f</code> object. 66 * @param v the specified <code>Vec2f</code> to which to set 67 * this <code>Vec2f</code> 68 */ 69 public void set(Vec2f v) { 70 this.x = v.x; 71 this.y = v.y; 72 } 73 74 /** 75 * Sets the location of this <code>Vec2f</code> to the 76 * specified <code>float</code> coordinates. 77 * 78 * @param x the new X coordinate of this {@code Vec2f} 79 * @param y the new Y coordinate of this {@code Vec2f} 80 */ 81 public void set(float x, float y) { 82 this.x = x; 83 this.y = y; 84 } 85 86 /** 87 * Returns the square of the distance between two points. 88 * 89 * @param x1 the X coordinate of the first specified point 90 * @param y1 the Y coordinate of the first specified point 91 * @param x2 the X coordinate of the second specified point 92 * @param y2 the Y coordinate of the second specified point 93 * @return the square of the distance between the two 94 * sets of specified coordinates. 95 */ 96 public static float distanceSq(float x1, float y1, float x2, float y2) { 97 x1 -= x2; 98 y1 -= y2; 99 return (x1 * x1 + y1 * y1); 100 } 101 102 /** 103 * Returns the distance between two points. 104 * 105 * @param x1 the X coordinate of the first specified point 106 * @param y1 the Y coordinate of the first specified point 107 * @param x2 the X coordinate of the second specified point 108 * @param y2 the Y coordinate of the second specified point 109 * @return the distance between the two sets of specified 110 * coordinates. 111 */ 112 public static float distance(float x1, float y1, float x2, float y2) { 113 x1 -= x2; 114 y1 -= y2; 115 return (float) Math.sqrt(x1 * x1 + y1 * y1); 116 } 117 118 /** 119 * Returns the square of the distance from this 120 * <code>Vec2f</code> to a specified point. 121 * 122 * @param vx the X coordinate of the specified point to be measured 123 * against this <code>Vec2f</code> 124 * @param vy the Y coordinate of the specified point to be measured 125 * against this <code>Vec2f</code> 126 * @return the square of the distance between this 127 * <code>Vec2f</code> and the specified point. 128 */ 129 public float distanceSq(float vx, float vy) { 130 vx -= x; 131 vy -= y; 132 return (vx * vx + vy * vy); 133 } 134 135 /** 136 * Returns the square of the distance from this 137 * <code>Vec2f</code> to a specified <code>Vec2f</code>. 138 * 139 * @param v the specified point to be measured 140 * against this <code>Vec2f</code> 141 * @return the square of the distance between this 142 * <code>Vec2f</code> to a specified <code>Vec2f</code>. 143 */ 144 public float distanceSq(Vec2f v) { 145 float vx = v.x - this.x; 146 float vy = v.y - this.y; 147 return (vx * vx + vy * vy); 148 } 149 150 /** 151 * Returns the distance from this <code>Vec2f</code> to 152 * a specified point. 153 * 154 * @param vx the X coordinate of the specified point to be measured 155 * against this <code>Vec2f</code> 156 * @param vy the Y coordinate of the specified point to be measured 157 * against this <code>Vec2f</code> 158 * @return the distance between this <code>Vec2f</code> 159 * and a specified point. 160 */ 161 public float distance(float vx, float vy) { 162 vx -= x; 163 vy -= y; 164 return (float) Math.sqrt(vx * vx + vy * vy); 165 } 166 167 /** 168 * Returns the distance from this <code>Vec2f</code> to a 169 * specified <code>Vec2f</code>. 170 * 171 * @param v the specified point to be measured 172 * against this <code>Vec2f</code> 173 * @return the distance between this <code>Vec2f</code> and 174 * the specified <code>Vec2f</code>. 175 */ 176 public float distance(Vec2f v) { 177 float vx = v.x - this.x; 178 float vy = v.y - this.y; 179 return (float) Math.sqrt(vx * vx + vy * vy); 180 } 181 182 /** 183 * Returns the hashcode for this <code>Vec2f</code>. 184 * @return a hash code for this <code>Vec2f</code>. 185 */ 186 @Override 187 public int hashCode() { 188 int bits = 7; 189 bits = 31 * bits + Float.floatToIntBits(x); 190 bits = 31 * bits + Float.floatToIntBits(y); 191 return bits; 192 } 193 194 /** 195 * Determines whether or not two 2D points or vectors are equal. 196 * Two instances of <code>Vec2f</code> are equal if the values of their 197 * <code>x</code> and <code>y</code> member fields, representing 198 * their position in the coordinate space, are the same. 199 * @param obj an object to be compared with this <code>Vec2f</code> 200 * @return <code>true</code> if the object to be compared is 201 * an instance of <code>Vec2f</code> and has 202 * the same values; <code>false</code> otherwise. 203 */ 204 @Override 205 public boolean equals(Object obj) { 206 if (obj == this) return true; 207 if (obj instanceof Vec2f) { 208 Vec2f v = (Vec2f) obj; 209 return (x == v.x) && (y == v.y); 210 } 211 return false; 212 } 213 214 /** 215 * Returns a <code>String</code> that represents the value 216 * of this <code>Vec2f</code>. 217 * @return a string representation of this <code>Vec2f</code>. 218 */ 219 @Override 220 public String toString() { 221 return "Vec2f[" + x + ", " + y + "]"; 222 } 223 }