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@0
|
59 public boolean isParallel(Direction d){
|
fiore@0
|
60 if(equals(d.x,d.y,DELTA)||turn(180).equals(d.x,d.y,DELTA))
|
fiore@0
|
61 return true;
|
fiore@0
|
62 else
|
fiore@0
|
63 return false;
|
fiore@0
|
64 }
|
fiore@0
|
65
|
fiore@0
|
66 /**
|
fiore@0
|
67 Turns this direction by an angle.
|
fiore@0
|
68 @param angle the angle in degrees
|
fiore@0
|
69 */
|
fiore@0
|
70 public Direction turn(double angle){
|
fiore@0
|
71 double a = Math.toRadians(angle);
|
fiore@0
|
72 return new Direction(
|
fiore@0
|
73 x * Math.cos(a) - y * Math.sin(a),
|
fiore@0
|
74 x * Math.sin(a) + y * Math.cos(a));
|
fiore@0
|
75 }
|
fiore@0
|
76
|
fiore@0
|
77 /**
|
fiore@0
|
78 Gets the x-component of this direction
|
fiore@0
|
79 @return the x-component (between -1 and 1)
|
fiore@0
|
80 */
|
fiore@0
|
81 public double getX()
|
fiore@0
|
82 {
|
fiore@0
|
83 return x;
|
fiore@0
|
84 }
|
fiore@0
|
85
|
fiore@0
|
86 /**
|
fiore@0
|
87 Gets the y-component of this direction
|
fiore@0
|
88 @return the y-component (between -1 and 1)
|
fiore@0
|
89 */
|
fiore@0
|
90 public double getY()
|
fiore@0
|
91 {
|
fiore@0
|
92 return y;
|
fiore@0
|
93 }
|
fiore@0
|
94
|
fiore@0
|
95 private boolean equals(double dx, double dy ){
|
fiore@0
|
96 return ((x==dx)&&(y==dy));
|
fiore@0
|
97 }
|
fiore@0
|
98
|
fiore@0
|
99 private boolean equals(double dx, double dy , double delta){
|
fiore@0
|
100 return ((Math.abs(x-dx)<delta)&&(Math.abs(y-dy)<delta));
|
fiore@0
|
101 }
|
fiore@0
|
102
|
fiore@0
|
103 @Override
|
fiore@0
|
104 public String toString(){
|
fiore@0
|
105 return "("+x+","+y+")";
|
fiore@0
|
106 }
|
fiore@0
|
107
|
fiore@0
|
108 private double x;
|
fiore@0
|
109 private double y;
|
fiore@0
|
110
|
fiore@0
|
111 private static final double DELTA = 0.05;
|
fiore@0
|
112
|
fiore@0
|
113 public static final Direction NORTH = new Direction(0, -1);
|
fiore@0
|
114 public static final Direction SOUTH = new Direction(0, 1);
|
fiore@0
|
115 public static final Direction EAST = new Direction(1, 0);
|
fiore@0
|
116 public static final Direction WEST = new Direction(-1, 0);
|
fiore@0
|
117 public static final Direction NONE = new Direction(0, 0);
|
fiore@0
|
118
|
fiore@0
|
119 public static Direction compute(Point2D p, Point2D q){
|
fiore@0
|
120 double x,y;
|
fiore@0
|
121 x = p.getX() - q.getX();
|
fiore@0
|
122 y = p.getY() - q.getY();
|
fiore@0
|
123 double length = Math.sqrt(x * x + y * y);
|
fiore@0
|
124 if (length == 0)
|
fiore@0
|
125 return NONE;
|
fiore@0
|
126 x = x / length;
|
fiore@0
|
127 y = y / length;
|
fiore@0
|
128 if(NORTH.equals(x, y))
|
fiore@0
|
129 return NORTH;
|
fiore@0
|
130 if(SOUTH.equals(x, y))
|
fiore@0
|
131 return SOUTH;
|
fiore@0
|
132 if(EAST.equals(x, y))
|
fiore@0
|
133 return EAST;
|
fiore@0
|
134 if(WEST.equals(x, y))
|
fiore@0
|
135 return WEST;
|
fiore@0
|
136 return NONE;
|
fiore@0
|
137 }
|
fiore@0
|
138 }
|