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