@js-draw/math 1.24.2 → 1.26.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-2024 Henry Heino
3
+ Copyright (c) 2023-2025 Henry Heino
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -52,6 +52,13 @@ export declare class Rect2 extends Abstract2DShape {
52
52
  divideIntoGrid(columns: number, rows: number): Rect2[];
53
53
  grownToPoint(point: Point2, margin?: number): Rect2;
54
54
  grownBy(margin: number): Rect2;
55
+ /**
56
+ * If this rectangle is smaller than `minSize`, returns a copy of this
57
+ * with a larger width/height.
58
+ *
59
+ * If smaller than `minSize`, padding is applied on both sides.
60
+ */
61
+ grownToSize(minSize: Vec2): Rect2;
55
62
  getClosestPointOnBoundaryTo(target: Point2): Vec3;
56
63
  /**
57
64
  * Returns `true` iff all points in this rectangle are within `distance` from `point`:
@@ -155,6 +155,20 @@ class Rect2 extends Abstract2DShape_1.default {
155
155
  }
156
156
  return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2);
157
157
  }
158
+ /**
159
+ * If this rectangle is smaller than `minSize`, returns a copy of this
160
+ * with a larger width/height.
161
+ *
162
+ * If smaller than `minSize`, padding is applied on both sides.
163
+ */
164
+ grownToSize(minSize) {
165
+ if (this.width >= minSize.x && this.height >= minSize.y) {
166
+ return this;
167
+ }
168
+ const deltaWidth = Math.max(0, minSize.x - this.width);
169
+ const deltaHeight = Math.max(0, minSize.y - this.height);
170
+ return new Rect2(this.x - deltaWidth / 2, this.y - deltaHeight / 2, this.width + deltaWidth, this.height + deltaHeight);
171
+ }
158
172
  getClosestPointOnBoundaryTo(target) {
159
173
  const closestEdgePoints = this.getEdges().map((edge) => {
160
174
  return edge.closestPointTo(target);
@@ -52,6 +52,13 @@ export declare class Rect2 extends Abstract2DShape {
52
52
  divideIntoGrid(columns: number, rows: number): Rect2[];
53
53
  grownToPoint(point: Point2, margin?: number): Rect2;
54
54
  grownBy(margin: number): Rect2;
55
+ /**
56
+ * If this rectangle is smaller than `minSize`, returns a copy of this
57
+ * with a larger width/height.
58
+ *
59
+ * If smaller than `minSize`, padding is applied on both sides.
60
+ */
61
+ grownToSize(minSize: Vec2): Rect2;
55
62
  getClosestPointOnBoundaryTo(target: Point2): Vec3;
56
63
  /**
57
64
  * Returns `true` iff all points in this rectangle are within `distance` from `point`:
@@ -149,6 +149,20 @@ export class Rect2 extends Abstract2DShape {
149
149
  }
150
150
  return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2);
151
151
  }
152
+ /**
153
+ * If this rectangle is smaller than `minSize`, returns a copy of this
154
+ * with a larger width/height.
155
+ *
156
+ * If smaller than `minSize`, padding is applied on both sides.
157
+ */
158
+ grownToSize(minSize) {
159
+ if (this.width >= minSize.x && this.height >= minSize.y) {
160
+ return this;
161
+ }
162
+ const deltaWidth = Math.max(0, minSize.x - this.width);
163
+ const deltaHeight = Math.max(0, minSize.y - this.height);
164
+ return new Rect2(this.x - deltaWidth / 2, this.y - deltaHeight / 2, this.width + deltaWidth, this.height + deltaHeight);
165
+ }
152
166
  getClosestPointOnBoundaryTo(target) {
153
167
  const closestEdgePoints = this.getEdges().map((edge) => {
154
168
  return edge.closestPointTo(target);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-draw/math",
3
- "version": "1.24.2",
3
+ "version": "1.26.0",
4
4
  "description": "A math library for js-draw. ",
5
5
  "types": "./dist/mjs/lib.d.ts",
6
6
  "main": "./dist/cjs/lib.js",
@@ -27,7 +27,7 @@
27
27
  "bezier-js": "6.1.3"
28
28
  },
29
29
  "devDependencies": {
30
- "@js-draw/build-tool": "^1.24.2",
30
+ "@js-draw/build-tool": "^1.26.0",
31
31
  "@types/bezier-js": "4.1.0",
32
32
  "@types/jest": "29.5.5",
33
33
  "@types/jsdom": "21.1.3"
@@ -44,5 +44,5 @@
44
44
  "svg",
45
45
  "math"
46
46
  ],
47
- "gitHead": "32c8db56fc8996c8d485118d1ee37077428344a3"
47
+ "gitHead": "6529cad584ca93a992f2576a43f25af48da3d707"
48
48
  }
@@ -138,6 +138,14 @@ describe('Rect2', () => {
138
138
  expect(new Rect2(1, 2, 2, 8).grownBy(-2)).objEq(new Rect2(2, 4, 0, 4));
139
139
  });
140
140
 
141
+ it('.grownToSize should grow the rectangle to the given minimum size', () => {
142
+ expect(Rect2.empty.grownToSize(Vec2.of(10, 10))).objEq(new Rect2(-5, -5, 10, 10));
143
+ expect(Rect2.empty.grownToSize(Vec2.of(10, 4))).objEq(new Rect2(-5, -2, 10, 4));
144
+
145
+ expect(Rect2.unitSquare.grownToSize(Vec2.of(0.5, 0.5))).toBe(Rect2.unitSquare);
146
+ expect(new Rect2(0, 0, 2, 2).grownToSize(Vec2.of(4, 0.5))).objEq(new Rect2(-1, 0, 4, 2));
147
+ });
148
+
141
149
  describe('should correctly expand to include a given point', () => {
142
150
  it('Growing an empty rectange to include (1, 0)', () => {
143
151
  const originalRect = Rect2.empty;
@@ -200,6 +200,28 @@ export class Rect2 extends Abstract2DShape {
200
200
  return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2);
201
201
  }
202
202
 
203
+ /**
204
+ * If this rectangle is smaller than `minSize`, returns a copy of this
205
+ * with a larger width/height.
206
+ *
207
+ * If smaller than `minSize`, padding is applied on both sides.
208
+ */
209
+ public grownToSize(minSize: Vec2) {
210
+ if (this.width >= minSize.x && this.height >= minSize.y) {
211
+ return this;
212
+ }
213
+
214
+ const deltaWidth = Math.max(0, minSize.x - this.width);
215
+ const deltaHeight = Math.max(0, minSize.y - this.height);
216
+
217
+ return new Rect2(
218
+ this.x - deltaWidth / 2,
219
+ this.y - deltaHeight / 2,
220
+ this.width + deltaWidth,
221
+ this.height + deltaHeight,
222
+ );
223
+ }
224
+
203
225
  public getClosestPointOnBoundaryTo(target: Point2) {
204
226
  const closestEdgePoints = this.getEdges().map((edge) => {
205
227
  return edge.closestPointTo(target);