Updates getPointsBetween

This commit is contained in:
Steve Ruiz 2021-06-24 13:56:25 +01:00
parent 062dbafc36
commit cbc26f2e06
3 changed files with 43 additions and 10 deletions

View file

@ -970,9 +970,12 @@ interface ShapeUtility<K extends Shape> {
* ## Utils * ## Utils
*/ */
class Utils { class Utils {
static pointsBetween(a: number[], b: number[], steps = 6): number[][] { static getPointsBetween(a: number[], b: number[], steps = 6): number[][] {
return Array.from(Array(steps)) return Array.from(Array(steps))
.map((_, i) => ease(i / steps)) .map((_, i) => {
const t = i / steps
return t * t * t
})
.map((t) => [...vec.lrp(a, b, t), (1 - t) / 2]) .map((t) => [...vec.lrp(a, b, t), (1 - t) / 2])
} }
@ -1605,7 +1608,7 @@ interface ShapeUtility<K extends Shape> {
} }
/** /**
* Get a vector d distance from A towards B. * Push a point A towards point B by a given distance.
* @param A * @param A
* @param B * @param B
* @param d * @param d
@ -1615,6 +1618,16 @@ interface ShapeUtility<K extends Shape> {
return Vec.add(A, Vec.mul(Vec.uni(Vec.vec(A, B)), d)) return Vec.add(A, Vec.mul(Vec.uni(Vec.vec(A, B)), d))
} }
/**
* Push a point in a given angle by a given distance.
* @param A
* @param B
* @param d
*/
static nudgeAtAngle = (A: number[], a: number, d: number): number[] => {
return [Math.cos(a) * d + A[0], Math.sin(a) * d + A[1]]
}
/** /**
* Round a vector to a precision length. * Round a vector to a precision length.
* @param a * @param a
@ -1623,6 +1636,21 @@ interface ShapeUtility<K extends Shape> {
static toPrecision = (a: number[], n = 4): number[] => { static toPrecision = (a: number[], n = 4): number[] => {
return [+a[0].toPrecision(n), +a[1].toPrecision(n)] return [+a[0].toPrecision(n), +a[1].toPrecision(n)]
} }
/**
* Get a number of points between two points.
* @param a
* @param b
* @param steps
*/
static pointsBetween = (a: number[], b: number[], steps = 6): number[][] => {
return Array.from(Array(steps))
.map((_, i) => {
const t = i / steps
return t * t * t
})
.map((t) => [...Vec.lrp(a, b, t), (1 - t) / 2])
}
} }
`, `,

View file

@ -5,6 +5,15 @@ import vec from 'utils/vec'
* ## Utils * ## Utils
*/ */
export default class Utils { export default class Utils {
static getPointsBetween(a: number[], b: number[], steps = 6): number[][] {
return Array.from(Array(steps))
.map((_, i) => {
const t = i / steps
return t * t * t
})
.map((t) => [...vec.lrp(a, b, t), (1 - t) / 2])
}
static getRayRayIntersection( static getRayRayIntersection(
p0: number[], p0: number[],
n0: number[], n0: number[],

View file

@ -96,13 +96,9 @@ const initialData: Data = {
code: ` code: `
const draw = new Draw({ const draw = new Draw({
points: [ points: [
[0, 0], ...Utils.getPointsBetween([0, 0], [20, 50]),
[0, 50], ...Utils.getPointsBetween([20, 50], [100, 20], 3),
[20, 80], ...Utils.getPointsBetween([100, 20], [100, 100], 10),
[56, 56],
[52, 52],
[80, 20],
[90, 90],
[100, 100], [100, 100],
], ],
}) })