Skip to content

Commit

Permalink
convert moves to Js Array + UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbaker1 committed Aug 5, 2023
1 parent b8bc99f commit 357a7f0
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 172 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions string-bean-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
js-sys = "0.3.64"
string-bean = { path = "../string-bean" }
wasm-bindgen = "0.2.87"
56 changes: 44 additions & 12 deletions string-bean-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

type ReturnArray = js_sys::Array;

#[wasm_bindgen]
pub fn plan_as_json(
pub fn json_plan_circle(
line_count: u32,
line_opacity: f32,
anchor_count: u32,
Expand All @@ -12,7 +14,7 @@ pub fn plan_as_json(
height: usize,
image_buffer: &[u8],
start_anchor: usize,
) -> JsValue {
) -> ReturnArray {
let (x_mid, y_mid) = (width / 2, height / 2);
let radius = radius.min(x_mid.min(y_mid)) as f64;

Expand All @@ -37,18 +39,48 @@ pub fn plan_as_json(
image_buffer,
);

let moves = planner
planner
.get_moves(start_anchor, line_count as _)
.unwrap_or(Vec::new());
.unwrap_or(Vec::new())
.into_iter()
.map(JsValue::from)
.collect()
}

#[wasm_bindgen]
pub fn json_plan(
line_count: u32,
line_opacity: f32,
anchor_list: &[f64],
anchor_gap_count: usize,
penalty: f32,
width: usize,
height: usize,
image_buffer: &[u8],
start_anchor: usize,
) -> ReturnArray {
let anchors = anchor_list
.chunks_exact(2)
.map(|chunk| (chunk[0], chunk[1]))
.collect::<Vec<_>>();

JsValue::from_str(&format!(
"[{}]",
moves
.iter()
.map(usize::to_string)
.collect::<Vec<_>>()
.join(","),
))
let mut planner = string_bean::ThreadPlanner::new(
line_opacity as _,
&anchors,
anchor_gap_count,
penalty as _,
grid_raytrace,
width,
height,
image_buffer,
);

planner
.get_moves(start_anchor, line_count as _)
.unwrap_or(Vec::new())
.into_iter()
.map(JsValue::from)
.collect()
}

/// https://playtechs.blogspot.com/2007/03/raytracing-on-grid.html
Expand Down
56 changes: 56 additions & 0 deletions web/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Compute anchors for a circle
* @param {number} anchor_count
* @param {number} width
* @param {number} height
* @param {number} radius
* @returns {[number, number][]}
*/
export function circle_anchors(anchor_count, width, height, radius) {
const x_mid = width / 2;
const y_mid = height / 2;

return Array.from({ length: anchor_count })
.map((_, anchor) => (anchor * 2 * Math.PI) / anchor_count)
.map((angle) => [
x_mid + radius * Math.cos(angle),
y_mid + radius * Math.sin(angle),
]);
}

/**
* Compute anchors for a rectangle. Starts in the top left.
* @param {number} anchor_count
* @param {number} width
* @param {number} height
* @returns {[number, number][]}
*/
export function rectangle_anchors(anchor_count, width, height) {
/** @type {[number, number][]} */
const anchors = [[0, 0]];

const totalLength = width * 2 + height * 2;
const gapLength = totalLength / anchor_count;

let currentLength = 0;

for (let i = 1; i < anchor_count; i++) {
currentLength += gapLength;

if (currentLength < width) {
// walk top left to right
anchors.push([currentLength, 0]);
} else if (currentLength < width + height) {
// walk right top to bottom
anchors.push([width, currentLength - (width)]);
} else if (currentLength < width * 2 + height) {
// walk bottom right to left
anchors.push([2 * width - (currentLength - height), height]);
} else {
// walk left bottom to top
anchors.push([0, height + 2 * width - (currentLength - height)]);
}
}

return anchors;
}
Loading

0 comments on commit 357a7f0

Please sign in to comment.