Changeset 25082 for trunk/magic/remove/src/Line.c
- Timestamp:
- Aug 14, 2009, 3:58:07 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/magic/remove/src/Line.c (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/magic/remove/src/Line.c
r25001 r25082 36 36 // If the current line is not vertical, check to see if the point 37 37 // is inside the two endpoints independent of direction 38 38 39 39 if (line->begin.x != line->end.x) 40 40 { … … 59 59 ux = line->end.x - line->begin.x; 60 60 uy = line->end.y - line->begin.y; 61 61 62 62 vx = x - line->begin.x; 63 63 vy = y - line->begin.y; 64 64 65 65 double u_u = ux * ux + uy * uy; // (u . u) 66 66 double u_v = ux * vx + uy * vy; // (u . v) 67 67 68 68 if (u_v <= 0) return vx * vx + vy * vy; // (v . v) 69 69 if (u_u <= u_v) … … 73 73 return wx * wx + wy * wy; // (w . w) 74 74 } 75 75 76 76 // Compute P(b) is the base of the perpendicular dropped from tuple to 77 77 // the line 78 78 79 79 b = u_v / u_u; 80 80 px = vx - b * ux; … … 109 109 double wx = first->begin.x - second->begin.x; 110 110 double wy = first->begin.y - second->begin.y; 111 111 112 112 // Calculate the perpendicular product, dt 113 113 114 114 double dt = dx1 * dy2 - dy1 * dx2; 115 115 116 116 // Are the lines parallel? 117 117 118 118 if (fabs (dt) <= 1e-8) 119 119 { 120 120 // Check to see if they are overlap 121 121 122 122 if (dx1 * wy - dy1 * wx != 0 || dx2 * wy - dy2 * wx != 0) return 0; 123 123 124 124 // The line are coplanar, so check to see if they are degenerate 125 125 126 126 len1 = dx1 * dx1 + dy1 * dy1; 127 127 len2 = dx2 * dx2 + dy2 * dy2; 128 128 129 129 // First, check to see if both lines are points and if they are 130 130 // distinct tuples 131 131 132 132 if (len1 == 0 && len2 == 0) 133 133 { … … 137 137 return 1; 138 138 } 139 139 140 140 // Check to see if the first line is a point and is inside the 141 141 // second line 142 142 143 143 if (len1 == 0) 144 144 { … … 148 148 return 0; 149 149 } 150 150 151 151 // Check to see if the second line is a point and is inside the 152 152 // first line 153 153 154 154 if (len2 == 0) 155 155 { … … 159 159 return 0; 160 160 } 161 161 162 162 // Since both lines are coplanar and have length, search for 163 163 // overlap tuples 164 164 165 165 w2x = first->end.x - second->begin.x; 166 166 w2y = first->end.y - second->begin.y; 167 167 168 168 t0 = (dx2 != 0) ? wx / dx2 : wy / dy2; 169 169 t1 = (dx2 != 0) ? w2x / dx2 : w2y / dy2; 170 170 171 171 if (t0 > t1) SwapDouble (&t0, &t1); 172 172 if ((inFirstSegment || inSecondSegment) && (t0 > 1 || t1 < 0)) … … 176 176 t0 = (t0 < 0) ? 0 : t0; // Clip to min 0 177 177 t1 = (t1 > 1) ? 1 : t1; // Clip to max 1 178 178 179 179 // Set the first tuple of intersection 180 180 181 181 tuple1->x = second->begin.x + t0 * dx2; 182 182 tuple1->y = second->begin.y + t0 * dy2; 183 183 184 184 // Check to see if the intersection is a single tuple 185 185 186 186 if (t0 == t1) return 1; 187 187 188 188 // Otherwise, set the second tuple of intersection as well 189 189 190 190 tuple2->x = second->begin.x + t1 * dx2; 191 191 tuple2->y = second->begin.y + t1 * dy2; 192 192 return 2; 193 193 } 194 194 195 195 // The segments are skew and may intersect in a tuple. 196 196 // Get the intersect parameter for the first line 197 197 198 198 double i1 = (dx2 * wy - dy2 * wx) / dt; 199 199 if (inFirstSegment && (i1 < 0 || i1 > 1)) return 0; 200 200 201 201 // Get the intersect parameter for the second line 202 202 … … 226 226 vertices[3].x = 0; vertices[3].y = numRows; 227 227 228 clipLine.begin.x = 0; 229 clipLine.begin.y = 0; 230 clipLine.end.x = 0; 231 clipLine.end.y = 0; 232 228 233 for (i = 0; i < 4 && found < 2; ++i) 229 234 { … … 237 242 ++found; 238 243 } 239 else if (tuple1.x != clipLine.begin.x || 244 else if (tuple1.x != clipLine.begin.x || 240 245 tuple1.y != clipLine.begin.y) 241 246 { … … 245 250 } 246 251 } 247 252 248 253 // If two endpoints are found, clip the line 249 254 250 255 if (found > 1) 251 256 { … … 283 288 vertices[3].x = minX; vertices[3].y = maxY; 284 289 290 clipLine.begin.x = 0; 291 clipLine.begin.y = 0; 292 clipLine.end.x = 0; 293 clipLine.end.y = 0; 294 285 295 for (i = 0; i < 4 && found < 2; ++i) 286 296 { … … 294 304 ++found; 295 305 } 296 else if (tuple1.x != clipLine.begin.x || 306 else if (tuple1.x != clipLine.begin.x || 297 307 tuple1.y != clipLine.begin.y) 298 308 { … … 302 312 } 303 313 } 304 314 305 315 // If two endpoints are found, clip the line 306 316 307 317 if (found > 1) 308 318 { … … 360 370 @param[out] pixels list of PixelPos pointers corresponding 361 371 based on the line settings 362 @param[in] line Line to map to pixels 372 @param[in] line Line to map to pixels 363 373 @param[in] numCols maximum X (columns) for the line 364 374 @param[in] numRows maximum Y (rows) for the line */ … … 367 377 { 368 378 Line offsetLine; 369 PixelPos *pixel;370 379 double slope, xOffset, yOffset, xMid, yMid; 371 380 int x, y, xBegin = numCols, yBegin = numRows, xEnd = 0, yEnd = 0; 372 381 373 382 // Extract the endpoints 374 383 375 384 double x1 = line->begin.x; 376 385 double y1 = line->begin.y; 377 386 double x2 = line->end.x; 378 387 double y2 = line->end.y; 379 388 380 389 // Determine the width and height of the line 381 390 382 391 double dx = x2 - x1; 383 392 double dy = y2 - y1; … … 403 412 404 413 // Step point by point based on the dominate axis 405 414 406 415 if (fabs (dx) > fabs (dy)) 407 416 { … … 411 420 // If line is back to front, reorder endpoints and recompute 412 421 // the line width and height 413 422 414 423 if (x1 > x2) 415 424 { … … 421 430 422 431 // Compute the slope of the line 423 432 424 433 slope = dy / dx; 425 434 426 435 // Compute the x and y offsets for the line width extent 427 436 428 if (xBegin > xEnd) 437 if (xBegin > xEnd) 429 438 SwapInt (&xBegin, &xEnd); 430 439 else … … 451 460 } 452 461 else 453 { 462 { 454 463 // ------------------- 455 464 // vertical(ish) lines … … 457 466 // If line is back to front, reorder endpoints and recompute 458 467 // the line width and height 459 468 460 469 if (y1 > y2) 461 470 { … … 465 474 dy = y2 - y1; 466 475 } 467 476 468 477 // Compute the slope of the line 469 478 470 479 slope = dx / dy; 471 480 472 481 // Compute the x and y offsets for the line width extent 473 482 … … 481 490 xMid = x1 + slope * (yBegin - y1); 482 491 xOffset = fabs (halfWidth * dr / dy); 483 492 484 493 for (y = yBegin; y != yEnd; ++y) 485 494 {
Note:
See TracChangeset
for help on using the changeset viewer.
