-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f051da8
commit a5e084e
Showing
3 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Point } from './types'; | ||
|
||
// Refer: https://github.com/Jam3/chaikin-smooth/blob/master/index.js | ||
|
||
export function smoothOnce(...points: Point[]): Point[] { | ||
const ret: Point[] = []; | ||
if (points.length === 0) { | ||
return []; | ||
} | ||
ret.push({ ...points[0] }); | ||
for (let i = 0; i < points.length - 1; i++) { | ||
const p0 = points[i]; | ||
const p1 = points[i + 1]; | ||
|
||
ret.push( | ||
{ | ||
x: 0.75 * p0.x + 0.25 * p1.x, | ||
y: 0.75 * p0.y + 0.25 * p1.y, | ||
w: p0.w * 0.75 + p1.w * 0.25, | ||
}, | ||
{ | ||
x: 0.25 * p0.x + 0.75 * p1.x, | ||
y: 0.25 * p0.y + 0.75 * p1.y, | ||
w: p0.w * 0.25 + p1.w * 0.75, | ||
} | ||
); | ||
} | ||
if (points.length > 2) { | ||
ret.push({ ...points[points.length - 1] }); | ||
} | ||
return ret; | ||
} | ||
|
||
export function smooth(points: Point[], times = 1): Point[] { | ||
let ret = points; | ||
for (let count = 0; count < times; count += 1) { | ||
ret = smoothOnce(...ret); | ||
} | ||
return ret; | ||
} |