MCPcopy
hub / github.com/clientIO/joint / getRectPoints

Function getRectPoints

packages/joint-core/src/routers/manhattan.mjs:494–556  ·  view source on GitHub ↗
(anchor, bbox, directionList, grid, opt)

Source from the content-addressed store, hash-verified

492// the anchor itself is returned as rect point (representing some directions)
493// (since those directions are unobstructed by the bbox)
494function getRectPoints(anchor, bbox, directionList, grid, opt) {
495
496 var precision = opt.precision;
497 var directionMap = opt.directionMap;
498
499 var anchorCenterVector = anchor.difference(bbox.center());
500
501 var keys = util.isObject(directionMap) ? Object.keys(directionMap) : [];
502 var dirList = util.toArray(directionList);
503 var rectPoints = keys.reduce(function(res, key) {
504
505 if (dirList.includes(key)) {
506 var direction = directionMap[key];
507
508 // create a line that is guaranteed to intersect the bbox if bbox is in the direction
509 // even if anchor lies outside of bbox
510 var endpoint = new g.Point(
511 anchor.x + direction.x * (Math.abs(anchorCenterVector.x) + bbox.width),
512 anchor.y + direction.y * (Math.abs(anchorCenterVector.y) + bbox.height)
513 );
514 var intersectionLine = new g.Line(anchor, endpoint);
515
516 // get the farther intersection, in case there are two
517 // (that happens if anchor lies next to bbox)
518 var intersections = intersectionLine.intersect(bbox) || [];
519 var numIntersections = intersections.length;
520 var farthestIntersectionDistance;
521 var farthestIntersection = null;
522 for (var i = 0; i < numIntersections; i++) {
523 var currentIntersection = intersections[i];
524 var distance = anchor.squaredDistance(currentIntersection);
525 if ((farthestIntersectionDistance === undefined) || (distance > farthestIntersectionDistance)) {
526 farthestIntersectionDistance = distance;
527 farthestIntersection = currentIntersection;
528 }
529 }
530
531 // if an intersection was found in this direction, it is our rectPoint
532 if (farthestIntersection) {
533 var point = align(farthestIntersection, grid, precision);
534
535 // if the rectPoint lies inside the bbox, offset it by one more step
536 if (bbox.containsPoint(point)) {
537 point = align(point.offset(direction.x * grid.x, direction.y * grid.y), grid, precision);
538 }
539
540 // then add the point to the result array
541 // aligned
542 res.push(point);
543 }
544 }
545
546 return res;
547 }, []);
548
549 // if anchor lies outside of bbox, add it to the array of points
550 if (!bbox.containsPoint(anchor)) {
551 // aligned

Callers 1

findRouteFunction · 0.85

Calls 4

alignFunction · 0.85
isObjectMethod · 0.80
keysMethod · 0.80
offsetMethod · 0.45

Tested by

no test coverage detected