Merge branch 'main' into release
This commit is contained in:
commit
c0b00356a7
19
pkg/p5.js
19
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(prev)
|
||||||
// console.log(curr)
|
// console.log(curr)
|
||||||
let calls = []
|
let calls = []
|
||||||
if (prev.pendown && !eq_vect(prev.position, curr.position)) {
|
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)) {
|
if (!eq_color(curr.pencolor, prev.pencolor)) {
|
||||||
calls.push(["stroke", ...resolve_color(curr.pencolor)])
|
calls.push(["stroke", ...resolve_color(curr.pencolor)])
|
||||||
|
@ -80,7 +91,7 @@ export function p5 (commands) {
|
||||||
for (let i = 1; i < states.length; ++i) {
|
for (let i = 1; i < states.length; ++i) {
|
||||||
const prev = states[i - 1]
|
const prev = states[i - 1]
|
||||||
const curr = states[i]
|
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) {
|
for (const call of calls) {
|
||||||
// console.log(call)
|
// console.log(call)
|
||||||
if (call === "clear") {
|
if (call === "clear") {
|
||||||
|
|
|
@ -56,6 +56,12 @@ export function add (v1, v2) {
|
||||||
return [x1 + x2, y1 + y2]
|
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) {
|
export function mult (vector, scalar) {
|
||||||
const [x, y] = vector
|
const [x, y] = vector
|
||||||
return [x * scalar, y * scalar]
|
return [x * scalar, y * scalar]
|
||||||
|
@ -92,14 +98,14 @@ export function command_to_state (prev_state, curr_command) {
|
||||||
const {heading, position} = prev_state
|
const {heading, position} = prev_state
|
||||||
const unit = unit_of(heading)
|
const unit = unit_of(heading)
|
||||||
const move = mult(unit, steps)
|
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": {
|
case "back": {
|
||||||
const [_, steps] = curr_command
|
const [_, steps] = curr_command
|
||||||
const {heading, position} = prev_state
|
const {heading, position} = prev_state
|
||||||
const unit = unit_of(heading)
|
const unit = unit_of(heading)
|
||||||
const move = mult(unit, -steps)
|
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": {
|
case "penup": {
|
||||||
return {...prev_state, pendown: false, clear: false}
|
return {...prev_state, pendown: false, clear: false}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user