Revision 28218 branches/v2_0_0_prep/libraries/libFMap_mapcontext/src/org/gvsig/fmap/mapcontext/rendering/symbols/styles/Line2DOffset.java

View differences:

Line2DOffset.java
67 67

  
68 68
	public static GeneralPathX offsetLine(Shape p, double offset) {
69 69

  
70
		if (Math.abs(offset) <= 1) {
70
		if (Math.abs(offset) < 1) {
71 71
			return new GeneralPathX(p);
72 72
		}
73 73
		PathIterator pi = p.getPathIterator(null);
......
75 75
		Coordinate from = null, first = null;
76 76
		ArrayList segments = new ArrayList(); //<LineSegment>
77 77
		GeneralPathX offsetSegments = new GeneralPathX();
78
		LineSegment line;
78 79
		try {
79 80
			while (!pi.isDone()) {
80 81
				// while not done
......
90 91

  
91 92
					// System.out.println("SEG_LINETO");
92 93
					Coordinate to = new Coordinate(dataCoords[0], dataCoords[1]);
93
					LineSegment line = new LineSegment(from, to);
94
					segments.add(line);
95
					from = to;
94
					if(from.compareTo(to)!=0){
95
						line = new LineSegment(from, to);
96
						segments.add(line);
97
						from = to;
98
					}
96 99
					break;
97 100
				case PathIterator.SEG_CLOSE:
98 101
					line = new LineSegment(from, first);
......
130 133
		for (int i = 0; i < segmentCount; i++) {
131 134
			LineSegment segment = (LineSegment)segments.get(i);
132 135
			double theta = segment.angle();
133
			if (Math.abs(theta) % (Math.PI*0.5) < 0.00001){
134
				theta=theta+0.00000000000001;
135
			}
136
			//FIXME: ?Esto para qu? es?
137
//			if (Math.abs(theta) % (Math.PI*0.5) < 0.00001){
138
//				theta=theta+0.00000000000001;
139
//			}
136 140

  
137 141
			double xOffset = offset * Math.sin(theta);
138 142
			double yOffset = offset * Math.cos(theta);
......
167 171
			}
168 172

  
169 173
			if (i < segmentCount - 1) {
170
				pEnd = eq.resolve((LineEquation)offsetLines.get(segments.get(1)));
174
				LineEquation eq1 = (LineEquation) offsetLines.get(segments.get(1));
175
				try{
176
					pEnd = eq.resolve(eq1);
177
				} catch (ParallelLinesCannotBeResolvedException e) { //Las lineas son paralelas y NO son la misma.
178
					pEnd = new Point2D.Double(eq.xEnd, eq.yEnd);
179
					gpx.append(new Line2D.Double(pIni, pEnd), true); // a?adimos una linea hasta el final del primer segmento
180
//					 y asignamos como punto final el principio del siguiente segmento
181
//					 para que en la siguiente iteraci?n lo tome como punto inicial.
182
					pIni = pEnd;
183
					pEnd = new Point2D.Double(eq1.x, eq1.y);
184
					segments.remove(0);
185
					continue;
186
				}
171 187
			} else {
172 188
				pEnd = new Point2D.Double(eq.xEnd, eq.yEnd);
173 189
			}
......
193 209
}
194 210

  
195 211
class LineEquation {
196
	double theta, x, y;
212
	double theta, m, x, y;
197 213

  
198 214
	double xEnd, yEnd; // just for simplicity of code
199 215

  
200 216
	public LineEquation(double theta, double x, double y, double xEnd,
201 217
			double yEnd) {
202
		this.theta = Math.tan(theta);
218
//		this.theta = Math.tan(theta); //Esto es un error, no podemos confundir el angulo de la recta con su pendiente
219
		this.theta = theta;
220
		this.m = Math.tan(theta);
203 221
		this.x = x;
204 222
		this.y = y;
205 223
		this.xEnd = xEnd;
......
209 227
	public Point2D resolve(LineEquation otherLine)
210 228
	throws ParallelLinesCannotBeResolvedException {
211 229
		/*
212
		 * line1 (this): y - y0 = m*(x - x0) line2 (otherLine): y' - y'0 =
213
		 * m'*(x' - x'0)
230
		 * line1 (this): y - y0 = m*(x - x0)
231
		 * line2 (otherLine): y' - y'0 = m'*(x' - x'0)
214 232
		 */
215
		if (otherLine.theta == this.theta) {
216
			if (this.xEnd == otherLine.x && this.yEnd == otherLine.y) {
217
				return new Point2D.Double(otherLine.x, otherLine.y);
233

  
234
		double X;
235
		double Y;
236
		if(Math.abs(this.x - this.xEnd)<0.00001) { //Esta linea es vertical
237
//			System.out.println("1 VERTICAL");
238
			X = this.xEnd;
239
			if (Math.abs(otherLine.x - otherLine.xEnd)<0.00001){//La otra linea es vertical
240
//				System.out.println("    2 PERPENDICULAR");
241
				if(Math.abs(this.x - otherLine.x)<0.00001){ //Son la misma linea, devolvemos el primer punto de la otra linea.
242
//					System.out.println("        MISMA LINEA");
243
					Y = otherLine.y;
244
				} else { //No son la misma linea, entonces son paralelas, excepcion.
245
//					System.out.println("        CASCO POR 1");
246
					throw new ParallelLinesCannotBeResolvedException(this, otherLine);
247
				}
248
			} else if (Math.abs(otherLine.y - otherLine.yEnd)<0.00001) { //La otra linea es horizontal
249
//				System.out.println("    2 HORIZONTAL");
250
				Y = otherLine.y;
251
			} else { //Si no
252
//				System.out.println("    2 CUALQUIERA");
253
				Y = otherLine.m*(X - otherLine.x)+otherLine.y;
218 254
			}
219
			throw new ParallelLinesCannotBeResolvedException(this, otherLine);
255

  
256
		} else if (Math.abs(this.y - this.yEnd)<0.00001) { //Esta linea es horizontal
257
//			System.out.println("1 HORIZONTAL");
258
			Y = this.yEnd;
259
			if (Math.abs(otherLine.y - otherLine.yEnd)<0.00001) { //La otra linea es horizontal
260
//				System.out.println("    2 HORIZONTAL");
261
				if(Math.abs(this.y - otherLine.y)<0.00001){ //Son la misma linea, devolvemos el primer punto de la otra linea.
262
//					System.out.println("        MISMA LINEA");
263
					X = otherLine.x;
264
				} else { //No son la misma linea, entonces son paralelas, excepcion.
265
//					System.out.println("        CASCO POR 2");
266
					throw new ParallelLinesCannotBeResolvedException(this, otherLine);
267
				}
268
			} else if (Math.abs(otherLine.x - otherLine.xEnd)<0.00001){//La otra linea es vertical
269
//				System.out.println("    2 VERTICAL");
270
				X = otherLine.x;
271
			} else { //Si no
272
//				System.out.println("    2 CUALQUIERA");
273
				X = (Y - otherLine.y)/otherLine.m +otherLine.x;
274
			}
275
		} else { //Esta linea no es ni vertical ni horizontal
276
//			System.out.println("1 CUALQUIERA");
277
			if (Math.abs(otherLine.y - otherLine.yEnd)<0.00001) { //La otra linea es horizontal
278
//				System.out.println("    2 HORIZONTAL");
279
				Y = otherLine.y;
280
				X = (Y - this.y)/this.m +this.x;
281
			} else if (Math.abs(otherLine.x - otherLine.xEnd)<0.00001){//La otra linea es vertical
282
//				System.out.println("    2 VERTICAL");
283
				X = otherLine.x;
284
				Y = this.m*(X - this.x)+this.y;
285
			} else if ((Math.abs(otherLine.m - this.m)<0.00001)) { //Tienen la misma pendiente
286
//				System.out.println("    MISMA PENDIENTE");
287
				Y = otherLine.m*(this.x - otherLine.x)+otherLine.y;
288
				if (Math.abs(this.y - Y)<0.00001){ //Las lineas son la misma
289
//					System.out.println("        MISMA LINEA");
290
					X = otherLine.x;
291
					Y = otherLine.y;
292
				} else {
293
//					System.out.println("        CASCO POR 3");
294
					throw new ParallelLinesCannotBeResolvedException(this, otherLine);
295
				}
296
			} else {
297
//				System.out.println("    AMBAS CUALESQUIERA");
298
				double mTimesX = this.m * this.x;
299
				X = (mTimesX - this.y - otherLine.m * otherLine.x + otherLine.y) / (this.m - otherLine.m);
300
				Y = this.m * X - mTimesX + this.y;
301
			}
220 302
		}
221 303

  
222
		/*
223
		 * m*(X - x0) + y0 = m'*(X - x'0) + y0' X = (m*x0 - y0 - m'*x0' + y'0) /
224
		 * (m - m')
225
		 */
226
		double thetaTimesX = this.theta * this.x;
227
		double X = (thetaTimesX - this.y - otherLine.theta * otherLine.x + otherLine.y)
228
		/ (this.theta - otherLine.theta);
304
//		System.out.println("DEVOLVEMOS X = "+X+" Y = "+Y);
305
		return new Point2D.Double(X, Y);
229 306

  
230
		/*
231
		 * Y - y0 = m*(X - x0) Y = m*X - m*x0 + y0
232
		 */
233
		double Y = this.theta * X - thetaTimesX + this.y;
234
		return new Point2D.Double(X, Y);
235 307
	}
236 308

  
237 309
	public String toString() {
238
		return "Y - " + y + " = " + theta + "*(X - " + x + ")";
310
		return "Y - " + y + " = " + m + "*(X - " + x + ")";
239 311
	}
240 312
}
241 313

  

Also available in: Unified diff