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