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. 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 org.jemmy;
  27 
  28 /**
  29  * The class for easy computations.
  30  * @author shura
  31  */
  32 public class Vector {
  33 
  34     private double x;
  35     private double y;
  36 
  37     /**
  38      *
  39      * @param x
  40      * @param y
  41      */
  42     public Vector(double x, double y) {
  43         this.x = x;
  44         this.y = y;
  45     }
  46 
  47     /**
  48      *
  49      * @param from
  50      * @param to
  51      */
  52     public Vector(Point from, Point to) {
  53         x = to.x - from.x;
  54         y = to.y - from.y;
  55     }
  56 
  57     /**
  58      *
  59      * @return
  60      */
  61     public double getX() {
  62         return x;
  63     }
  64 
  65     /**
  66      *
  67      * @return
  68      */
  69     public double getY() {
  70         return y;
  71     }
  72 
  73     /**
  74      *
  75      * @return
  76      */
  77     public double lenght() {
  78         return Math.sqrt(x*x + y*y);
  79     }
  80 
  81     /**
  82      *
  83      * @param newLenght
  84      * @return self
  85      */
  86     public Vector setLenght(double newLenght) {
  87         double lenght = lenght();
  88         x = x * newLenght / lenght;
  89         y = y * newLenght / lenght;
  90         return this;
  91     }
  92 
  93     /**
  94      * @param multiplier
  95      * @return self
  96      */
  97     public Vector multiply(double multiplier) {
  98         x*=multiplier;
  99         y*=multiplier;
 100         return this;
 101     }
 102 
 103     /**
 104      *
 105      * @return a clone
 106      */
 107     @Override
 108     public Vector clone() {
 109         return new Vector(x, y);
 110     }
 111 
 112     /**
 113      *
 114      * @return
 115      */
 116     @Override
 117     public String toString() {
 118         return "(" + x + "," + y + ")";
 119     }
 120 
 121     /**
 122      * Adds another vector <code>(x1 + x2, y1 + y2)</code>
 123      * @param v
 124      * @return self
 125      */
 126     public Vector add(Vector v) {
 127         x+=v.x;
 128         y+=v.y;
 129         return this;
 130     }
 131 
 132 
 133 }