1 /* 2 * Copyright (c) 2007, 2017 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package org.jemmy; 25 26 /** 27 * The class for easy computations. 28 * @author shura 29 */ 30 public class Vector { 31 32 private double x; 33 private double y; 34 35 /** 36 * 37 * @param x 38 * @param y 39 */ 40 public Vector(double x, double y) { 41 this.x = x; 42 this.y = y; 43 } 44 45 /** 46 * 47 * @param from 48 * @param to 49 */ 50 public Vector(Point from, Point to) { 51 x = to.x - from.x; 52 y = to.y - from.y; 53 } 54 55 /** 56 * 57 * @return 58 */ 59 public double getX() { 60 return x; 61 } 62 63 /** 64 * 65 * @return 66 */ 67 public double getY() { 68 return y; 69 } 70 71 /** 72 * 73 * @return 74 */ 75 public double lenght() { 76 return Math.sqrt(x*x + y*y); 77 } 78 79 /** 80 * 81 * @param newLenght 82 * @return self 83 */ 84 public Vector setLenght(double newLenght) { 85 double lenght = lenght(); 86 x = x * newLenght / lenght; 87 y = y * newLenght / lenght; 88 return this; 89 } 90 91 /** 92 * @param multiplier 93 * @return self 94 */ 95 public Vector multiply(double multiplier) { 96 x*=multiplier; 97 y*=multiplier; 98 return this; 99 } 100 101 /** 102 * 103 * @return a clone 104 */ 105 @Override 106 public Vector clone() { 107 return new Vector(x, y); 108 } 109 110 /** 111 * 112 * @return 113 */ 114 @Override 115 public String toString() { 116 return "(" + x + "," + y + ")"; 117 } 118 119 /** 120 * Adds another vector <code>(x1 + x2, y1 + y2)</code> 121 * @param v 122 * @return self 123 */ 124 public Vector add(Vector v) { 125 x+=v.x; 126 y+=v.y; 127 return this; 128 } 129 130 131 }