1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.javafx.geometry; 27 28 import javafx.geometry.Point2D; 29 import javafx.geometry.Point3D; 30 import static org.junit.Assert.assertEquals; 31 import static org.junit.Assert.assertFalse; 32 import static org.junit.Assert.assertNotNull; 33 import static org.junit.Assert.assertTrue; 34 35 import org.junit.Test; 36 37 public class Point2DTest { 38 39 @Test 40 public void testConstruction() { 41 Point2D p = new Point2D(1f, 2f); 42 assertEquals(1f, p.getX(), 1e-100); 43 assertEquals(2f, p.getY(), 1e-100); 44 } 45 46 @Test 47 public void testDistance() { 48 Point2D p1 = new Point2D(0, 0); 49 Point2D p2 = new Point2D(1, 0); 50 Point2D p3 = new Point2D(1, 1); 51 52 assertEquals(1, p2.distance(p1), 1e-100); 53 assertEquals(1, p2.distance(0, 0), 1e-100); 54 assertEquals(1, p2.distance(p3), 1e-100); 55 assertEquals(1.41421356, p1.distance(p3), 1e-5); 56 } 57 58 @Test 59 public void testEquals() { 60 Point2D p1 = new Point2D(0, 0); 61 Point2D p2 = new Point2D(0, 1); 62 Point2D p3 = new Point2D(1, 0); 63 64 assertTrue(p1.equals(p1)); 65 assertTrue(p1.equals(new Point2D(0, 0))); 66 assertFalse(p1.equals(new Object())); 67 assertFalse(p1.equals(p2)); 68 assertFalse(p1.equals(p3)); 69 } 70 71 @Test 72 public void testHash() { 73 Point2D p1 = new Point2D(0, 0); 74 Point2D p2 = new Point2D(0, 1); 75 Point2D p3 = new Point2D(0, 1); 76 77 assertEquals(p3.hashCode(), p2.hashCode()); 78 assertEquals(p3.hashCode(), p2.hashCode()); 79 assertFalse(p1.hashCode() == p2.hashCode()); 80 } 81 82 @Test 83 public void testAdd() { 84 Point2D p1 = new Point2D(2, 4); 85 Point2D p2 = new Point2D(-1, 3); 86 87 assertEquals(new Point2D(1, 7), p1.add(p2)); 88 assertEquals(new Point2D(1, 7), p1.add(-1, 3)); 89 } 90 91 @Test(expected=NullPointerException.class) 92 public void testAddNull() { 93 Point2D point = new Point2D(1, 2); 94 point.add(null); 95 } 96 97 @Test 98 public void testSubtract() { 99 Point2D p1 = new Point2D(2, 4); 100 Point2D p2 = new Point2D(-1, 3); 101 102 assertEquals(new Point2D(3, 1), p1.subtract(p2)); 103 assertEquals(new Point2D(3, 1), p1.subtract(-1, 3)); 104 } 105 106 @Test(expected=NullPointerException.class) 107 public void testSubtractNull() { 108 Point2D point = new Point2D(1, 2); 109 point.subtract(null); 110 } 111 112 @Test 113 public void testMultiplyByNumber() { 114 Point2D p1 = new Point2D(2, 4); 115 Point2D p2 = new Point2D(-1, 3); 116 117 assertEquals(new Point2D(4, 8), p1.multiply(2)); 118 assertEquals(new Point2D(1, -3), p2.multiply(-1)); 119 assertEquals(new Point2D(0, 0), p1.multiply(0)); 120 } 121 122 @Test 123 public void testNormalize() { 124 Point2D p1 = new Point2D(0, 0); 125 Point2D p2 = new Point2D(0, 1); 126 Point2D p3 = new Point2D(1, 1); 127 Point2D p4 = new Point2D(120, -350); 128 129 double sqrt3 = Math.sqrt(2); 130 double sqrt4 = Math.sqrt(136900); 131 132 assertEquals(new Point2D(0, 0), p1.normalize()); 133 assertEquals(new Point2D(0, 1), p2.normalize()); 134 assertEquals(new Point2D(1 / sqrt3, 1 / sqrt3), p3.normalize()); 135 assertEquals(new Point2D(120 / sqrt4, -350 / sqrt4), p4.normalize()); 136 } 137 138 @Test 139 public void testMidpoint() { 140 Point2D p1 = new Point2D(0, 0); 141 Point2D p2 = new Point2D(1, -2); 142 143 assertEquals(new Point2D(0.5, -1), p1.midpoint(p2)); 144 assertEquals(new Point2D(0.5, -1), p1.midpoint(1, -2)); 145 } 146 147 @Test(expected=NullPointerException.class) 148 public void testMidpointNull() { 149 Point2D point = new Point2D(1, 2); 150 point.midpoint(null); 151 } 152 153 @Test 154 public void testVectorAngle() { 155 Point2D p1 = new Point2D(0, 0); 156 Point2D p2 = new Point2D(0, 1); 157 Point2D p3 = new Point2D(1, 1); 158 Point2D p4 = new Point2D(-1, 0); 159 Point2D p5 = new Point2D(10, 10); 160 161 assertEquals(Double.NaN, p1.angle(p2), 0.000001); 162 assertEquals(0, p3.angle(p5), 0.000001); 163 assertEquals(0, p3.angle(p3), 0.000001); 164 assertEquals(45, p2.angle(p3), 0.000001); 165 assertEquals(90, p2.angle(p4), 0.000001); 166 assertEquals(135, p2.angle(-1, -1), 0.000001); 167 assertEquals(Double.NaN, p2.angle(p2, p4), 0.000001); 168 } 169 170 @Test(expected=NullPointerException.class) 171 public void testVectorAngleNull() { 172 Point2D point = new Point2D(1, 2); 173 point.angle(null); 174 } 175 176 @Test 177 public void testPointAngle() { 178 Point2D p1 = new Point2D(2, 2); 179 Point2D p2 = new Point2D(0, 2); 180 Point2D p3 = new Point2D(-3, 2); 181 Point2D p4 = new Point2D(-7, 2); 182 Point2D p5 = new Point2D(-3, 4); 183 Point2D p6 = new Point2D(-5, 2); 184 185 assertEquals(180, p2.angle(p1, p3), 0.000001); 186 assertEquals(90, p3.angle(p5, p6), 0.000001); 187 assertEquals(0, p2.angle(p3, p4), 0.000001); 188 } 189 190 @Test(expected=NullPointerException.class) 191 public void testPointAngle1Null() { 192 Point2D point = new Point2D(1, 2); 193 point.angle(null, new Point2D(2, 8)); 194 } 195 196 @Test(expected=NullPointerException.class) 197 public void testPointAngle2Null() { 198 Point2D point = new Point2D(2, 3); 199 point.angle(new Point2D(5, 3), null); 200 } 201 202 @Test 203 public void testPointAngleTooClose() { 204 Point2D p1 = new Point2D(-0.8944271909999159, 0.4472135954999579); 205 Point2D v = new Point2D(0.0, 0.0); 206 Point2D p2 = new Point2D(-0.894427190999924, 0.4472135954999417); 207 assertEquals(0.0, v.angle(p1, p2), 0.000001); 208 assertEquals(0.0, v.angle(p2, p1), 0.000001); 209 } 210 211 @Test 212 public void testPointAngleTooOpposite() { 213 Point2D p1 = new Point2D(-0.8944271909999159, 0.4472135954999579); 214 Point2D v = new Point2D(0.0, 0.0); 215 Point2D p2 = new Point2D(0.894427190999924, -0.4472135954999417); 216 assertEquals(180.0, v.angle(p1, p2), 0.000001); 217 assertEquals(180.0, v.angle(p2, p1), 0.000001); 218 } 219 220 @Test 221 public void testMagnitude() { 222 Point2D p1 = new Point2D(0, 0); 223 Point2D p2 = new Point2D(0, 1); 224 Point2D p3 = new Point2D(-10, 20); 225 226 assertEquals(0, p1.magnitude(), 0.000001); 227 assertEquals(1, p2.magnitude(), 0.000001); 228 assertEquals(Math.sqrt(500), p3.magnitude(), 0.000001); 229 } 230 231 @Test 232 public void testDotProduct() { 233 Point2D p1 = new Point2D(0, 0); 234 Point2D p2 = new Point2D(1, 1); 235 Point2D p3 = new Point2D(2, -2); 236 Point2D p4 = new Point2D(-4, 5); 237 238 assertEquals(0, p1.dotProduct(p4), 0.000001); 239 assertEquals(1, p2.dotProduct(p4), 0.000001); 240 assertEquals(-18, p3.dotProduct(p4), 0.000001); 241 assertEquals(2, p3.dotProduct(-4, -5), 0.000001); 242 } 243 244 @Test(expected=NullPointerException.class) 245 public void testDotProductNull() { 246 Point2D point = new Point2D(1, 2); 247 point.dotProduct(null); 248 } 249 250 @Test 251 public void testCrossProduct() { 252 Point2D p1 = new Point2D(0, 0); 253 Point2D p2 = new Point2D(0, 3); 254 Point2D p3 = new Point2D(2, 0); 255 256 assertEquals(new Point3D(0, 0, 0), p1.crossProduct(p3)); 257 assertEquals(new Point3D(0, 0, 0), p2.crossProduct(p1)); 258 assertEquals(new Point3D(0, 0, -6), p2.crossProduct(p3)); 259 assertEquals(new Point3D(0, 0, 6), p3.crossProduct(p2)); 260 } 261 262 @Test(expected=NullPointerException.class) 263 public void testCrossProductNull() { 264 Point2D point = new Point2D(1, 2); 265 point.crossProduct(null); 266 } 267 268 @Test 269 public void testAngleTooClose() { 270 Point2D p1 = new Point2D(-0.8944271909999159, 0.4472135954999579); 271 Point2D p2 = new Point2D(-0.894427190999924, 0.4472135954999417); 272 assertEquals(0.0, p1.angle(p2), 0.000001); 273 assertEquals(0.0, p2.angle(p1), 0.000001); 274 } 275 276 @Test 277 public void testAngleTooOpposite() { 278 Point2D p1 = new Point2D(-0.8944271909999159, 0.4472135954999579); 279 Point2D p2 = new Point2D(0.894427190999924, -0.4472135954999417); 280 assertEquals(180.0, p1.angle(p2), 0.000001); 281 assertEquals(180.0, p2.angle(p1), 0.000001); 282 } 283 284 @Test 285 public void testToString() { 286 Point2D p1 = new Point2D(0, 0); 287 assertNotNull(p1.toString()); 288 } 289 }