MATSIM
VectorUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ScaleEndPointUtil
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package org.matsim.vis.vecmathutils;
21 
22 import java.awt.geom.Point2D;
23 
25 
32 public class VectorUtils {
33 
40  public static Tuple<Point2D.Double, Point2D.Double> scaleVector(final Point2D.Double start, final Point2D.Double end,
41  double scaleFactor) {
42 
43  return calculateScaledVector(start, end, scaleFactor);
44  }
45 
53  @Deprecated
54  public static Tuple<Point2D.Double, Point2D.Double> scaleVectorStart(final Point2D.Double start, final Point2D.Double end,
55  double scaleFactor) {
56 
57  Tuple<Point2D.Double, Point2D.Double> scaledVector = calculateScaledVector(start, end, scaleFactor);
58  return new Tuple<Point2D.Double, Point2D.Double>(scaledVector.getFirst(), end);
59  }
60 
68  @Deprecated
69  public static Tuple<Point2D.Double, Point2D.Double> scaleVectorEnd(final Point2D.Double start, final Point2D.Double end,
70  double scaleFactor) {
71 
72  Tuple<Point2D.Double, Point2D.Double> scaledVector = calculateScaledVector(start, end, scaleFactor);
73  return new Tuple<Point2D.Double, Point2D.Double>(start, scaledVector.getSecond());
74  }
75 
76 
77  private static Tuple<Point2D.Double, Point2D.Double> calculateScaledVector(
78  final Point2D.Double start, final Point2D.Double end,
79  double scaleFactor) {
80  //norm
81  Point2D.Double.Double delta = new Point2D.Double.Double(end.x - start.x, end.y - start.y);
82  double length = Math.sqrt(Math.pow(delta.x, 2) + Math.pow(delta.y, 2));
83  Point2D.Double.Double deltaNorm = new Point2D.Double.Double(delta.x / length, delta.y / length);
84  //scale
85  double scaledLength = length * scaleFactor;
86  double offset = (length - scaledLength) / 2;
87  Point2D.Double scaledEnd = new Point2D.Double(start.x + ((length - offset) * deltaNorm.x), start.y
88  + ((length - offset) * deltaNorm.y));
89  Point2D.Double scaledStart = new Point2D.Double(start.x
90  + (offset * deltaNorm.x), start.y
91  + (offset * deltaNorm.y));
92  Tuple<Point2D.Double, Point2D.Double> scaledVector = new Tuple<Point2D.Double, Point2D.Double>(scaledStart, scaledEnd);
93  return scaledVector;
94  }
95 }
static Tuple< Point2D.Double, Point2D.Double > scaleVector(final Point2D.Double start, final Point2D.Double end, double scaleFactor)
static Tuple< Point2D.Double, Point2D.Double > scaleVectorStart(final Point2D.Double start, final Point2D.Double end, double scaleFactor)
static Tuple< Point2D.Double, Point2D.Double > scaleVectorEnd(final Point2D.Double start, final Point2D.Double end, double scaleFactor)
static Tuple< Point2D.Double, Point2D.Double > calculateScaledVector(final Point2D.Double start, final Point2D.Double end, double scaleFactor)