Skip to content

Commit c7d2259

Browse files
authored
Merge pull request #2089 from SreejaReghu/bidimensional_tool
Bidimensional ruler tool bug fix
2 parents 4a6c5f6 + 043852d commit c7d2259

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/tools/bidimensional.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {custom} from '../app/custom.js';
1414
/* eslint-disable no-unused-vars */
1515
import {Style} from '../gui/style.js';
1616
import {Annotation} from '../image/annotation.js';
17-
import {ViewController} from '../app/viewController.js';
18-
/* eslint-enable no-unused-vars */
1917

2018
/**
2119
* Bidimensional (long/short axis) annotation factory.
@@ -736,24 +734,44 @@ export class BidimensionalFactory {
736734
anchor1.x() - kline.x(),
737735
anchor1.y() - kline.y()
738736
);
739-
const newLine = new BidimensionalLine(pointBegin, pointEnd);
740737

741-
// Preserve all custom independent properties
738+
const newLine = new BidimensionalLine(pointBegin, pointEnd);
742739
const oldLine = annotation.mathShape;
743-
newLine.shortAxisLength = oldLine.shortAxisLength;
744-
newLine.shortAxisT = oldLine.shortAxisT;
740+
741+
if (oldLine.shortAxisCenter instanceof Point2D) {
742+
const center = oldLine.shortAxisCenter;
743+
744+
// 2. Project the existing center onto the NEW long axis to find the new T
745+
const dx = pointEnd.getX() - pointBegin.getX();
746+
const dy = pointEnd.getY() - pointBegin.getY();
747+
const lenSq = dx * dx + dy * dy;
748+
749+
if (lenSq > 0) {
750+
const vx = center.getX() - pointBegin.getX();
751+
const vy = center.getY() - pointBegin.getY();
752+
// Vector projection formula: (V dot U) / |U|^2
753+
const t = (vx * dx + vy * dy) / lenSq;
754+
755+
// Clamp t between 0 and 1 so short axis doesn't slide off the line
756+
newLine.shortAxisT = Math.max(0, Math.min(1, t));
757+
758+
// Re-sync the center to the clamped T on the new line
759+
newLine.shortAxisCenter = new Point2D(
760+
pointBegin.getX() + dx * newLine.shortAxisT,
761+
pointBegin.getY() + dy * newLine.shortAxisT
762+
);
763+
} else {
764+
newLine.shortAxisT = 0.5;
765+
newLine.shortAxisCenter = center;
766+
}
767+
} else {
768+
newLine.shortAxisT = oldLine.shortAxisT ?? 0.5;
769+
}
770+
771+
// Preserve the perpendicular lengths
745772
newLine.shortAxisL1 = oldLine.shortAxisL1;
746773
newLine.shortAxisL2 = oldLine.shortAxisL2;
747-
if (
748-
oldLine.shortAxisCenter instanceof Object &&
749-
'getX' in oldLine.shortAxisCenter &&
750-
'getY' in oldLine.shortAxisCenter
751-
) {
752-
newLine.shortAxisCenter = new Point2D(
753-
oldLine.shortAxisCenter.getX(),
754-
oldLine.shortAxisCenter.getY()
755-
);
756-
}
774+
newLine.shortAxisLength = oldLine.shortAxisLength;
757775
newLine.hasShortAxisInteraction = oldLine.hasShortAxisInteraction;
758776

759777
annotation.mathShape = newLine;

0 commit comments

Comments
 (0)