Skip to content

Commit

Permalink
fix: getMinMax
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Nov 5, 2024
1 parent 9617fc1 commit 659f8f4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/math/Vector2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ export class Vector2 {
return this
}

min(...vecs: VectorLike[]): this {
this.x = Math.min(this.x, ...vecs.map(v => v.x))
this.y = Math.min(this.y, ...vecs.map(v => v.y))
return this
}

max(...vecs: VectorLike[]): this {
this.x = Math.max(this.x, ...vecs.map(v => v.x))
this.y = Math.max(this.y, ...vecs.map(v => v.y))
return this
}

normalize(): this {
return this.scale(1 / (this.length() || 1))
}
Expand Down
37 changes: 29 additions & 8 deletions src/paths/Path2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,35 @@ export class Path2D {
}

getMinMax(min = Vector2.MAX, max = Vector2.MIN, withStyle = true): { min: Vector2, max: Vector2 } {
this.getCurves().forEach(curve => curve.getMinMax(min, max))
if (withStyle) {
const strokeHalfWidth = this.strokeWidth / 2
min.x -= strokeHalfWidth
min.y -= strokeHalfWidth
max.x += strokeHalfWidth
max.y += strokeHalfWidth
}
const strokeWidth = this.strokeWidth
this.getCurves().forEach((curve) => {
curve.getMinMax(min, max)
if (withStyle) {
if (strokeWidth > 1) {
const halfStrokeWidth = strokeWidth / 2
const isClockwise = curve.isClockwise()
const points = []
for (let t = 0; t <= 1; t += 1 / curve.arcLengthDivisions) {
const point = curve.getPoint(t)
const normal = curve.getNormal(t)
const dist1 = normal.clone().scale(isClockwise ? halfStrokeWidth : -halfStrokeWidth)
const dist2 = normal.clone().scale(isClockwise ? -halfStrokeWidth : halfStrokeWidth)
points.push(
point.clone().add(dist1),
point.clone().add(dist2),
point.clone().add({ x: halfStrokeWidth, y: 0 }),
point.clone().add({ x: -halfStrokeWidth, y: 0 }),
point.clone().add({ x: 0, y: halfStrokeWidth }),
point.clone().add({ x: 0, y: -halfStrokeWidth }),
point.clone().add({ x: halfStrokeWidth, y: halfStrokeWidth }),
point.clone().add({ x: -halfStrokeWidth, y: -halfStrokeWidth }),
)
}
min.min(...points)
max.max(...points)
}
}
})
return { min, max }
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Path2D } from './paths'
import { BoundingBox, Vector2 } from './math'

export function getPathsBoundingBox(paths: Path2D[]): BoundingBox | undefined {
export function getPathsBoundingBox(paths: Path2D[], withStyle = true): BoundingBox | undefined {
if (!paths.length) {
return undefined
}
const min = Vector2.MAX
const max = Vector2.MIN
paths.forEach(path => path.getMinMax(min, max))
paths.forEach(path => path.getMinMax(min, max, withStyle))
return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y)
}

0 comments on commit 659f8f4

Please sign in to comment.