Skip to content

Commit

Permalink
feat: EquilateralPloygonCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Jan 6, 2025
1 parent 1cc87e6 commit eadefcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core/Path2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ export class Path2D extends CompositeCurve<CurvePath> {
}

toCommands(): Path2DCommand[] {
return this.curves.flatMap(path => path.toCommands())
return this.curves.flatMap(v => v.toCommands())
}

toData(): Path2DData {
return this.curves.map(path => path.toData()).join(' ')
return this.curves.filter(v => v.curves.length).map(v => v.toData()).join(' ')
}

toSVGPathString(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/curve/ArcCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { EllipseCurve } from './EllipseCurve'
export class ArcCurve extends EllipseCurve {
constructor(
cx = 0, cy = 0,
r = 1,
radius = 1,
startAngle = 0,
endAngle = Math.PI * 2,
clockwise = false,
) {
super(cx, cy, r, r, 0, startAngle, endAngle, clockwise)
super(cx, cy, radius, radius, 0, startAngle, endAngle, clockwise)
}

override getAdaptivePointArray(output: number[] = []): number[] {
Expand Down
29 changes: 27 additions & 2 deletions src/curve/PloygonCurve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import type { LineCurve } from './LineCurve'
import { Vector2 } from '../math'
import { CompositeCurve } from './CompositeCurve'
import { LineCurve } from './LineCurve'

export class PloygonCurve extends CompositeCurve<LineCurve> {
//
static equilateral(
cx = 0,
cy = 0,
radius = 1,
sideCount = 3,
): PloygonCurve {
const ploygon = new PloygonCurve()
const points: Vector2[] = []
for (let i = 0; i < sideCount; i++) {
const radian = (i * 2 * Math.PI) / sideCount - 0.5 * Math.PI
points.push(
new Vector2(
radius * Math.cos(radian),
radius * Math.sin(radian),
)
.add({ x: cx, y: cy }),
)
}
for (let i = 0; i < sideCount; i++) {
ploygon.curves.push(
LineCurve.from(points[i], points[(i + 1) % sideCount]),
)
}
return ploygon
}
}

0 comments on commit eadefcb

Please sign in to comment.