Skip to content

Commit

Permalink
feat(geom): add scaleWithCenter()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 8, 2024
1 parent afe8377 commit e328494
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export * from "./point-inside.js";
export * from "./resample.js";
export * from "./rotate.js";
export * from "./scale.js";
export * from "./scale-with-center.js";
export * from "./scatter.js";
export * from "./simplify.js";
export * from "./split-arclength.js";
Expand Down
27 changes: 27 additions & 0 deletions packages/geom/src/scale-with-center.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { IShape } from "@thi.ng/geom-api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { mulN } from "@thi.ng/vectors/muln";
import { scale } from "./scale.js";
import { translate } from "./translate.js";

/**
* Applies a sequence of translate and scale operations to return an uniformly
* scaled version of the given shape using `center` as the point of reference.
*
* @remarks
* This op is likely slower than using {@link transform} with a equivalent
* transformation matrix (e.g. using [`scaleWithCenter()` from
* thi.ng/matrices](https://docs.thi.ng/umbrella/matrices/functions/scaleWithCenter23.html)),
* but will not change the shape type (as might be the case with `transform()`).
*
* Also see: {@link scale}, {@link translate}, {@link applyTransforms}.
*
* @param shape
* @param center
* @param factor
*/
export const scaleWithCenter = (
shape: IShape,
center: ReadonlyVec,
factor: number
) => translate(scale(translate(shape, mulN([], center, -1)), factor), center);

0 comments on commit e328494

Please sign in to comment.