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