@js-draw/math
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -23,2 +23,3 @@ /** | ||
export { QuadraticBezier } from './shapes/QuadraticBezier'; | ||
export { Abstract2DShape } from './shapes/Abstract2DShape'; | ||
export { Mat33, Mat33Array } from './Mat33'; | ||
@@ -25,0 +26,0 @@ export { Point2, Vec2 } from './Vec2'; |
@@ -21,3 +21,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toRoundedString = exports.Color4 = exports.Vec3 = exports.Vec2 = exports.Mat33 = exports.QuadraticBezier = exports.Rect2 = exports.PathCommandType = exports.Path = exports.LineSegment2 = void 0; | ||
exports.toRoundedString = exports.Color4 = exports.Vec3 = exports.Vec2 = exports.Mat33 = exports.Abstract2DShape = exports.QuadraticBezier = exports.Rect2 = exports.PathCommandType = exports.Path = exports.LineSegment2 = void 0; | ||
var LineSegment2_1 = require("./shapes/LineSegment2"); | ||
@@ -32,2 +32,4 @@ Object.defineProperty(exports, "LineSegment2", { enumerable: true, get: function () { return LineSegment2_1.LineSegment2; } }); | ||
Object.defineProperty(exports, "QuadraticBezier", { enumerable: true, get: function () { return QuadraticBezier_1.QuadraticBezier; } }); | ||
var Abstract2DShape_1 = require("./shapes/Abstract2DShape"); | ||
Object.defineProperty(exports, "Abstract2DShape", { enumerable: true, get: function () { return Abstract2DShape_1.Abstract2DShape; } }); | ||
var Mat33_1 = require("./Mat33"); | ||
@@ -34,0 +36,0 @@ Object.defineProperty(exports, "Mat33", { enumerable: true, get: function () { return Mat33_1.Mat33; } }); |
import LineSegment2 from './LineSegment2'; | ||
import { Point2 } from '../Vec2'; | ||
import Rect2 from './Rect2'; | ||
declare abstract class Abstract2DShape { | ||
/** | ||
* An abstract base class for 2D shapes. | ||
*/ | ||
export declare abstract class Abstract2DShape { | ||
protected static readonly smallValue = 1e-12; | ||
@@ -6,0 +9,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Abstract2DShape = void 0; | ||
/** | ||
* An abstract base class for 2D shapes. | ||
*/ | ||
class Abstract2DShape { | ||
@@ -37,3 +41,5 @@ /** | ||
} | ||
exports.Abstract2DShape = Abstract2DShape; | ||
// @internal | ||
Abstract2DShape.smallValue = 1e-12; | ||
exports.default = Abstract2DShape; |
@@ -38,4 +38,2 @@ import LineSegment2 from './LineSegment2'; | ||
} | ||
type GeometryType = Abstract2DShape; | ||
type GeometryArrayType = Array<GeometryType>; | ||
/** | ||
@@ -46,3 +44,2 @@ * Represents a union of lines and curves. | ||
readonly startPoint: Point2; | ||
readonly parts: PathCommand[]; | ||
/** | ||
@@ -54,6 +51,14 @@ * A rough estimate of the bounding box of the path. | ||
readonly bbox: Rect2; | ||
constructor(startPoint: Point2, parts: PathCommand[]); | ||
/** The individual shapes that make up this path. */ | ||
readonly parts: Readonly<PathCommand>[]; | ||
/** | ||
* Creates a new `Path` that starts at `startPoint` and is made up of the path commands, | ||
* `parts`. | ||
* | ||
* See also {@link fromString} | ||
*/ | ||
constructor(startPoint: Point2, parts: Readonly<PathCommand>[]); | ||
getExactBBox(): Rect2; | ||
private cachedGeometry; | ||
get geometry(): GeometryArrayType; | ||
get geometry(): Abstract2DShape[]; | ||
/** | ||
@@ -83,2 +88,4 @@ * Iterates through the start/end points of each component in this path. | ||
* If `strokeRadius > 0`, the resultant `parameterValue` has no defined value. | ||
* | ||
* **Note**: `strokeRadius` is half of a stroke's width. | ||
*/ | ||
@@ -91,2 +98,12 @@ intersection(line: LineSegment2, strokeRadius?: number): IntersectionResult[]; | ||
private getEndPoint; | ||
/** | ||
* Like {@link closedRoughlyIntersects} except takes stroke width into account. | ||
* | ||
* This is intended to be a very fast and rough approximation. Use {@link intersection} | ||
* and {@link signedDistance} for more accurate (but much slower) intersection calculations. | ||
* | ||
* **Note**: Unlike other methods, this accepts `strokeWidth` (and not `strokeRadius`). | ||
* | ||
* `strokeRadius` is half of `strokeWidth`. | ||
*/ | ||
roughlyIntersects(rect: Rect2, strokeWidth?: number): boolean; | ||
@@ -103,7 +120,15 @@ closedRoughlyIntersects(rect: Rect2): boolean; | ||
private cachedStringVersion; | ||
toString(useNonAbsCommands?: boolean): string; | ||
/** | ||
* Convert to an [SVG path representation](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths). | ||
* | ||
* If `useNonAbsCommands` is given, relative path commands (e.g. `l10,0`) are to be used instead of | ||
* absolute commands (e.g. `L10,0`). | ||
* | ||
* See also {@link fromString}. | ||
*/ | ||
toString(useNonAbsCommands?: boolean, ignoreCache?: boolean): string; | ||
serialize(): string; | ||
static toString(startPoint: Point2, parts: PathCommand[], onlyAbsCommands?: boolean): string; | ||
/** | ||
* Create a Path from a SVG path specification. | ||
* Create a `Path` from a subset of the SVG path specification. | ||
* | ||
@@ -114,2 +139,10 @@ * ## To-do | ||
* - TODO: Support `s`,`t` commands shorthands. | ||
* | ||
* @example | ||
* ```ts,runnable,console | ||
* import { Path } from '@js-draw/math'; | ||
* | ||
* const path = Path.fromString('m0,0l100,100'); | ||
* console.log(path.toString(true)); // true: Prefer relative to absolute path commands | ||
* ``` | ||
*/ | ||
@@ -116,0 +149,0 @@ static fromString(pathString: string): Path; |
@@ -25,8 +25,14 @@ "use strict"; | ||
class Path { | ||
/** | ||
* Creates a new `Path` that starts at `startPoint` and is made up of the path commands, | ||
* `parts`. | ||
* | ||
* See also {@link fromString} | ||
*/ | ||
constructor(startPoint, parts) { | ||
this.startPoint = startPoint; | ||
this.parts = parts; | ||
this.cachedGeometry = null; | ||
this.cachedPolylineApproximation = null; | ||
this.cachedStringVersion = null; | ||
this.parts = parts; | ||
// Initial bounding box contains one point: the start point. | ||
@@ -36,3 +42,3 @@ this.bbox = Rect2_1.default.bboxOf([startPoint]); | ||
// calculation) | ||
for (const part of parts) { | ||
for (const part of this.parts) { | ||
this.bbox = this.bbox.union(Path.computeBBoxForSegment(startPoint, part)); | ||
@@ -342,2 +348,4 @@ } | ||
* If `strokeRadius > 0`, the resultant `parameterValue` has no defined value. | ||
* | ||
* **Note**: `strokeRadius` is half of a stroke's width. | ||
*/ | ||
@@ -438,2 +446,12 @@ intersection(line, strokeRadius) { | ||
} | ||
/** | ||
* Like {@link closedRoughlyIntersects} except takes stroke width into account. | ||
* | ||
* This is intended to be a very fast and rough approximation. Use {@link intersection} | ||
* and {@link signedDistance} for more accurate (but much slower) intersection calculations. | ||
* | ||
* **Note**: Unlike other methods, this accepts `strokeWidth` (and not `strokeRadius`). | ||
* | ||
* `strokeRadius` is half of `strokeWidth`. | ||
*/ | ||
roughlyIntersects(rect, strokeWidth = 0) { | ||
@@ -466,3 +484,3 @@ if (this.parts.length === 0) { | ||
} | ||
// Treats this as a closed path and returns true if part of `rect` is roughly within | ||
// Treats this as a closed path and returns true if part of `rect` is *roughly* within | ||
// this path's interior. | ||
@@ -549,4 +567,12 @@ // | ||
} | ||
toString(useNonAbsCommands) { | ||
if (this.cachedStringVersion) { | ||
/** | ||
* Convert to an [SVG path representation](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths). | ||
* | ||
* If `useNonAbsCommands` is given, relative path commands (e.g. `l10,0`) are to be used instead of | ||
* absolute commands (e.g. `L10,0`). | ||
* | ||
* See also {@link fromString}. | ||
*/ | ||
toString(useNonAbsCommands, ignoreCache = false) { | ||
if (this.cachedStringVersion && !ignoreCache) { | ||
return this.cachedStringVersion; | ||
@@ -641,3 +667,3 @@ } | ||
/** | ||
* Create a Path from a SVG path specification. | ||
* Create a `Path` from a subset of the SVG path specification. | ||
* | ||
@@ -648,2 +674,10 @@ * ## To-do | ||
* - TODO: Support `s`,`t` commands shorthands. | ||
* | ||
* @example | ||
* ```ts,runnable,console | ||
* import { Path } from '@js-draw/math'; | ||
* | ||
* const path = Path.fromString('m0,0l100,100'); | ||
* console.log(path.toString(true)); // true: Prefer relative to absolute path commands | ||
* ``` | ||
*/ | ||
@@ -650,0 +684,0 @@ static fromString(pathString) { |
@@ -23,2 +23,3 @@ /** | ||
export { QuadraticBezier } from './shapes/QuadraticBezier'; | ||
export { Abstract2DShape } from './shapes/Abstract2DShape'; | ||
export { Mat33, Mat33Array } from './Mat33'; | ||
@@ -25,0 +26,0 @@ export { Point2, Vec2 } from './Vec2'; |
import LineSegment2 from './LineSegment2'; | ||
import { Point2 } from '../Vec2'; | ||
import Rect2 from './Rect2'; | ||
declare abstract class Abstract2DShape { | ||
/** | ||
* An abstract base class for 2D shapes. | ||
*/ | ||
export declare abstract class Abstract2DShape { | ||
protected static readonly smallValue = 1e-12; | ||
@@ -6,0 +9,0 @@ /** |
@@ -38,4 +38,2 @@ import LineSegment2 from './LineSegment2'; | ||
} | ||
type GeometryType = Abstract2DShape; | ||
type GeometryArrayType = Array<GeometryType>; | ||
/** | ||
@@ -46,3 +44,2 @@ * Represents a union of lines and curves. | ||
readonly startPoint: Point2; | ||
readonly parts: PathCommand[]; | ||
/** | ||
@@ -54,6 +51,14 @@ * A rough estimate of the bounding box of the path. | ||
readonly bbox: Rect2; | ||
constructor(startPoint: Point2, parts: PathCommand[]); | ||
/** The individual shapes that make up this path. */ | ||
readonly parts: Readonly<PathCommand>[]; | ||
/** | ||
* Creates a new `Path` that starts at `startPoint` and is made up of the path commands, | ||
* `parts`. | ||
* | ||
* See also {@link fromString} | ||
*/ | ||
constructor(startPoint: Point2, parts: Readonly<PathCommand>[]); | ||
getExactBBox(): Rect2; | ||
private cachedGeometry; | ||
get geometry(): GeometryArrayType; | ||
get geometry(): Abstract2DShape[]; | ||
/** | ||
@@ -83,2 +88,4 @@ * Iterates through the start/end points of each component in this path. | ||
* If `strokeRadius > 0`, the resultant `parameterValue` has no defined value. | ||
* | ||
* **Note**: `strokeRadius` is half of a stroke's width. | ||
*/ | ||
@@ -91,2 +98,12 @@ intersection(line: LineSegment2, strokeRadius?: number): IntersectionResult[]; | ||
private getEndPoint; | ||
/** | ||
* Like {@link closedRoughlyIntersects} except takes stroke width into account. | ||
* | ||
* This is intended to be a very fast and rough approximation. Use {@link intersection} | ||
* and {@link signedDistance} for more accurate (but much slower) intersection calculations. | ||
* | ||
* **Note**: Unlike other methods, this accepts `strokeWidth` (and not `strokeRadius`). | ||
* | ||
* `strokeRadius` is half of `strokeWidth`. | ||
*/ | ||
roughlyIntersects(rect: Rect2, strokeWidth?: number): boolean; | ||
@@ -103,7 +120,15 @@ closedRoughlyIntersects(rect: Rect2): boolean; | ||
private cachedStringVersion; | ||
toString(useNonAbsCommands?: boolean): string; | ||
/** | ||
* Convert to an [SVG path representation](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths). | ||
* | ||
* If `useNonAbsCommands` is given, relative path commands (e.g. `l10,0`) are to be used instead of | ||
* absolute commands (e.g. `L10,0`). | ||
* | ||
* See also {@link fromString}. | ||
*/ | ||
toString(useNonAbsCommands?: boolean, ignoreCache?: boolean): string; | ||
serialize(): string; | ||
static toString(startPoint: Point2, parts: PathCommand[], onlyAbsCommands?: boolean): string; | ||
/** | ||
* Create a Path from a SVG path specification. | ||
* Create a `Path` from a subset of the SVG path specification. | ||
* | ||
@@ -114,2 +139,10 @@ * ## To-do | ||
* - TODO: Support `s`,`t` commands shorthands. | ||
* | ||
* @example | ||
* ```ts,runnable,console | ||
* import { Path } from '@js-draw/math'; | ||
* | ||
* const path = Path.fromString('m0,0l100,100'); | ||
* console.log(path.toString(true)); // true: Prefer relative to absolute path commands | ||
* ``` | ||
*/ | ||
@@ -116,0 +149,0 @@ static fromString(pathString: string): Path; |
{ | ||
"name": "@js-draw/math", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "A math library for js-draw. ", | ||
@@ -48,3 +48,3 @@ "types": "./dist/mjs/lib.d.ts", | ||
], | ||
"gitHead": "e824c37e9f216852cf096976e3a74fb4f177ead3" | ||
"gitHead": "ccf1d0634e902c731fcd794df11cd001c3a30585" | ||
} |
@@ -33,2 +33,3 @@ /** | ||
export { QuadraticBezier } from './shapes/QuadraticBezier'; | ||
export { Abstract2DShape } from './shapes/Abstract2DShape'; | ||
@@ -35,0 +36,0 @@ export { Mat33, Mat33Array } from './Mat33'; |
@@ -5,3 +5,7 @@ import LineSegment2 from './LineSegment2'; | ||
abstract class Abstract2DShape { | ||
/** | ||
* An abstract base class for 2D shapes. | ||
*/ | ||
export abstract class Abstract2DShape { | ||
// @internal | ||
protected static readonly smallValue = 1e-12; | ||
@@ -8,0 +12,0 @@ |
@@ -50,5 +50,5 @@ import Path, { PathCommandType } from './Path'; | ||
const path1 = Path.fromString('M100,100 L101,101 Q102,102 90.000000001,89.99999999 Z'); | ||
path1['cachedStringVersion'] = null; // Clear the cache. | ||
const ignoreCache = true; | ||
expect(path1.toString()).toBe([ | ||
expect(path1.toString(undefined, ignoreCache)).toBe([ | ||
'M100,100', 'l1,1', 'q1,1 -11-11', 'l10,10' | ||
@@ -62,5 +62,5 @@ ].join('')); | ||
const path1 = Path.fromString(pathStr); | ||
path1['cachedStringVersion'] = null; // Clear the cache. | ||
const path = Path.fromString(path1.toString(true)); | ||
path1['cachedStringVersion'] = null; // Clear the cache. | ||
const ignoreCache = true; | ||
const path = Path.fromString(path1.toString(true, ignoreCache)); | ||
path1['cachedStringVersion'] = null; // Clear the cache manually | ||
@@ -67,0 +67,0 @@ expect(path.toString(true)).toBe(path1.toString(true)); |
@@ -54,5 +54,2 @@ import { toRoundedString, toStringOfSamePrecision } from '../rounding'; | ||
type GeometryType = Abstract2DShape; | ||
type GeometryArrayType = Array<GeometryType>; | ||
/** | ||
@@ -69,3 +66,17 @@ * Represents a union of lines and curves. | ||
public constructor(public readonly startPoint: Point2, public readonly parts: PathCommand[]) { | ||
/** The individual shapes that make up this path. */ | ||
public readonly parts: Readonly<PathCommand>[]; | ||
/** | ||
* Creates a new `Path` that starts at `startPoint` and is made up of the path commands, | ||
* `parts`. | ||
* | ||
* See also {@link fromString} | ||
*/ | ||
public constructor( | ||
public readonly startPoint: Point2, | ||
parts: Readonly<PathCommand>[], | ||
) { | ||
this.parts = parts; | ||
// Initial bounding box contains one point: the start point. | ||
@@ -76,3 +87,3 @@ this.bbox = Rect2.bboxOf([startPoint]); | ||
// calculation) | ||
for (const part of parts) { | ||
for (const part of this.parts) { | ||
this.bbox = this.bbox.union(Path.computeBBoxForSegment(startPoint, part)); | ||
@@ -91,6 +102,6 @@ } | ||
private cachedGeometry: GeometryArrayType|null = null; | ||
private cachedGeometry: Abstract2DShape[]|null = null; | ||
// Lazy-loads and returns this path's geometry | ||
public get geometry(): GeometryArrayType { | ||
public get geometry(): Abstract2DShape[] { | ||
if (this.cachedGeometry) { | ||
@@ -101,3 +112,3 @@ return this.cachedGeometry; | ||
let startPoint = this.startPoint; | ||
const geometry: GeometryArrayType = []; | ||
const geometry: Abstract2DShape[] = []; | ||
@@ -266,3 +277,3 @@ for (const part of this.parts) { | ||
type DistanceFunctionRecord = { | ||
part: GeometryType, | ||
part: Abstract2DShape, | ||
bbox: Rect2, | ||
@@ -306,3 +317,3 @@ distFn: DistanceFunction, | ||
// line could intersect are considered. | ||
const sdf = (point: Point2): [GeometryType|null, number] => { | ||
const sdf = (point: Point2): [Abstract2DShape|null, number] => { | ||
let minDist = Infinity; | ||
@@ -476,2 +487,4 @@ let minDistPart: Abstract2DShape|null = null; | ||
* If `strokeRadius > 0`, the resultant `parameterValue` has no defined value. | ||
* | ||
* **Note**: `strokeRadius` is half of a stroke's width. | ||
*/ | ||
@@ -587,2 +600,12 @@ public intersection(line: LineSegment2, strokeRadius?: number): IntersectionResult[] { | ||
/** | ||
* Like {@link closedRoughlyIntersects} except takes stroke width into account. | ||
* | ||
* This is intended to be a very fast and rough approximation. Use {@link intersection} | ||
* and {@link signedDistance} for more accurate (but much slower) intersection calculations. | ||
* | ||
* **Note**: Unlike other methods, this accepts `strokeWidth` (and not `strokeRadius`). | ||
* | ||
* `strokeRadius` is half of `strokeWidth`. | ||
*/ | ||
public roughlyIntersects(rect: Rect2, strokeWidth: number = 0) { | ||
@@ -621,3 +644,3 @@ if (this.parts.length === 0) { | ||
// Treats this as a closed path and returns true if part of `rect` is roughly within | ||
// Treats this as a closed path and returns true if part of `rect` is *roughly* within | ||
// this path's interior. | ||
@@ -726,4 +749,12 @@ // | ||
public toString(useNonAbsCommands?: boolean): string { | ||
if (this.cachedStringVersion) { | ||
/** | ||
* Convert to an [SVG path representation](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths). | ||
* | ||
* If `useNonAbsCommands` is given, relative path commands (e.g. `l10,0`) are to be used instead of | ||
* absolute commands (e.g. `L10,0`). | ||
* | ||
* See also {@link fromString}. | ||
*/ | ||
public toString(useNonAbsCommands?: boolean, ignoreCache: boolean = false): string { | ||
if (this.cachedStringVersion && !ignoreCache) { | ||
return this.cachedStringVersion; | ||
@@ -831,3 +862,3 @@ } | ||
/** | ||
* Create a Path from a SVG path specification. | ||
* Create a `Path` from a subset of the SVG path specification. | ||
* | ||
@@ -838,2 +869,10 @@ * ## To-do | ||
* - TODO: Support `s`,`t` commands shorthands. | ||
* | ||
* @example | ||
* ```ts,runnable,console | ||
* import { Path } from '@js-draw/math'; | ||
* | ||
* const path = Path.fromString('m0,0l100,100'); | ||
* console.log(path.toString(true)); // true: Prefer relative to absolute path commands | ||
* ``` | ||
*/ | ||
@@ -840,0 +879,0 @@ public static fromString(pathString: string): Path { |
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
464839
12711