diff java/src/uk/ac/qmul/eecs/ccmi/gui/Direction.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/gui/Direction.java	Tue Jul 08 16:28:59 2014 +0100
@@ -0,0 +1,144 @@
+/*  
+ CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
+	  
+ Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
+ Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package uk.ac.qmul.eecs.ccmi.gui;
+
+import java.awt.geom.Point2D;
+
+/**
+   This class describes a direction in the 2D plane. 
+   A direction is a vector of length 1 with an angle between 0 
+   (inclusive) and 360 degrees (exclusive). There is also
+   a degenerate direction of length 0. 
+*/
+public class Direction
+{
+   /**
+      Constructs a direction (normalized to length 1).
+      @param dx the x-value of the direction
+      @param dy the corresponding y-value of the direction
+   */
+   public Direction(double dx, double dy)
+   {
+      x = dx;
+      y = dy;
+      double length = Math.sqrt(x * x + y * y);
+      if (length == 0) return;
+      x = x / length;
+      y = y / length;
+   }
+
+   /**
+      Constructs a direction between two points
+      @param p the starting point
+      @param q the ending point
+   */
+   public Direction(Point2D p, Point2D q)
+   {
+      this(q.getX() - p.getX(),
+         q.getY() - p.getY());
+   }
+   
+   /**
+    * Checks whether the direction passed as argument is parallel to this direction.
+    * 
+    * @param d the direction to check against 
+    * @return {@code true} if this direction and {@code d} are parallel to each other, false otherwise
+    */
+   public boolean isParallel(Direction d){
+	   if(equals(d.x,d.y,DELTA)||turn(180).equals(d.x,d.y,DELTA))
+		   return true;
+	   else 
+		   return false;
+   }
+
+   /**
+      Turns this direction by an angle.
+      @param angle the angle in degrees
+      
+      @return a new object representing the turned direction 
+   */
+   public Direction turn(double angle){
+      double a = Math.toRadians(angle);
+      return new Direction(
+         x * Math.cos(a) - y * Math.sin(a),
+         x * Math.sin(a) + y * Math.cos(a));
+   }
+
+   /**
+      Gets the x-component of this direction
+      @return the x-component (between -1 and 1)
+   */
+   public double getX() {
+      return x;
+   }
+
+   /**
+      Gets the y-component of this direction
+      @return the y-component (between -1 and 1)
+   */
+   public double getY() {
+      return y;
+   }
+   
+   private boolean equals(double dx, double dy ){
+	   return ((x==dx)&&(y==dy));
+   }
+   
+   private boolean equals(double dx, double dy , double delta){
+	   return ((Math.abs(x-dx)<delta)&&(Math.abs(y-dy)<delta));
+   }
+   
+   @Override
+   public String toString(){
+	   return "("+x+","+y+")";
+   }
+
+   private double x;
+   private double y;
+   
+   private static final double DELTA = 0.05;
+
+   public static final Direction NORTH = new Direction(0, -1);
+   public static final Direction SOUTH = new Direction(0, 1);
+   public static final Direction EAST = new Direction(1, 0);
+   public static final Direction WEST = new Direction(-1, 0);
+   public static final Direction NONE = new Direction(0, 0);
+   
+   public static Direction compute(Point2D p, Point2D q){
+	  double x,y;
+	  x = p.getX() - q.getX();
+	  y = p.getY() - q.getY();
+      double length = Math.sqrt(x * x + y * y);
+      if (length == 0) 
+    	  return NONE;
+      x = x / length;
+      y = y / length;
+      if(NORTH.equals(x, y))
+    	  return NORTH;
+      if(SOUTH.equals(x, y))
+    	  return SOUTH;
+      if(EAST.equals(x, y))
+    	  return EAST;
+      if(WEST.equals(x, y))
+    	  return WEST;
+      return NONE;
+   }
+}