Skip to content

Commit

Permalink
refactor(geom): update size handling in various ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 23, 2022
1 parent ddf0a6e commit ab4b93d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
10 changes: 5 additions & 5 deletions packages/geom/src/api/aabb.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isNumber } from "@thi.ng/checks/is-number";
import type { AABBLike, Attribs } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { add3 } from "@thi.ng/vectors/add";
import { addN3 } from "@thi.ng/vectors/addn";
import { set } from "@thi.ng/vectors/set";
import { set3 } from "@thi.ng/vectors/set";
import { subN3 } from "@thi.ng/vectors/subn";
import { __asVec } from "../internal/args.js";
import { __copyAttribs } from "../internal/copy.js";

export class AABB implements AABBLike {
Expand All @@ -15,7 +15,7 @@ export class AABB implements AABBLike {
size: number | Vec = 1,
public attribs?: Attribs
) {
this.size = isNumber(size) ? [size, size, size] : size;
this.size = __asVec(size, 3);
}

get type() {
Expand All @@ -24,8 +24,8 @@ export class AABB implements AABBLike {

copy(): AABB {
return new AABB(
set([], this.pos),
set([], this.size),
set3([], this.pos),
set3([], this.size),
__copyAttribs(this)
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/geom/src/api/ellipse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNumber } from "@thi.ng/checks/is-number";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { set } from "@thi.ng/vectors/set";
import { __asVec } from "../internal/args.js";
import { __copyAttribs } from "../internal/copy.js";

export class Ellipse implements IHiccupShape {
Expand All @@ -12,7 +12,7 @@ export class Ellipse implements IHiccupShape {
r: number | Vec = [1, 1],
public attribs?: Attribs
) {
this.r = isNumber(r) ? [r, r] : r;
this.r = __asVec(r);
}

get type() {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom/src/api/plane.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { set } from "@thi.ng/vectors/set";
import { set3 } from "@thi.ng/vectors/set";
import { __copyAttribs } from "../internal/copy.js";

export class Plane implements IHiccupShape {
Expand All @@ -15,7 +15,7 @@ export class Plane implements IHiccupShape {
}

copy(): Plane {
return new Plane(set([], this.normal), this.w, __copyAttribs(this));
return new Plane(set3([], this.normal), this.w, __copyAttribs(this));
}

toHiccup() {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom/src/api/rect.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isNumber } from "@thi.ng/checks/is-number";
import type { AABBLike, Attribs, IHiccupShape } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { add2 } from "@thi.ng/vectors/add";
import { addN2 } from "@thi.ng/vectors/addn";
import { set2 } from "@thi.ng/vectors/set";
import { subN2 } from "@thi.ng/vectors/subn";
import { __asVec } from "../internal/args.js";
import { __copyAttribs } from "../internal/copy.js";

export class Rect implements AABBLike, IHiccupShape {
Expand All @@ -15,7 +15,7 @@ export class Rect implements AABBLike, IHiccupShape {
size: number | Vec = 1,
public attribs?: Attribs
) {
this.size = isNumber(size) ? [size, size] : size;
this.size = __asVec(size);
}

get type() {
Expand Down
5 changes: 5 additions & 0 deletions packages/geom/src/internal/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isNumber } from "@thi.ng/checks/is-number";
import { isPlainObject } from "@thi.ng/checks/is-plain-object";
import type { Vec } from "@thi.ng/vectors";
import { vecOf } from "@thi.ng/vectors/vec-of";

/**
* Takes an array of arguments, checks if last element is a plain object or
Expand Down Expand Up @@ -56,3 +58,6 @@ export const __argsVN = (args: any[]) => {
: [args[0], undefined, attr]
: [undefined, undefined, attr];
};

export const __asVec = (x: number | Vec, size = 2) =>
isNumber(x) ? vecOf(size, x) : x;
14 changes: 10 additions & 4 deletions packages/geom/src/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { subN2 } from "@thi.ng/vectors/subn";
import type { Circle } from "./api/circle.js";
import type { Polygon } from "./api/polygon.js";
import { Rect } from "./api/rect.js";
import { __argsVV } from "./internal/args.js";
import { __argsVV, __asVec } from "./internal/args.js";

export function rect(pos: Vec, size: number | Vec, attribs?: Attribs): Rect;
export function rect(size: number | Vec, attribs?: Attribs): Rect;
Expand All @@ -31,12 +31,18 @@ export const rectFromMinMaxWithMargin = (
attribs?: Attribs
) => rectFromMinMax(min, max, attribs).offset(margin);

export const rectFromCentroid = (centroid: Vec, size: Vec, attribs?: Attribs) =>
new Rect(maddN2([], size, -0.5, centroid), size, attribs);
export const rectFromCentroid = (
centroid: Vec,
size: number | Vec,
attribs?: Attribs
) => {
size = __asVec(size);
return new Rect(maddN2([], size, -0.5, centroid), size, attribs);
};

export const rectFromCentroidWithMargin = (
centroid: Vec,
size: Vec,
size: number | Vec,
margin: number,
attribs?: Attribs
) => rectFromCentroid(centroid, size, attribs).offset(margin);
Expand Down

0 comments on commit ab4b93d

Please sign in to comment.