Skip to content

Commit

Permalink
Fix problems with Bound (#215)
Browse files Browse the repository at this point in the history
1. `Group.toBound`'s return type was wrongly marked `Group`. Closes #214
2. `Bound` stores top left and bottom right `Pt`s separately. Close #213

For 2, top left and bottom right `Pt`s were separate from `Pt`s that are
indexable using integers.

This causes that, when a calculation method such as `add` inherited from
`Group` is used on a `Bound`, the `Bound`'s corner `Pts`s are sometimes
not updated, as `Group` uses integer indices for getting the member
`Pt`s.

In order to fix this, the separate members `_topLeft` and `_bottomRight`
are removed from `Bound`. `topLeft` and `bottomRight` are now just
getters for integer-indexable `Pt`s.
  • Loading branch information
ii41 committed May 19, 2024
1 parent 47de4b7 commit 5d8e12f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 95 deletions.
4 changes: 1 addition & 3 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,12 @@ declare class Group extends Array<Pt> {
$matrixMultiply(g: GroupLike | number, transposed?: boolean, elementwise?: boolean): Group;
zipSlice(index: number, defaultValue?: number | boolean): Pt;
$zip(defaultValue?: number | boolean, useLongest?: boolean): Group;
toBound(): Group;
toBound(): Bound;
toString(): string;
}
declare class Bound extends Group implements IPt {
protected _center: Pt;
protected _size: Pt;
protected _topLeft: Pt;
protected _bottomRight: Pt;
protected _inited: boolean;
constructor(...args: Pt[]);
static fromBoundingRect(rect: ClientRect): Bound;
Expand Down
4 changes: 1 addition & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,12 @@ declare class Group extends Array<Pt> {
$matrixMultiply(g: GroupLike | number, transposed?: boolean, elementwise?: boolean): Group;
zipSlice(index: number, defaultValue?: number | boolean): Pt;
$zip(defaultValue?: number | boolean, useLongest?: boolean): Group;
toBound(): Group;
toBound(): Bound;
toString(): string;
}
declare class Bound extends Group implements IPt {
protected _center: Pt;
protected _size: Pt;
protected _topLeft: Pt;
protected _bottomRight: Pt;
protected _inited: boolean;
constructor(...args: Pt[]);
static fromBoundingRect(rect: ClientRect): Bound;
Expand Down
34 changes: 13 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4092,8 +4092,6 @@ var Bound = class _Bound extends Group {
super(...args);
this._center = new Pt();
this._size = new Pt();
this._topLeft = new Pt();
this._bottomRight = new Pt();
this._inited = false;
this.init();
}
Expand Down Expand Up @@ -4127,10 +4125,6 @@ var Bound = class _Bound extends Group {
this._inited = true;
}
if (this.p1 && this.p2) {
const a = this.p1;
const b = this.p2;
this.topLeft = a.$min(b);
this._bottomRight = a.$max(b);
this._updateSize();
this._inited = true;
}
Expand All @@ -4139,42 +4133,42 @@ var Bound = class _Bound extends Group {
* Clone this bound and return a new one.
*/
clone() {
return new _Bound(this._topLeft.clone(), this._bottomRight.clone());
return new _Bound(this.topLeft.clone(), this.bottomRight.clone());
}
/**
* Recalculte size and center.
*/
_updateSize() {
this._size = this._bottomRight.$subtract(this._topLeft).abs();
this._size = this.bottomRight.$subtract(this.topLeft).abs();
this._updateCenter();
}
/**
* Recalculate center.
*/
_updateCenter() {
this._center = this._size.$multiply(0.5).add(this._topLeft);
this._center = this._size.$multiply(0.5).add(this.topLeft);
}
/**
* Recalculate based on top-left position and size.
*/
_updatePosFromTop() {
this._bottomRight = this._topLeft.$add(this._size);
this.bottomRight = this.topLeft.$add(this._size);
this._updateCenter();
}
/**
* Recalculate based on bottom-right position and size.
*/
_updatePosFromBottom() {
this._topLeft = this._bottomRight.$subtract(this._size);
this.topLeft = this.bottomRight.$subtract(this._size);
this._updateCenter();
}
/**
* Recalculate based on center position and size.
*/
_updatePosFromCenter() {
const half = this._size.$multiply(0.5);
this._topLeft = this._center.$subtract(half);
this._bottomRight = this._center.$add(half);
this.topLeft = this._center.$subtract(half);
this.bottomRight = this._center.$add(half);
}
/**
* Size of this Bound
Expand All @@ -4200,22 +4194,20 @@ var Bound = class _Bound extends Group {
* Top-left position of this Bound
*/
get topLeft() {
return new Pt(this._topLeft);
return new Pt(this[0]);
}
set topLeft(p) {
this._topLeft = new Pt(p);
this[0] = this._topLeft;
this[0] = new Pt(p);
this._updateSize();
}
/**
* Bottom-right position of this Bound
*/
get bottomRight() {
return new Pt(this._bottomRight);
return new Pt(this[1]);
}
set bottomRight(p) {
this._bottomRight = new Pt(p);
this[1] = this._bottomRight;
this[1] = new Pt(p);
this._updateSize();
}
/**
Expand Down Expand Up @@ -4277,8 +4269,8 @@ var Bound = class _Bound extends Group {
* It's simpler and preferable to change the Bound's properties (eg, topLeft, bottomRight) instead of updating the Bound's Pts.
*/
update() {
this._topLeft = this[0];
this._bottomRight = this[1];
this.topLeft = this[0];
this.bottomRight = this[1];
this._updateSize();
return this;
}
Expand Down
34 changes: 13 additions & 21 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4024,8 +4024,6 @@ var Bound = class _Bound extends Group {
super(...args);
this._center = new Pt();
this._size = new Pt();
this._topLeft = new Pt();
this._bottomRight = new Pt();
this._inited = false;
this.init();
}
Expand Down Expand Up @@ -4059,10 +4057,6 @@ var Bound = class _Bound extends Group {
this._inited = true;
}
if (this.p1 && this.p2) {
const a = this.p1;
const b = this.p2;
this.topLeft = a.$min(b);
this._bottomRight = a.$max(b);
this._updateSize();
this._inited = true;
}
Expand All @@ -4071,42 +4065,42 @@ var Bound = class _Bound extends Group {
* Clone this bound and return a new one.
*/
clone() {
return new _Bound(this._topLeft.clone(), this._bottomRight.clone());
return new _Bound(this.topLeft.clone(), this.bottomRight.clone());
}
/**
* Recalculte size and center.
*/
_updateSize() {
this._size = this._bottomRight.$subtract(this._topLeft).abs();
this._size = this.bottomRight.$subtract(this.topLeft).abs();
this._updateCenter();
}
/**
* Recalculate center.
*/
_updateCenter() {
this._center = this._size.$multiply(0.5).add(this._topLeft);
this._center = this._size.$multiply(0.5).add(this.topLeft);
}
/**
* Recalculate based on top-left position and size.
*/
_updatePosFromTop() {
this._bottomRight = this._topLeft.$add(this._size);
this.bottomRight = this.topLeft.$add(this._size);
this._updateCenter();
}
/**
* Recalculate based on bottom-right position and size.
*/
_updatePosFromBottom() {
this._topLeft = this._bottomRight.$subtract(this._size);
this.topLeft = this.bottomRight.$subtract(this._size);
this._updateCenter();
}
/**
* Recalculate based on center position and size.
*/
_updatePosFromCenter() {
const half = this._size.$multiply(0.5);
this._topLeft = this._center.$subtract(half);
this._bottomRight = this._center.$add(half);
this.topLeft = this._center.$subtract(half);
this.bottomRight = this._center.$add(half);
}
/**
* Size of this Bound
Expand All @@ -4132,22 +4126,20 @@ var Bound = class _Bound extends Group {
* Top-left position of this Bound
*/
get topLeft() {
return new Pt(this._topLeft);
return new Pt(this[0]);
}
set topLeft(p) {
this._topLeft = new Pt(p);
this[0] = this._topLeft;
this[0] = new Pt(p);
this._updateSize();
}
/**
* Bottom-right position of this Bound
*/
get bottomRight() {
return new Pt(this._bottomRight);
return new Pt(this[1]);
}
set bottomRight(p) {
this._bottomRight = new Pt(p);
this[1] = this._bottomRight;
this[1] = new Pt(p);
this._updateSize();
}
/**
Expand Down Expand Up @@ -4209,8 +4201,8 @@ var Bound = class _Bound extends Group {
* It's simpler and preferable to change the Bound's properties (eg, topLeft, bottomRight) instead of updating the Bound's Pts.
*/
update() {
this._topLeft = this[0];
this._bottomRight = this[1];
this.topLeft = this[0];
this.bottomRight = this[1];
this._updateSize();
return this;
}
Expand Down
34 changes: 13 additions & 21 deletions dist/pts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4102,8 +4102,6 @@ See https://github.com/williamngan/pts for details. */
super(...args);
this._center = new Pt();
this._size = new Pt();
this._topLeft = new Pt();
this._bottomRight = new Pt();
this._inited = false;
this.init();
}
Expand Down Expand Up @@ -4137,10 +4135,6 @@ See https://github.com/williamngan/pts for details. */
this._inited = true;
}
if (this.p1 && this.p2) {
const a = this.p1;
const b = this.p2;
this.topLeft = a.$min(b);
this._bottomRight = a.$max(b);
this._updateSize();
this._inited = true;
}
Expand All @@ -4149,42 +4143,42 @@ See https://github.com/williamngan/pts for details. */
* Clone this bound and return a new one.
*/
clone() {
return new _Bound(this._topLeft.clone(), this._bottomRight.clone());
return new _Bound(this.topLeft.clone(), this.bottomRight.clone());
}
/**
* Recalculte size and center.
*/
_updateSize() {
this._size = this._bottomRight.$subtract(this._topLeft).abs();
this._size = this.bottomRight.$subtract(this.topLeft).abs();
this._updateCenter();
}
/**
* Recalculate center.
*/
_updateCenter() {
this._center = this._size.$multiply(0.5).add(this._topLeft);
this._center = this._size.$multiply(0.5).add(this.topLeft);
}
/**
* Recalculate based on top-left position and size.
*/
_updatePosFromTop() {
this._bottomRight = this._topLeft.$add(this._size);
this.bottomRight = this.topLeft.$add(this._size);
this._updateCenter();
}
/**
* Recalculate based on bottom-right position and size.
*/
_updatePosFromBottom() {
this._topLeft = this._bottomRight.$subtract(this._size);
this.topLeft = this.bottomRight.$subtract(this._size);
this._updateCenter();
}
/**
* Recalculate based on center position and size.
*/
_updatePosFromCenter() {
const half = this._size.$multiply(0.5);
this._topLeft = this._center.$subtract(half);
this._bottomRight = this._center.$add(half);
this.topLeft = this._center.$subtract(half);
this.bottomRight = this._center.$add(half);
}
/**
* Size of this Bound
Expand All @@ -4210,22 +4204,20 @@ See https://github.com/williamngan/pts for details. */
* Top-left position of this Bound
*/
get topLeft() {
return new Pt(this._topLeft);
return new Pt(this[0]);
}
set topLeft(p) {
this._topLeft = new Pt(p);
this[0] = this._topLeft;
this[0] = new Pt(p);
this._updateSize();
}
/**
* Bottom-right position of this Bound
*/
get bottomRight() {
return new Pt(this._bottomRight);
return new Pt(this[1]);
}
set bottomRight(p) {
this._bottomRight = new Pt(p);
this[1] = this._bottomRight;
this[1] = new Pt(p);
this._updateSize();
}
/**
Expand Down Expand Up @@ -4287,8 +4279,8 @@ See https://github.com/williamngan/pts for details. */
* It's simpler and preferable to change the Bound's properties (eg, topLeft, bottomRight) instead of updating the Bound's Pts.
*/
update() {
this._topLeft = this[0];
this._bottomRight = this[1];
this.topLeft = this[0];
this.bottomRight = this[1];
this._updateSize();
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions dist/pts.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pts.min.js.map

Large diffs are not rendered by default.

0 comments on commit 5d8e12f

Please sign in to comment.