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