Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/ShapeEdge.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /**
2 * ditaa - Diagrams Through Ascii Art
3 *
•4 * Copyright (C) 2004-2011 Efstathios Sideris
5 *
•6 * ditaa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
•11 * ditaa is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
•14 * GNU Lesser General Public License for more details.
15 *
•16 * You should have received a copy of the GNU Lesser General Public
17 * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package org.stathissideris.ascii2image.graphics;
21
22 import java.awt.geom.GeneralPath;
23
24 /**
25 *
26 * @author Efstathios Sideris
27 */
28 public class ShapeEdge {
29
30 private static final boolean DEBUG = false;
31
32 private static final int TYPE_HORIZONTAL = 0;
33 private static final int TYPE_VERTICAL = 1;
34 private static final int TYPE_SLOPED = 2;
35
36
37 private DiagramShape owner;
38 private ShapePoint startPoint;
39 private ShapePoint endPoint;
40
41 public ShapeEdge(ShapePoint start, ShapePoint end, DiagramShape owner){
42 this.startPoint = start;
43 this.endPoint = end;
44 this.owner = owner;
45 }
46
47 public ShapeEdge(ShapeEdge other){
48 this(
49 new ShapePoint(other.startPoint),
50 new ShapePoint(other.endPoint),
51 other.owner
52 );
53 }
54
55 private float getDistanceFromOrigin() {
56 int type = this.getType();
57 if(type == TYPE_SLOPED)
58 throw new RuntimeException("Cannot calculate distance of sloped edge from origin");
59 if(type == TYPE_HORIZONTAL)
60 return startPoint.y;
61 return startPoint.x; //vertical
62 }
63
64 //TODO: moveInwardsBy() not implemented
65 public void moveInwardsBy(float offset){
66 int type = this.getType();
67 if(type == TYPE_SLOPED)
•68 throw new RuntimeException("Cannot move a sloped edge inwards: "+this);
69
70 float xOffset = 0;
71 float yOffset = 0;
72
73 ShapePoint middle = getMiddle();
74 GeneralPath path = owner.makeIntoPath();
75 if(type == TYPE_HORIZONTAL){
76 xOffset = 0;
77 ShapePoint up = new ShapePoint(middle.x, middle.y - 0.05f);
78 ShapePoint down = new ShapePoint(middle.x, middle.y + 0.05f);
79 if(path.contains(up)) yOffset = -offset;
80 else if(path.contains(down)) yOffset = offset;
81 } else if(type == TYPE_VERTICAL){
82 yOffset = 0;
83 ShapePoint left = new ShapePoint(middle.x - 0.05f, middle.y);
84 ShapePoint right = new ShapePoint(middle.x + 0.05f, middle.y);
85 if(path.contains(left)) xOffset = -offset;
86 else if(path.contains(right)) xOffset = offset;
87 }
88 if(DEBUG) System.out.println("Moved edge "+this+" by "+xOffset+", "+yOffset);
89 translate(xOffset, yOffset);
90 }
91
92 public void translate(float dx, float dy){
93 startPoint.x += dx;
94 startPoint.y += dy;
95 endPoint.x += dx;
96 endPoint.y += dy;
97 }
98
99 public ShapePoint getMiddle(){
100 return new ShapePoint(
101 (startPoint.x + endPoint.x) / 2,
102 (startPoint.y + endPoint.y) / 2
103 );
104 }
105
106 /**
107 * Returns the type of the edge
108 * (<code>TYPE_HORIZONTAL, TYPE_VERTICAL, TYPE_SLOPED).
109 *
110 * @return
111 */
112 private int getType(){
113 if(isVertical()) return TYPE_VERTICAL;
114 if(isHorizontal()) return TYPE_HORIZONTAL;
115 return TYPE_SLOPED;
116 }
117
118 /**
119 * @return
120 */
121 public ShapePoint getEndPoint() {
122 return endPoint;
123 }
124
125 /**
126 * @return
127 */
128 public ShapePoint getStartPoint() {
129 return startPoint;
130 }
131
132 /**
133 * @param point
134 */
135 public void setEndPoint(ShapePoint point) {
136 endPoint = point;
137 }
138
139 /**
140 * @param point
141 */
142 public void setStartPoint(ShapePoint point) {
143 startPoint = point;
144 }
145
146 /**
147 * @return
148 */
149 public DiagramShape getOwner() {
150 return owner;
151 }
152
153 /**
154 * @param shape
155 */
156 public void setOwner(DiagramShape shape) {
157 owner = shape;
158 }
159
160 public boolean equals(Object object){
161 if(!(object instanceof ShapeEdge)) return false;
162 ShapeEdge edge = (ShapeEdge) object;
163 if(startPoint.equals(edge.getStartPoint())
164 && endPoint.equals(edge.getEndPoint())) return true;
165 if(startPoint.equals(edge.getEndPoint())
166 && endPoint.equals(edge.getStartPoint())) return true;
167 return false;
168 }
169
170 public boolean touchesWith(ShapeEdge other){
171 if(this.equals(other)) return true;
172
173 if(this.isHorizontal() && other.isVertical()) return false;
174 if(other.isHorizontal() && this.isVertical()) return false;
175
176 if(this.getDistanceFromOrigin() != other.getDistanceFromOrigin()) return false;
177
178 //covering this corner case (should produce false):
179 // ---------
180 // ---------
181
182 ShapeEdge first = new ShapeEdge(this);
183 ShapeEdge second = new ShapeEdge(other);
184
185 if(first.isVertical()) {
186 first.changeAxis();
187 second.changeAxis();
188 }
189
190 first.fixDirection();
191 second.fixDirection();
192
193 if(first.startPoint.x > second.startPoint.x) {
194 ShapeEdge temp = first;
195 first = second;
196 second = temp;
197 }
198
199 if(first.endPoint.equals(second.startPoint)) return false;
200
201 // case 1:
202 // ----------
203 // -----------
204
205 // case 2:
206 // ------
207 // -----------------
208
209 if(this.startPoint.isWithinEdge(other) || this.endPoint.isWithinEdge(other)) return true;
210 if(other.startPoint.isWithinEdge(this) || other.endPoint.isWithinEdge(this)) return true;
211
212
213 return false;
214 }
215
216 private void changeAxis(){
217 ShapePoint temp = new ShapePoint(startPoint);
218 startPoint = new ShapePoint(endPoint.y, endPoint.x);
219 endPoint = new ShapePoint(temp.y, temp.x);
220 }
221
222 /**
223 * if horizontal flips start and end points so that start is left of end
•224 * if vertical flips start and end points so that start is over of end
225 *
226 */
227 private void fixDirection(){
228 if(isHorizontal()) {
229 if(startPoint.x > endPoint.x) flipDirection();
230 } else if(isVertical()) {
231 if(startPoint.y > endPoint.y) flipDirection();
232 } else {
•233 throw new RuntimeException("Cannot fix direction of sloped edge");
234 }
235 }
236
237 private void flipDirection(){
238 ShapePoint temp = startPoint;
239 startPoint = endPoint;
240 endPoint = temp;
241 }
242
243 public boolean isHorizontal(){
244 if(startPoint.y == endPoint.y) return true;
245 return false;
246 }
247
248 public boolean isVertical(){
249 if(startPoint.x == endPoint.x) return true;
250 return false;
251 }
252
253 public String toString(){
254 return startPoint+" -> "+endPoint;
255 }
256
257 }