From e3b90a78e7e85e186087487afc3b14c37bd4f302 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Fri, 11 Jul 2025 15:07:38 -0400 Subject: [PATCH] allow squashing of lines --- pkg/p5.js | 19 +++++++++++++++---- pkg/turtle_geometry.js | 10 ++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pkg/p5.js b/pkg/p5.js index e5eb4dd..8c92512 100644 --- a/pkg/p5.js +++ b/pkg/p5.js @@ -1,11 +1,22 @@ -import {eq_vect, eq_color, resolve_color, get_turtle_color, set_turtle_color, turtle_radius, turtle_angle, turn_to_rad, turtle_init, command_to_state, background_color, rotate, last} from "./turtle_geometry.js" +import {eq_vect, eq_color, resolve_color, get_turtle_color, set_turtle_color, turtle_radius, turtle_angle, turn_to_rad, turtle_init, command_to_state, background_color, rotate, last, sub} from "./turtle_geometry.js" -function states_to_call (prev, curr) { +function states_to_call (prev, curr, call_stack) { // console.log(prev) // console.log(curr) let calls = [] if (prev.pendown && !eq_vect(prev.position, curr.position)) { - calls.push(["line", prev.position[0], prev.position[1], curr.position[0], curr.position[1]]) + console.log("headings", prev.heading, curr.heading) + console.log("forward?", curr.forward) + console.log("#calls", call_stack.length) + if (prev.heading == curr.heading && curr.forward && call_stack.length > 0) { + console.log("extending line!") + const [x, y] = sub(curr.position, prev.position) + const last_call = call_stack.pop(); + const new_call = ["line", last_call[1], last_call[2], x + last_call[3], y + last_call[4]] + calls.push(new_call) + } else { + calls.push(["line", prev.position[0], prev.position[1], curr.position[0], curr.position[1]]) + } } if (!eq_color(curr.pencolor, prev.pencolor)) { calls.push(["stroke", ...resolve_color(curr.pencolor)]) @@ -80,7 +91,7 @@ export function p5 (commands) { for (let i = 1; i < states.length; ++i) { const prev = states[i - 1] const curr = states[i] - const calls = states_to_call(prev, curr) + const calls = states_to_call(prev, curr, my_calls) for (const call of calls) { // console.log(call) if (call === "clear") { diff --git a/pkg/turtle_geometry.js b/pkg/turtle_geometry.js index 0140f77..ec4461f 100644 --- a/pkg/turtle_geometry.js +++ b/pkg/turtle_geometry.js @@ -56,6 +56,12 @@ export function add (v1, v2) { return [x1 + x2, y1 + y2] } +export function sub (v1, v2) { + const [x1, y1] = v1 + const [x2, y2] = v2 + return [x1 - x2, y1 - y2] +} + export function mult (vector, scalar) { const [x, y] = vector return [x * scalar, y * scalar] @@ -92,14 +98,14 @@ export function command_to_state (prev_state, curr_command) { const {heading, position} = prev_state const unit = unit_of(heading) const move = mult(unit, steps) - return {...prev_state, position: add(position, move), clear: false} + return {...prev_state, position: add(position, move), clear: false, forward: true} } case "back": { const [_, steps] = curr_command const {heading, position} = prev_state const unit = unit_of(heading) const move = mult(unit, -steps) - return {...prev_state, position: add(position, move), clear: false} + return {...prev_state, position: add(position, move), clear: false, forward: false} } case "penup": { return {...prev_state, pendown: false, clear: false}