Skip to content

Commit

Permalink
feat: optimize snap method
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Jul 3, 2024
1 parent 2e92805 commit 2862a3b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/geo/src/geo/geo_line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const closestPtOnLine = (
/**
* get closest point of polar coords. 0, 45, 90, 135, 150
*/
export const closestPolarPt = (center: IPoint, p: IPoint, count = 4) => {
export const getPolarTrackSnapPt = (center: IPoint, p: IPoint, count = 4) => {
let closestPt: IPoint = { x: 0, y: 0 };
let closestDist = Infinity;
for (let i = 1; i <= count; i++) {
Expand All @@ -46,6 +46,9 @@ export const closestPolarPt = (center: IPoint, p: IPoint, count = 4) => {
};
const { point } = closestPtOnLine(center, pt, p);
const dist = distance(point, p);
if (dist === 0) {
return point;
}
if (dist < closestDist) {
closestDist = dist;
closestPt = point;
Expand Down
4 changes: 2 additions & 2 deletions packages/geo/src/geo/geo_resize_line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type IPoint, type ITransformRect } from '../type';
import { getSweepAngle } from './geo_angle';
import { closestPolarPt } from './geo_line';
import { getPolarTrackSnapPt } from './geo_line';
import { Matrix } from './geo_matrix_class';
import { distance } from './geo_point';

Expand Down Expand Up @@ -49,7 +49,7 @@ export const resizeLine = (
}

if (options.keepPolarSnap) {
newPos = closestPolarPt(globalOrigin, newPos);
newPos = getPolarTrackSnapPt(globalOrigin, newPos);
}

let width = distance(newPos, globalOrigin);
Expand Down

0 comments on commit 2862a3b

Please sign in to comment.