annotate java/src/uk/ac/qmul/eecs/ccmi/gui/Direction.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 6
f@0 7 This program is free software: you can redistribute it and/or modify
f@0 8 it under the terms of the GNU General Public License as published by
f@0 9 the Free Software Foundation, either version 3 of the License, or
f@0 10 (at your option) any later version.
f@0 11
f@0 12 This program is distributed in the hope that it will be useful,
f@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 15 GNU General Public License for more details.
f@0 16
f@0 17 You should have received a copy of the GNU General Public License
f@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 19 */
f@0 20
f@0 21 package uk.ac.qmul.eecs.ccmi.gui;
f@0 22
f@0 23 import java.awt.geom.Point2D;
f@0 24
f@0 25 /**
f@0 26 This class describes a direction in the 2D plane.
f@0 27 A direction is a vector of length 1 with an angle between 0
f@0 28 (inclusive) and 360 degrees (exclusive). There is also
f@0 29 a degenerate direction of length 0.
f@0 30 */
f@0 31 public class Direction
f@0 32 {
f@0 33 /**
f@0 34 Constructs a direction (normalized to length 1).
f@0 35 @param dx the x-value of the direction
f@0 36 @param dy the corresponding y-value of the direction
f@0 37 */
f@0 38 public Direction(double dx, double dy)
f@0 39 {
f@0 40 x = dx;
f@0 41 y = dy;
f@0 42 double length = Math.sqrt(x * x + y * y);
f@0 43 if (length == 0) return;
f@0 44 x = x / length;
f@0 45 y = y / length;
f@0 46 }
f@0 47
f@0 48 /**
f@0 49 Constructs a direction between two points
f@0 50 @param p the starting point
f@0 51 @param q the ending point
f@0 52 */
f@0 53 public Direction(Point2D p, Point2D q)
f@0 54 {
f@0 55 this(q.getX() - p.getX(),
f@0 56 q.getY() - p.getY());
f@0 57 }
f@0 58
f@0 59 /**
f@0 60 * Checks whether the direction passed as argument is parallel to this direction.
f@0 61 *
f@0 62 * @param d the direction to check against
f@0 63 * @return {@code true} if this direction and {@code d} are parallel to each other, false otherwise
f@0 64 */
f@0 65 public boolean isParallel(Direction d){
f@0 66 if(equals(d.x,d.y,DELTA)||turn(180).equals(d.x,d.y,DELTA))
f@0 67 return true;
f@0 68 else
f@0 69 return false;
f@0 70 }
f@0 71
f@0 72 /**
f@0 73 Turns this direction by an angle.
f@0 74 @param angle the angle in degrees
f@0 75
f@0 76 @return a new object representing the turned direction
f@0 77 */
f@0 78 public Direction turn(double angle){
f@0 79 double a = Math.toRadians(angle);
f@0 80 return new Direction(
f@0 81 x * Math.cos(a) - y * Math.sin(a),
f@0 82 x * Math.sin(a) + y * Math.cos(a));
f@0 83 }
f@0 84
f@0 85 /**
f@0 86 Gets the x-component of this direction
f@0 87 @return the x-component (between -1 and 1)
f@0 88 */
f@0 89 public double getX() {
f@0 90 return x;
f@0 91 }
f@0 92
f@0 93 /**
f@0 94 Gets the y-component of this direction
f@0 95 @return the y-component (between -1 and 1)
f@0 96 */
f@0 97 public double getY() {
f@0 98 return y;
f@0 99 }
f@0 100
f@0 101 private boolean equals(double dx, double dy ){
f@0 102 return ((x==dx)&&(y==dy));
f@0 103 }
f@0 104
f@0 105 private boolean equals(double dx, double dy , double delta){
f@0 106 return ((Math.abs(x-dx)<delta)&&(Math.abs(y-dy)<delta));
f@0 107 }
f@0 108
f@0 109 @Override
f@0 110 public String toString(){
f@0 111 return "("+x+","+y+")";
f@0 112 }
f@0 113
f@0 114 private double x;
f@0 115 private double y;
f@0 116
f@0 117 private static final double DELTA = 0.05;
f@0 118
f@0 119 public static final Direction NORTH = new Direction(0, -1);
f@0 120 public static final Direction SOUTH = new Direction(0, 1);
f@0 121 public static final Direction EAST = new Direction(1, 0);
f@0 122 public static final Direction WEST = new Direction(-1, 0);
f@0 123 public static final Direction NONE = new Direction(0, 0);
f@0 124
f@0 125 public static Direction compute(Point2D p, Point2D q){
f@0 126 double x,y;
f@0 127 x = p.getX() - q.getX();
f@0 128 y = p.getY() - q.getY();
f@0 129 double length = Math.sqrt(x * x + y * y);
f@0 130 if (length == 0)
f@0 131 return NONE;
f@0 132 x = x / length;
f@0 133 y = y / length;
f@0 134 if(NORTH.equals(x, y))
f@0 135 return NORTH;
f@0 136 if(SOUTH.equals(x, y))
f@0 137 return SOUTH;
f@0 138 if(EAST.equals(x, y))
f@0 139 return EAST;
f@0 140 if(WEST.equals(x, y))
f@0 141 return WEST;
f@0 142 return NONE;
f@0 143 }
f@0 144 }