Skip to content

Commit

Permalink
Calculate the center automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jul 31, 2024
1 parent c67f07e commit 2839abc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@turf/area": "^7.0.0",
"@turf/bbox": "^7.0.0",
"@turf/bearing": "^7.0.0",
"@turf/center": "^7.0.0",
"@turf/destination": "^7.0.0",
"@turf/distance": "^7.0.0",
"@turf/length": "^7.0.0",
Expand Down
7 changes: 4 additions & 3 deletions src/routes/route_check/jat_check/EditJunction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@
};
function autogenerateMovements() {
let [center, ...arms] = $state.jat[junctionIdx][stage].arms;
$state.jat[junctionIdx][stage].movements = generateMovements(center, arms);
$state.jat[junctionIdx][stage].movements = generateMovements(
$state.jat[junctionIdx][stage].arms,
);
}
</script>

Expand Down Expand Up @@ -196,7 +197,7 @@
</p>
{#if $state.jat[junctionIdx][stage].arms.length > 0}
<SecondaryButton on:click={autogenerateMovements}>
Generate cycling movements between all arms (1st arm is the center)
Generate cycling movements between all arms
</SecondaryButton>
{/if}
{#each $state.jat[junctionIdx][stage].movements as movement, idx}
Expand Down
27 changes: 20 additions & 7 deletions src/routes/route_check/jat_check/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import type { Position } from "$lib/map";
import destination from "@turf/destination";
import bearing from "@turf/bearing";
import distance from "@turf/distance";
import turfCenter from "@turf/center";

export function generateMovements(center: Arm, arms: Arm[]): Movement[] {
export function generateMovements(arms: Arm[]): Movement[] {
let spacingMeters = 0.003;
let jitter = (idx: number, offset: number) => {
let pt = arms[idx].point;
let angleToCenter = bearing(pt, center.point);
let angleToCenter = bearing(pt, center);
// This should wind up on the left side of the road
let perpAngle = angleToCenter - 90;
let projectDistance = spacingMeters * offset;
Expand All @@ -18,26 +19,38 @@ export function generateMovements(center: Arm, arms: Arm[]): Movement[] {
};

// Sort arms around center in CCW order
let center = turfCenter({
type: "FeatureCollection",
features: arms.map((arm) => {
return {
type: "Feature",
geometry: {
type: "Point",
coordinates: arm.point,
},
};
}),
}).geometry.coordinates;
arms.sort(
(a1, a2) =>
normalize(bearing(center.point, a2.point)) -
normalize(bearing(center.point, a1.point)),
normalize(bearing(center, a2.point)) -
normalize(bearing(center, a1.point)),
);

let movements = [];
for (let i = 0; i < arms.length; i++) {
let arm1 = arms[i];
let angleStartToCenter = bearing(arm1.point, center.point);
let angleStartToCenter = bearing(arm1.point, center);

// Continue in order
let toCount = 1;
for (let j of order(i, arms.length)) {
let arm2 = arms[j];
let angleCenterToEnd = bearing(center.point, arm2.point);
let angleCenterToEnd = bearing(center, arm2.point);
// Start at the arm, far from the junction
let point1 = jitter(i, toCount++);
// Then go some percent towards the junction
let dist = distance(arm1.point, center.point);
let dist = distance(arm1.point, center);
let point2 = destination(point1, 0.5 * dist, angleStartToCenter).geometry
.coordinates as Position;
let point3 = destination(point2, 0.2 * dist, angleCenterToEnd).geometry
Expand Down

0 comments on commit 2839abc

Please sign in to comment.