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     public Vector(double x, double y) {
  38         this.x = x;
  39         this.y = y;
  40     }
  41 
  42     public Vector(Point from, Point to) {
  43         x = to.x - from.x;
  44         y = to.y - from.y;
  45     }
  46 
  47     public double getX() {
  48         return x;
  49     }
  50 
  51     public double getY() {
  52         return y;
  53     }
  54 
  55     public double lenght() {
  56         return Math.sqrt(x*x + y*y);
  57     }
  58 
  59     public Vector setLenght(double newLenght) {
  60         double lenght = lenght();
  61         x = x * newLenght / lenght;
  62         y = y * newLenght / lenght;
  63         return this;
  64     }
  65 
  66     public Vector multiply(double multiplier) {
  67         x*=multiplier;
  68         y*=multiplier;
  69         return this;
  70     }
  71 
  72     /**
  73      * {@inheritDoc}
  74      */
  75     @Override
  76     public Vector clone() {
  77         return new Vector(x, y);
  78     }
  79 
  80     /**
  81      * {@inheritDoc}
  82      */
  83     @Override
  84     public String toString() {
  85         return "(" + x + "," + y + ")";
  86     }
  87 
  88     public Vector add(Vector v) {
  89         x+=v.x;
  90         y+=v.y;
  91         return this;
  92     }
  93 
  94 }