Debian Codegraph @

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

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