Skip to content

Commit

Permalink
fix: Check if the snapped point is close enough to the source point
Browse files Browse the repository at this point in the history
- Some snapping functions didn't do it strictly.
  • Loading branch information
miyanokomiya committed Nov 24, 2024
1 parent 85d6f5d commit 0442e9d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/composables/shapeSnapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ export function newShapeSnapping(option: Option) {
const dy = isHInterval ? intervalResult.h!.d : (yClosest?.[1].d ?? 0);

const diff = { x: dx, y: dy };
if (getNorm(diff) >= snapThreshold) return;

const adjustedP = add(p, diff);
const targets: SnappingResultTarget[] = [];
const intervalTargets: IntervalSnappingResultTarget[] = [];
Expand Down Expand Up @@ -334,6 +336,7 @@ export function newShapeSnapping(option: Option) {
srcP: p,
snappingResult: firstResult,
guideline,
scale,
});
}

Expand Down Expand Up @@ -541,8 +544,9 @@ export function newShapeIntervalSnapping(option: ShapeIntervalSnappingOption) {
x: xClosest?.[1] ?? 0,
y: yClosest?.[1] ?? 0,
};
const [adjustedTop, , , adjustedLeft] = getRectLines(moveRect(rect, diff));
if (getNorm(diff) >= snapThreshold) return;

const [adjustedTop, , , adjustedLeft] = getRectLines(moveRect(rect, diff));
const ret: InvervalSnappingResult = {};

if (xClosest) {
Expand Down Expand Up @@ -826,29 +830,34 @@ function snapPointOnLine({
srcP,
snappingResult,
guideline,
scale,
}: {
srcP: IVec2;
snappingResult: SnappingResult;
guideline: ISegment;
scale: number;
}): SnappingResult | undefined {
const movingP = add(srcP, snappingResult.diff);
const snapThreshold = SNAP_THRESHOLD * scale;
const guidelineVec = sub(guideline[1], guideline[0]);
const candidateInfo = getSecondGuidelineCandidateInfo(snappingResult, guidelineVec);

const closestInfo = pickMinItem(
candidateInfo.candidates.map((seg) => {
const intersection = getCrossLineAndLine(seg, guideline);
if (!intersection) return;
const d2 = getD2(sub(movingP, intersection));
const d2 = getD2(sub(srcP, intersection));
return [seg, intersection, d2] as const;
}),
(info) => info?.[2] ?? Infinity,
);
if (!closestInfo) return;

const [secondGuideline, snappedP] = closestInfo;
const diff = sub(snappedP, srcP);
if (getNorm(diff) >= snapThreshold) return;

return {
diff: sub(snappedP, srcP),
diff,
...optimizeSnappingTargetInfoForPoint(
filterSnappingTargetsBySecondGuideline(candidateInfo, secondGuideline),
snappedP,
Expand Down

0 comments on commit 0442e9d

Please sign in to comment.