-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stub some plainer primitives * fix imports * simplify math and primitives * remove tests * stub graph * format vector * flesh out vw graph editor
- Loading branch information
Showing
9 changed files
with
878 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, it, expect, beforeEach } from "vitest"; | ||
import { Vector } from "./vector"; | ||
|
||
describe("vector test", () => { | ||
let sut: Vector; | ||
beforeEach(() => { | ||
sut = new Vector(0, 0); | ||
}); | ||
it("adds 1 + 2 to equal 3", () => { | ||
const v23 = new Vector(2, 3); | ||
expect(sut.add(v23).toString()).toEqual("(2,3)"); | ||
expect(sut.clone().add(new Vector(1, 1)).toString()).toEqual("(3,4)"); | ||
expect(sut.clone().sub(new Vector(2, 3)).toString()).toEqual("(0,0)"); | ||
sut.sub(v23); | ||
expect(sut.toString()).toEqual("(0,0)"); | ||
}); | ||
}); |
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,53 @@ | ||
export class Vector { | ||
public constructor(public x: number, public y: number) {} | ||
public add(other: Vector): this { | ||
this.x += other.x; | ||
this.y += other.y; | ||
return this; | ||
} | ||
public clone(): Vector { | ||
return new Vector(this.x, this.y); | ||
} | ||
public copy(other: Vector): this { | ||
this.x = other.x; | ||
this.y = other.y; | ||
return this; | ||
} | ||
public divide(other: Vector): this { | ||
this.x /= other.x; | ||
this.y /= other.y; | ||
return this; | ||
} | ||
public equals(other: Vector): boolean { | ||
return this.x === other.x && this.y === other.y; | ||
} | ||
public length(): number { | ||
return Math.sqrt(this.lengthSquared()); | ||
} | ||
public lengthSquared(): number { | ||
return this.x * this.x + this.y * this.y; | ||
} | ||
public mix(other: Vector, amount = 0.5): this { | ||
const interp = (start: number, end: number): number => { | ||
const tot = end - start; | ||
const dx = tot * amount; | ||
return start + dx; | ||
}; | ||
this.x = interp(this.x, other.x); | ||
this.y = interp(this.y, other.y); | ||
return this; | ||
} | ||
public norm(): this { | ||
const length = this.length(); | ||
this.divide(new Vector(length, length)); | ||
return this; | ||
} | ||
public sub(other: Vector): this { | ||
this.x -= other.x; | ||
this.y -= other.y; | ||
return this; | ||
} | ||
public toString(): string { | ||
return `(${this.x},${this.y})`; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import type { PageLoad, RouteParams } from "./$types"; | ||
import type { BBox, MultiPolygon, Position } from "geojson"; | ||
import type { PageLoad } from "./$types"; | ||
import { Point, Segment } from "./world/primitives"; | ||
|
||
export const load = (async ({ params, fetch }) => { | ||
const resp = await fetch("/districts/states/MO/shape.geojson"); | ||
let coordinates: Position[][][] = []; | ||
let bbox: BBox | undefined; | ||
if (resp.statusText === "OK") { | ||
const d = (await resp.json()) as MultiPolygon; | ||
if (d.type === "MultiPolygon") { | ||
coordinates = d.coordinates; | ||
bbox = d.bbox; | ||
} | ||
} | ||
const points: Point[] = [ | ||
new Point(10, 10), | ||
new Point(20, 20), | ||
new Point(30, 20), | ||
]; | ||
const segments: Segment[] = [ | ||
new Segment(points[0], points[1]), | ||
new Segment(points[1], points[2]), | ||
]; | ||
|
||
return { | ||
coordinates, | ||
bbox, | ||
points, | ||
segments, | ||
}; | ||
}) satisfies PageLoad; |
Oops, something went wrong.