f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0:
f@0: package uk.ac.qmul.eecs.ccmi.gui;
f@0:
f@0: import java.awt.geom.Point2D;
f@0:
f@0: /**
f@0: This class describes a direction in the 2D plane.
f@0: A direction is a vector of length 1 with an angle between 0
f@0: (inclusive) and 360 degrees (exclusive). There is also
f@0: a degenerate direction of length 0.
f@0: */
f@0: public class Direction
f@0: {
f@0: /**
f@0: Constructs a direction (normalized to length 1).
f@0: @param dx the x-value of the direction
f@0: @param dy the corresponding y-value of the direction
f@0: */
f@0: public Direction(double dx, double dy)
f@0: {
f@0: x = dx;
f@0: y = dy;
f@0: double length = Math.sqrt(x * x + y * y);
f@0: if (length == 0) return;
f@0: x = x / length;
f@0: y = y / length;
f@0: }
f@0:
f@0: /**
f@0: Constructs a direction between two points
f@0: @param p the starting point
f@0: @param q the ending point
f@0: */
f@0: public Direction(Point2D p, Point2D q)
f@0: {
f@0: this(q.getX() - p.getX(),
f@0: q.getY() - p.getY());
f@0: }
f@0:
f@0: /**
f@0: * Checks whether the direction passed as argument is parallel to this direction.
f@0: *
f@0: * @param d the direction to check against
f@0: * @return {@code true} if this direction and {@code d} are parallel to each other, false otherwise
f@0: */
f@0: public boolean isParallel(Direction d){
f@0: if(equals(d.x,d.y,DELTA)||turn(180).equals(d.x,d.y,DELTA))
f@0: return true;
f@0: else
f@0: return false;
f@0: }
f@0:
f@0: /**
f@0: Turns this direction by an angle.
f@0: @param angle the angle in degrees
f@0:
f@0: @return a new object representing the turned direction
f@0: */
f@0: public Direction turn(double angle){
f@0: double a = Math.toRadians(angle);
f@0: return new Direction(
f@0: x * Math.cos(a) - y * Math.sin(a),
f@0: x * Math.sin(a) + y * Math.cos(a));
f@0: }
f@0:
f@0: /**
f@0: Gets the x-component of this direction
f@0: @return the x-component (between -1 and 1)
f@0: */
f@0: public double getX() {
f@0: return x;
f@0: }
f@0:
f@0: /**
f@0: Gets the y-component of this direction
f@0: @return the y-component (between -1 and 1)
f@0: */
f@0: public double getY() {
f@0: return y;
f@0: }
f@0:
f@0: private boolean equals(double dx, double dy ){
f@0: return ((x==dx)&&(y==dy));
f@0: }
f@0:
f@0: private boolean equals(double dx, double dy , double delta){
f@0: return ((Math.abs(x-dx)