@js-draw/math
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -15,3 +15,3 @@ import Vec3 from './Vec3'; | ||
*/ | ||
export default class Color4 { | ||
export declare class Color4 { | ||
/** Red component. Should be in the range [0, 1]. */ | ||
@@ -124,2 +124,2 @@ readonly r: number; | ||
} | ||
export { Color4 }; | ||
export default Color4; |
@@ -56,2 +56,9 @@ import LineSegment2 from './LineSegment2'; | ||
get geometry(): GeometryArrayType; | ||
/** | ||
* Iterates through the start/end points of each component in this path. | ||
* | ||
* If a start point is equivalent to the end point of the previous segment, | ||
* the point is **not** emitted twice. | ||
*/ | ||
startEndPoints(): Generator<import("../Vec3").Vec3, undefined, unknown>; | ||
private cachedPolylineApproximation; | ||
@@ -58,0 +65,0 @@ polylineApproximation(): LineSegment2[]; |
@@ -54,2 +54,3 @@ "use strict"; | ||
for (const part of this.parts) { | ||
let exhaustivenessCheck; | ||
switch (part.kind) { | ||
@@ -72,2 +73,5 @@ case PathCommandType.CubicBezierTo: | ||
break; | ||
default: | ||
exhaustivenessCheck = part; | ||
return exhaustivenessCheck; | ||
} | ||
@@ -78,2 +82,31 @@ } | ||
} | ||
/** | ||
* Iterates through the start/end points of each component in this path. | ||
* | ||
* If a start point is equivalent to the end point of the previous segment, | ||
* the point is **not** emitted twice. | ||
*/ | ||
*startEndPoints() { | ||
yield this.startPoint; | ||
for (const part of this.parts) { | ||
let exhaustivenessCheck; | ||
switch (part.kind) { | ||
case PathCommandType.CubicBezierTo: | ||
yield part.endPoint; | ||
break; | ||
case PathCommandType.QuadraticBezierTo: | ||
yield part.endPoint; | ||
break; | ||
case PathCommandType.LineTo: | ||
yield part.point; | ||
break; | ||
case PathCommandType.MoveTo: | ||
yield part.point; | ||
break; | ||
default: | ||
exhaustivenessCheck = part; | ||
return exhaustivenessCheck; | ||
} | ||
} | ||
} | ||
// Approximates this path with a group of line segments. | ||
@@ -80,0 +113,0 @@ polylineApproximation() { |
@@ -22,3 +22,2 @@ import LineSegment2 from './LineSegment2'; | ||
readonly size: Vec2; | ||
readonly bottomRight: Point2; | ||
readonly area: number; | ||
@@ -39,2 +38,3 @@ constructor(x: number, y: number, w: number, h: number); | ||
get maxDimension(): number; | ||
get bottomRight(): Vec3; | ||
get topRight(): Vec3; | ||
@@ -41,0 +41,0 @@ get bottomLeft(): Vec3; |
@@ -29,3 +29,2 @@ "use strict"; | ||
this.size = Vec2_1.Vec2.of(this.w, this.h); | ||
this.bottomRight = this.topLeft.plus(this.size); | ||
this.area = this.w * this.h; | ||
@@ -46,4 +45,4 @@ } | ||
return this.x <= other.x && this.y <= other.y | ||
&& this.bottomRight.x >= other.bottomRight.x | ||
&& this.bottomRight.y >= other.bottomRight.y; | ||
&& this.x + this.w >= other.x + other.w | ||
&& this.y + this.h >= other.y + other.h; | ||
} | ||
@@ -121,2 +120,8 @@ intersects(other) { | ||
} | ||
// Prevent width/height from being negative | ||
if (margin < 0) { | ||
const xMargin = -Math.min(-margin, this.w / 2); | ||
const yMargin = -Math.min(-margin, this.h / 2); | ||
return new Rect2(this.x - xMargin, this.y - yMargin, this.w + xMargin * 2, this.h + yMargin * 2); | ||
} | ||
return new Rect2(this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2); | ||
@@ -150,2 +155,5 @@ } | ||
} | ||
get bottomRight() { | ||
return this.topLeft.plus(this.size); | ||
} | ||
get topRight() { | ||
@@ -241,12 +249,12 @@ return this.bottomRight.plus(Vec2_1.Vec2.of(0, -this.h)); | ||
const firstRect = rects[0]; | ||
let minX = firstRect.topLeft.x; | ||
let minY = firstRect.topLeft.y; | ||
let maxX = firstRect.bottomRight.x; | ||
let maxY = firstRect.bottomRight.y; | ||
let minX = firstRect.x; | ||
let minY = firstRect.y; | ||
let maxX = firstRect.x + firstRect.w; | ||
let maxY = firstRect.y + firstRect.h; | ||
for (let i = 1; i < rects.length; i++) { | ||
const rect = rects[i]; | ||
minX = Math.min(minX, rect.topLeft.x); | ||
minY = Math.min(minY, rect.topLeft.y); | ||
maxX = Math.max(maxX, rect.bottomRight.x); | ||
maxY = Math.max(maxY, rect.bottomRight.y); | ||
minX = Math.min(minX, rect.x); | ||
minY = Math.min(minY, rect.y); | ||
maxX = Math.max(maxX, rect.x + rect.w); | ||
maxY = Math.max(maxY, rect.y + rect.h); | ||
} | ||
@@ -253,0 +261,0 @@ return new Rect2(minX, minY, maxX - minX, maxY - minY); |
@@ -15,3 +15,3 @@ import Vec3 from './Vec3'; | ||
*/ | ||
export default class Color4 { | ||
export declare class Color4 { | ||
/** Red component. Should be in the range [0, 1]. */ | ||
@@ -124,2 +124,2 @@ readonly r: number; | ||
} | ||
export { Color4 }; | ||
export default Color4; |
@@ -56,2 +56,9 @@ import LineSegment2 from './LineSegment2'; | ||
get geometry(): GeometryArrayType; | ||
/** | ||
* Iterates through the start/end points of each component in this path. | ||
* | ||
* If a start point is equivalent to the end point of the previous segment, | ||
* the point is **not** emitted twice. | ||
*/ | ||
startEndPoints(): Generator<import("../Vec3").Vec3, undefined, unknown>; | ||
private cachedPolylineApproximation; | ||
@@ -58,0 +65,0 @@ polylineApproximation(): LineSegment2[]; |
@@ -22,3 +22,2 @@ import LineSegment2 from './LineSegment2'; | ||
readonly size: Vec2; | ||
readonly bottomRight: Point2; | ||
readonly area: number; | ||
@@ -39,2 +38,3 @@ constructor(x: number, y: number, w: number, h: number); | ||
get maxDimension(): number; | ||
get bottomRight(): Vec3; | ||
get topRight(): Vec3; | ||
@@ -41,0 +41,0 @@ get bottomLeft(): Vec3; |
{ | ||
"name": "@js-draw/math", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "A math library for js-draw. ", | ||
@@ -48,3 +48,3 @@ "types": "./dist/mjs/lib.d.ts", | ||
], | ||
"gitHead": "ced98ae289f299c44b54515571d0087aa6d32cdc" | ||
"gitHead": "46b3d8f819f8e083f6e3e1d01e027e4311355456" | ||
} |
@@ -16,3 +16,3 @@ import Vec3 from './Vec3'; | ||
*/ | ||
export default class Color4 { | ||
export class Color4 { | ||
private constructor( | ||
@@ -441,2 +441,2 @@ /** Red component. Should be in the range [0, 1]. */ | ||
export { Color4 }; | ||
export default Color4; |
@@ -100,2 +100,4 @@ import { toRoundedString, toStringOfSamePrecision } from '../rounding'; | ||
for (const part of this.parts) { | ||
let exhaustivenessCheck: never; | ||
switch (part.kind) { | ||
@@ -128,2 +130,5 @@ case PathCommandType.CubicBezierTo: | ||
break; | ||
default: | ||
exhaustivenessCheck = part; | ||
return exhaustivenessCheck; | ||
} | ||
@@ -136,2 +141,34 @@ } | ||
/** | ||
* Iterates through the start/end points of each component in this path. | ||
* | ||
* If a start point is equivalent to the end point of the previous segment, | ||
* the point is **not** emitted twice. | ||
*/ | ||
public *startEndPoints() { | ||
yield this.startPoint; | ||
for (const part of this.parts) { | ||
let exhaustivenessCheck: never; | ||
switch (part.kind) { | ||
case PathCommandType.CubicBezierTo: | ||
yield part.endPoint; | ||
break; | ||
case PathCommandType.QuadraticBezierTo: | ||
yield part.endPoint; | ||
break; | ||
case PathCommandType.LineTo: | ||
yield part.point; | ||
break; | ||
case PathCommandType.MoveTo: | ||
yield part.point; | ||
break; | ||
default: | ||
exhaustivenessCheck = part; | ||
return exhaustivenessCheck; | ||
} | ||
} | ||
} | ||
private cachedPolylineApproximation: LineSegment2[]|null = null; | ||
@@ -138,0 +175,0 @@ |
@@ -114,2 +114,23 @@ | ||
it('should correctly compute the intersection of one rectangle and several others', () => { | ||
const mainRect = new Rect2(334,156,333,179); | ||
const shouldIntersect = [ | ||
new Rect2(400.8, 134.8, 8.4, 161.4), | ||
new Rect2(324.8,93,164.4,75.2), | ||
new Rect2(435.8,146.8,213.2,192.6), | ||
new Rect2(550.8,211.8,3.4,3.4), | ||
new Rect2(478.8,93.8,212.4,95.4), | ||
]; | ||
const shouldNotIntersect = [ | ||
new Rect2(200, 200, 1, 1), | ||
]; | ||
for (const rect of shouldIntersect) { | ||
expect(mainRect.intersects(rect)).toBe(true); | ||
} | ||
for (const rect of shouldNotIntersect) { | ||
expect(mainRect.intersects(rect)).toBe(false); | ||
} | ||
}); | ||
it('intersecting rectangles should have their intersections correctly computed', () => { | ||
@@ -134,2 +155,15 @@ expect(new Rect2(-1, -1, 2, 2).intersection(Rect2.empty)).objEq(Rect2.empty); | ||
it('.grownBy should expand a rectangle by the given margin', () => { | ||
expect(Rect2.empty.grownBy(0)).toBe(Rect2.empty); | ||
// Should add padding to all sides. | ||
expect(new Rect2(1, 2, 3, 4).grownBy(1)).objEq(new Rect2(0, 1, 5, 6)); | ||
// Shrinking should not result in negative widths/heights and | ||
// should adjust x/y appropriately | ||
expect(new Rect2(1, 2, 1, 2).grownBy(-1)).objEq(new Rect2(1.5, 3, 0, 0)); | ||
expect(new Rect2(1, 2, 4, 4).grownBy(-1)).objEq(new Rect2(2, 3, 2, 2)); | ||
expect(new Rect2(1, 2, 2, 8).grownBy(-2)).objEq(new Rect2(2, 4, 0, 4)); | ||
}); | ||
describe('should correctly expand to include a given point', () => { | ||
@@ -136,0 +170,0 @@ it('Growing an empty rectange to include (1, 0)', () => { |
@@ -24,3 +24,2 @@ import LineSegment2 from './LineSegment2'; | ||
public readonly size: Vec2; | ||
public readonly bottomRight: Point2; | ||
public readonly area: number; | ||
@@ -49,3 +48,2 @@ | ||
this.size = Vec2.of(this.w, this.h); | ||
this.bottomRight = this.topLeft.plus(this.size); | ||
this.area = this.w * this.h; | ||
@@ -70,4 +68,4 @@ } | ||
return this.x <= other.x && this.y <= other.y | ||
&& this.bottomRight.x >= other.bottomRight.x | ||
&& this.bottomRight.y >= other.bottomRight.y; | ||
&& this.x + this.w >= other.x + other.w | ||
&& this.y + this.h >= other.y + other.h; | ||
} | ||
@@ -165,2 +163,13 @@ | ||
// Prevent width/height from being negative | ||
if (margin < 0) { | ||
const xMargin = -Math.min(-margin, this.w / 2); | ||
const yMargin = -Math.min(-margin, this.h / 2); | ||
return new Rect2( | ||
this.x - xMargin, this.y - yMargin, | ||
this.w + xMargin * 2, this.h + yMargin * 2, | ||
); | ||
} | ||
return new Rect2( | ||
@@ -201,2 +210,6 @@ this.x - margin, this.y - margin, this.w + margin * 2, this.h + margin * 2 | ||
public get bottomRight() { | ||
return this.topLeft.plus(this.size); | ||
} | ||
public get topRight() { | ||
@@ -323,13 +336,13 @@ return this.bottomRight.plus(Vec2.of(0, -this.h)); | ||
const firstRect = rects[0]; | ||
let minX: number = firstRect.topLeft.x; | ||
let minY: number = firstRect.topLeft.y; | ||
let maxX: number = firstRect.bottomRight.x; | ||
let maxY: number = firstRect.bottomRight.y; | ||
let minX: number = firstRect.x; | ||
let minY: number = firstRect.y; | ||
let maxX: number = firstRect.x + firstRect.w; | ||
let maxY: number = firstRect.y + firstRect.h; | ||
for (let i = 1; i < rects.length; i++) { | ||
const rect = rects[i]; | ||
minX = Math.min(minX, rect.topLeft.x); | ||
minY = Math.min(minY, rect.topLeft.y); | ||
maxX = Math.max(maxX, rect.bottomRight.x); | ||
maxY = Math.max(maxY, rect.bottomRight.y); | ||
minX = Math.min(minX, rect.x); | ||
minY = Math.min(minY, rect.y); | ||
maxX = Math.max(maxX, rect.x + rect.w); | ||
maxY = Math.max(maxY, rect.y + rect.h); | ||
} | ||
@@ -336,0 +349,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
436577
11938