Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rain #17

Merged
merged 5 commits into from
May 21, 2021
Merged

rain #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @zemn.me/math/canvas/element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Canvas: React.FC<CanvasProps> = ({ draw }) => {
d={d}
key={d}
style={{ fill: 'none', stroke: 'black' }}
vectorEffect="none-scaling-stroke"
vectorEffect="non-scaling-stroke"
/>
)
})}
Expand Down
4 changes: 3 additions & 1 deletion @zemn.me/math/cartesian.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as Matrix from './matrix'
export type Point<T extends number = number> = Matrix.Matrix<1, T>
export type Vector<T extends number = number> = Matrix.Matrix<1, T>
export type Point<T extends number = number> = Vector<T>
export type Point2D = Point<2>
export type Point3D = Point<3>
export type Line2D = Point2D[]
export type Line3D = Point3D[]
export type Vec3D = Point3D
13 changes: 11 additions & 2 deletions @zemn.me/math/homog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ type Extends<T1, T2> = T2 extends T1 ? T2 : never

export type Point2D = Matrix.Matrix<1, 3>
export type Point3D = Matrix.Matrix<1, 4>
export type Line2D = Point2D[]
export type Line3D = Point3D[]
export type Vec3D = Point3D
export type Vec2D = Point2D
export type Line2D = readonly Point2D[]
export type Line3D = readonly Point3D[]

export function pointToCart(p: Point2D): Cart.Point2D
export function pointToCart(p: Point3D): Cart.Point3D
Expand All @@ -23,3 +25,10 @@ export function lineToCart(p: Line3D): Cart.Line3D
export function lineToCart(line: Line2D | Line3D): Cart.Line2D | Cart.Line3D {
return line.map((point: any) => pointToCart(point))
}

export function fromCart(p: Cart.Point2D): Point2D
export function fromCart(p: Cart.Point3D): Point3D

export function fromCart(p: Cart.Point2D | Cart.Point3D): any {
return [...p, [1]]
}
33 changes: 33 additions & 0 deletions @zemn.me/math/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ describe('matrix', () => {
expect(matrix.add(a, b)).toEqual(o)
})
})

describe('.asVec', () => {
test('1', () => {
const i = [[1], [2], [3]] as const
expect(matrix.asVec(i)).toEqual([1, 2, 3])
})
})
describe('.fromVec', () => {
test('1', () => {
const i = [[1], [2], [3]] as const
expect(i).toEqual(matrix.fromVec(matrix.asVec(i)))
})
})
})

describe('vec', () => {
Expand All @@ -381,4 +394,24 @@ describe('vec', () => {
},
)
})

describe('.mag', () => {
test('1', () => {
expect(vec.mag([4, 4])).toEqual(
Math.sqrt(Math.pow(4, 2) + Math.pow(4, 2)),
)
})
})

/*

describe('.unit', () => {
test('1', () => {
expectMatrixSimilar([vec.unit([2, 2])], [[1, 1]])
})
test('2', () => {
expectMatrixSimilar([vec.unit([1, 1])], [[1, 1]])
})
})
*/
})
6 changes: 6 additions & 0 deletions @zemn.me/math/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,9 @@ export const inverse: <IJ extends number>(m: Square<IJ>) => Square<IJ> = <

return map(transpose(checkerboard(minors(m))), (n) => d * n)
}

export const asVec = <I extends number>(m: Matrix<1, I>): Vector<I, number> =>
[].concat(...(m as any)) as any

export const fromVec = <I extends number>(m: vec.Vector<I>): Matrix<1, I> =>
map(New(1, m.length), (_, [i, j]) => m[j])
110 changes: 0 additions & 110 deletions @zemn.me/math/shape/3d.ts

This file was deleted.

13 changes: 13 additions & 0 deletions @zemn.me/math/shape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ export class Translate3D<T extends Canvas.Drawable3D>
}
}

export class Group3D<T extends readonly Canvas.Drawable3D[]>
implements Canvas.Drawable3D
{
readonly targets: T
constructor(...targets: T) {
this.targets = targets
}

public lines3D(): Homog.Line3D[] {
return [].concat.apply(this.targets.map((t) => t.lines3D()))
}
}

export class Cube implements Canvas.Drawable3D {
private readonly square: Square
constructor(public readonly diameter: number) {
Expand Down
78 changes: 78 additions & 0 deletions @zemn.me/math/sim/particle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as Vec from '@zemn.me/math/vec'

export type Simulation<D extends number = number> = (
particles: Particle<D>[],
elapsedSeconds: number,
) => { done?: true } | undefined

export interface Particle<D extends number = number> {
/**
* kg
*/
mass: number

/**
* m/s
*/
speed: Vec.Vector<D>

/**
* m
*/
displacement: Vec.Vector<D>
}

export type Force<D extends number = number> = Vec.Vector<D>

export type Field<D extends number = number> = (
particle: Particle<number>,
globalTime: number,
) => Force<D>

export const Gravity: Field<number> = ({ mass }) => [0, -mass * 10] as const

/**
* @param force (particle) => kg * m/s/s
* @param time seconds
*/
export const simulate: <D extends number>(
p: Particle<D>,
timeDelta: number,
...fields: Field<D>[]
) => void = (particle, timeDelta, ...fields) => {
const [firstForce, ...otherForces] = fields.map((f) =>
f(particle, timeDelta),
)
const resultantForce = otherForces.reduce(
(a, c) => Vec.add(a, c),
firstForce,
)

let acc = Vec.div(resultantForce, particle.mass)

let dx // change in displacement
const v0 = particle.speed
const a = acc
const t = timeDelta

const timeTravelledDueToExtantSpeed = Vec.mul(t, v0)
const timeTravelledDuetoAccel = Vec.mul(0.5 * Math.pow(t, 2), a)
const increaseInSpeed = Vec.mul(t, acc)

dx = Vec.add(timeTravelledDueToExtantSpeed, timeTravelledDuetoAccel)

particle.displacement = Vec.add(particle.displacement, dx)
particle.speed = Vec.add(particle.speed, increaseInSpeed)

/*console.table({
mass: particle.mass,
speed: particle.speed,
position: particle.displacement,
"experienced accel.": acc,
"under force": resultantForce,
dx,
timeDelta,
timeTravelledDueToExtantSpeed ,
increaseInSpeed
});*/
}
16 changes: 16 additions & 0 deletions @zemn.me/math/sim/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const kg = 1
export const kilogram = kg
export const g = kg / 1000
export const gram = g

export const second = 1
export const s = second
export const millisecond = 1000
export const ms = millisecond

export const meter = 1
export const m = meter
export const millimeter = meter / 1000
export const mm = millimeter
export const cm = meter / 100
export const centimenter = cm
20 changes: 19 additions & 1 deletion @zemn.me/math/vec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,28 @@ export const New: <N extends number>(n: number) => Vector<N, undefined> = (n) =>
export const add: <I extends number>(
v1: Vector<I>,
v2: Vector<I>,
) => Vector<I> = (v1, v2) => map(v1, (v, i) => v + v2[i])
) => Vector<I> = (v1, v2) => {
;[v1, v2] = [v1, v2].sort((a, b) => b.length - a.length)
return map(v1, (v, i) => v + (i in v2 ? v2[i] : 0))
}

export const mul: <I extends number>(v1: number, v2: Vector<I>) => Vector<I> = (
v1,
v2,
) => map(v2, (v, i) => v * v1)

export const div: <I extends number>(v1: Vector<I>, v2: number) => Vector<I> = (
v2,
v1,
) => map(v2, (v, i) => v / v1)

/*
export const unit = <T extends number>(vector: Vector<T>): Vector<T> => {
const m = mag(vector)
return map(vector, (v) => v / m)
}
*/

export const dot: (v1: Iterable<number>, v2: Iterable<number>) => number = (
v1,
v2,
Expand Down Expand Up @@ -100,3 +115,6 @@ export const zip: {
[T1 | T3, T2 | T3]
>
} = _zip as any

export const mag = (v: Vector<number>): number =>
Math.sqrt(sum(map(v, (v) => Math.pow(v, 2))))
Loading