File size: 14,080 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
/**
* This class maintains a list of static geometry related utility methods.
*
*
* Copyright: i-Vis Research Group, Bilkent University, 2007 - present
*/
const Point = require('./Point');
function IGeometry() {
}
/**
* This method calculates *half* the amount in x and y directions of the two
* input rectangles needed to separate them keeping their respective
* positioning, and returns the result in the input array. An input
* separation buffer added to the amount in both directions. We assume that
* the two rectangles do intersect.
*/
IGeometry.calcSeparationAmount = function (rectA, rectB, overlapAmount, separationBuffer)
{
if (!rectA.intersects(rectB)) {
throw "assert failed";
}
let directions = new Array(2);
this.decideDirectionsForOverlappingNodes(rectA, rectB, directions);
overlapAmount[0] = Math.min(rectA.getRight(), rectB.getRight()) -
Math.max(rectA.x, rectB.x);
overlapAmount[1] = Math.min(rectA.getBottom(), rectB.getBottom()) -
Math.max(rectA.y, rectB.y);
// update the overlapping amounts for the following cases:
if ((rectA.getX() <= rectB.getX()) && (rectA.getRight() >= rectB.getRight()))
{
/* Case x.1:
*
* rectA
* | |
* | _________ |
* | | | |
* |________|_______|______|
* | |
* | |
* rectB
*/
overlapAmount[0] += Math.min((rectB.getX() - rectA.getX()),
(rectA.getRight() - rectB.getRight()));
}
else if ((rectB.getX() <= rectA.getX()) && (rectB.getRight() >= rectA.getRight()))
{
/* Case x.2:
*
* rectB
* | |
* | _________ |
* | | | |
* |________|_______|______|
* | |
* | |
* rectA
*/
overlapAmount[0] += Math.min((rectA.getX() - rectB.getX()),
(rectB.getRight() - rectA.getRight()));
}
if ((rectA.getY() <= rectB.getY()) && (rectA.getBottom() >= rectB.getBottom()))
{
/* Case y.1:
* ________ rectA
* |
* |
* ______|____ rectB
* | |
* | |
* ______|____|
* |
* |
* |________
*
*/
overlapAmount[1] += Math.min((rectB.getY() - rectA.getY()),
(rectA.getBottom() - rectB.getBottom()));
}
else if ((rectB.getY() <= rectA.getY()) && (rectB.getBottom() >= rectA.getBottom()))
{
/* Case y.2:
* ________ rectB
* |
* |
* ______|____ rectA
* | |
* | |
* ______|____|
* |
* |
* |________
*
*/
overlapAmount[1] += Math.min((rectA.getY() - rectB.getY()),
(rectB.getBottom() - rectA.getBottom()));
}
// find slope of the line passes two centers
let slope = Math.abs((rectB.getCenterY() - rectA.getCenterY()) /
(rectB.getCenterX() - rectA.getCenterX()));
// if centers are overlapped
if ((rectB.getCenterY() === rectA.getCenterY()) &&
(rectB.getCenterX() === rectA.getCenterX()))
{
// assume the slope is 1 (45 degree)
slope = 1.0;
}
let moveByY = slope * overlapAmount[0];
let moveByX = overlapAmount[1] / slope;
if (overlapAmount[0] < moveByX)
{
moveByX = overlapAmount[0];
}
else
{
moveByY = overlapAmount[1];
}
// return half the amount so that if each rectangle is moved by these
// amounts in opposite directions, overlap will be resolved
overlapAmount[0] = -1 * directions[0] * ((moveByX / 2) + separationBuffer);
overlapAmount[1] = -1 * directions[1] * ((moveByY / 2) + separationBuffer);
};
/**
* This method decides the separation direction of overlapping nodes
*
* if directions[0] = -1, then rectA goes left
* if directions[0] = 1, then rectA goes right
* if directions[1] = -1, then rectA goes up
* if directions[1] = 1, then rectA goes down
*/
IGeometry.decideDirectionsForOverlappingNodes = function (rectA, rectB, directions)
{
if (rectA.getCenterX() < rectB.getCenterX())
{
directions[0] = -1;
}
else
{
directions[0] = 1;
}
if (rectA.getCenterY() < rectB.getCenterY())
{
directions[1] = -1;
}
else
{
directions[1] = 1;
}
};
/**
* This method calculates the intersection (clipping) points of the two
* input rectangles with line segment defined by the centers of these two
* rectangles. The clipping points are saved in the input double array and
* whether or not the two rectangles overlap is returned.
*/
IGeometry.getIntersection2 = function(rectA, rectB, result)
{
//result[0-1] will contain clipPoint of rectA, result[2-3] will contain clipPoint of rectB
let p1x = rectA.getCenterX();
let p1y = rectA.getCenterY();
let p2x = rectB.getCenterX();
let p2y = rectB.getCenterY();
//if two rectangles intersect, then clipping points are centers
if (rectA.intersects(rectB))
{
result[0] = p1x;
result[1] = p1y;
result[2] = p2x;
result[3] = p2y;
return true;
}
//variables for rectA
let topLeftAx = rectA.getX();
let topLeftAy = rectA.getY();
let topRightAx = rectA.getRight();
let bottomLeftAx = rectA.getX();
let bottomLeftAy = rectA.getBottom();
let bottomRightAx = rectA.getRight();
let halfWidthA = rectA.getWidthHalf();
let halfHeightA = rectA.getHeightHalf();
//variables for rectB
let topLeftBx = rectB.getX();
let topLeftBy = rectB.getY();
let topRightBx = rectB.getRight();
let bottomLeftBx = rectB.getX();
let bottomLeftBy = rectB.getBottom();
let bottomRightBx = rectB.getRight();
let halfWidthB = rectB.getWidthHalf();
let halfHeightB = rectB.getHeightHalf();
//flag whether clipping points are found
let clipPointAFound = false;
let clipPointBFound = false;
// line is vertical
if (p1x === p2x)
{
if (p1y > p2y)
{
result[0] = p1x;
result[1] = topLeftAy;
result[2] = p2x;
result[3] = bottomLeftBy;
return false;
}
else if (p1y < p2y)
{
result[0] = p1x;
result[1] = bottomLeftAy;
result[2] = p2x;
result[3] = topLeftBy;
return false;
}
else
{
//not line, return null;
}
}
// line is horizontal
else if (p1y === p2y)
{
if (p1x > p2x)
{
result[0] = topLeftAx;
result[1] = p1y;
result[2] = topRightBx;
result[3] = p2y;
return false;
}
else if (p1x < p2x)
{
result[0] = topRightAx;
result[1] = p1y;
result[2] = topLeftBx;
result[3] = p2y;
return false;
}
else
{
//not valid line, return null;
}
}
else
{
//slopes of rectA's and rectB's diagonals
let slopeA = rectA.height / rectA.width;
let slopeB = rectB.height / rectB.width;
//slope of line between center of rectA and center of rectB
let slopePrime = (p2y - p1y) / (p2x - p1x);
let cardinalDirectionA;
let cardinalDirectionB;
let tempPointAx;
let tempPointAy;
let tempPointBx;
let tempPointBy;
//determine whether clipping point is the corner of nodeA
if ((-slopeA) === slopePrime)
{
if (p1x > p2x)
{
result[0] = bottomLeftAx;
result[1] = bottomLeftAy;
clipPointAFound = true;
}
else
{
result[0] = topRightAx;
result[1] = topLeftAy;
clipPointAFound = true;
}
}
else if (slopeA === slopePrime)
{
if (p1x > p2x)
{
result[0] = topLeftAx;
result[1] = topLeftAy;
clipPointAFound = true;
}
else
{
result[0] = bottomRightAx;
result[1] = bottomLeftAy;
clipPointAFound = true;
}
}
//determine whether clipping point is the corner of nodeB
if ((-slopeB) === slopePrime)
{
if (p2x > p1x)
{
result[2] = bottomLeftBx;
result[3] = bottomLeftBy;
clipPointBFound = true;
}
else
{
result[2] = topRightBx;
result[3] = topLeftBy;
clipPointBFound = true;
}
}
else if (slopeB === slopePrime)
{
if (p2x > p1x)
{
result[2] = topLeftBx;
result[3] = topLeftBy;
clipPointBFound = true;
}
else
{
result[2] = bottomRightBx;
result[3] = bottomLeftBy;
clipPointBFound = true;
}
}
//if both clipping points are corners
if (clipPointAFound && clipPointBFound)
{
return false;
}
//determine Cardinal Direction of rectangles
if (p1x > p2x)
{
if (p1y > p2y)
{
cardinalDirectionA = this.getCardinalDirection(slopeA, slopePrime, 4);
cardinalDirectionB = this.getCardinalDirection(slopeB, slopePrime, 2);
}
else
{
cardinalDirectionA = this.getCardinalDirection(-slopeA, slopePrime, 3);
cardinalDirectionB = this.getCardinalDirection(-slopeB, slopePrime, 1);
}
}
else
{
if (p1y > p2y)
{
cardinalDirectionA = this.getCardinalDirection(-slopeA, slopePrime, 1);
cardinalDirectionB = this.getCardinalDirection(-slopeB, slopePrime, 3);
}
else
{
cardinalDirectionA = this.getCardinalDirection(slopeA, slopePrime, 2);
cardinalDirectionB = this.getCardinalDirection(slopeB, slopePrime, 4);
}
}
//calculate clipping Point if it is not found before
if (!clipPointAFound)
{
switch (cardinalDirectionA)
{
case 1:
tempPointAy = topLeftAy;
tempPointAx = p1x + (-halfHeightA) / slopePrime;
result[0] = tempPointAx;
result[1] = tempPointAy;
break;
case 2:
tempPointAx = bottomRightAx;
tempPointAy = p1y + halfWidthA * slopePrime;
result[0] = tempPointAx;
result[1] = tempPointAy;
break;
case 3:
tempPointAy = bottomLeftAy;
tempPointAx = p1x + halfHeightA / slopePrime;
result[0] = tempPointAx;
result[1] = tempPointAy;
break;
case 4:
tempPointAx = bottomLeftAx;
tempPointAy = p1y + (-halfWidthA) * slopePrime;
result[0] = tempPointAx;
result[1] = tempPointAy;
break;
}
}
if (!clipPointBFound)
{
switch (cardinalDirectionB)
{
case 1:
tempPointBy = topLeftBy;
tempPointBx = p2x + (-halfHeightB) / slopePrime;
result[2] = tempPointBx;
result[3] = tempPointBy;
break;
case 2:
tempPointBx = bottomRightBx;
tempPointBy = p2y + halfWidthB * slopePrime;
result[2] = tempPointBx;
result[3] = tempPointBy;
break;
case 3:
tempPointBy = bottomLeftBy;
tempPointBx = p2x + halfHeightB / slopePrime;
result[2] = tempPointBx;
result[3] = tempPointBy;
break;
case 4:
tempPointBx = bottomLeftBx;
tempPointBy = p2y + (-halfWidthB) * slopePrime;
result[2] = tempPointBx;
result[3] = tempPointBy;
break;
}
}
}
return false;
};
/**
* This method returns in which cardinal direction does input point stays
* 1: North
* 2: East
* 3: South
* 4: West
*/
IGeometry.getCardinalDirection = function (slope, slopePrime, line)
{
if (slope > slopePrime)
{
return line;
}
else
{
return 1 + line % 4;
}
};
/**
* This method calculates the intersection of the two lines defined by
* point pairs (s1,s2) and (f1,f2).
*/
IGeometry.getIntersection = function(s1, s2, f1, f2)
{
if (f2 == null) {
return this.getIntersection2(s1, s2, f1);
}
let x1 = s1.x;
let y1 = s1.y;
let x2 = s2.x;
let y2 = s2.y;
let x3 = f1.x;
let y3 = f1.y;
let x4 = f2.x;
let y4 = f2.y;
let x, y; // intersection point
let a1, a2, b1, b2, c1, c2; // coefficients of line eqns.
let denom;
a1 = y2 - y1;
b1 = x1 - x2;
c1 = x2 * y1 - x1 * y2; // { a1*x + b1*y + c1 = 0 is line 1 }
a2 = y4 - y3;
b2 = x3 - x4;
c2 = x4 * y3 - x3 * y4; // { a2*x + b2*y + c2 = 0 is line 2 }
denom = a1 * b2 - a2 * b1;
if (denom === 0)
{
return null;
}
x = (b1 * c2 - b2 * c1) / denom;
y = (a2 * c1 - a1 * c2) / denom;
return new Point(x, y);
};
/**
* This method finds and returns the angle of the vector from the + x-axis
* in clockwise direction (compatible w/ Java coordinate system!).
*/
IGeometry.angleOfVector = function(Cx, Cy, Nx, Ny)
{
let C_angle;
if (Cx !== Nx)
{
C_angle = Math.atan((Ny - Cy) / (Nx - Cx));
if (Nx < Cx)
{
C_angle += Math.PI;
}
else if (Ny < Cy)
{
C_angle += this.TWO_PI;
}
}
else if (Ny < Cy)
{
C_angle = this.ONE_AND_HALF_PI; // 270 degrees
}
else
{
C_angle = this.HALF_PI; // 90 degrees
}
return C_angle;
};
/**
* This method checks whether the given two line segments (one with point
* p1 and p2, the other with point p3 and p4) intersect at a point other
* than these points.
*/
IGeometry.doIntersect = function(p1, p2, p3, p4){
let a = p1.x;
let b = p1.y;
let c = p2.x;
let d = p2.y;
let p = p3.x;
let q = p3.y;
let r = p4.x;
let s = p4.y;
let det = (c - a) * (s - q) - (r - p) * (d - b);
if (det === 0) {
return false;
} else {
let lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
let gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
}
};
// -----------------------------------------------------------------------------
// Section: Class Constants
// -----------------------------------------------------------------------------
/**
* Some useful pre-calculated constants
*/
IGeometry.HALF_PI = 0.5 * Math.PI;
IGeometry.ONE_AND_HALF_PI = 1.5 * Math.PI;
IGeometry.TWO_PI = 2.0 * Math.PI;
IGeometry.THREE_PI = 3.0 * Math.PI;
module.exports = IGeometry;
|