mirror of
https://github.com/remko/waforth
synced 2025-01-13 08:01:32 +01:00
thurtle: Add SETXY & SETROTATION
This commit is contained in:
parent
47b83fc610
commit
9ec19dbc15
3 changed files with 40 additions and 3 deletions
|
@ -87,10 +87,11 @@ export default [
|
|||
: SPIRAL ( n -- )
|
||||
DUP 1 < IF DROP EXIT THEN
|
||||
DUP FORWARD
|
||||
20 RIGHT
|
||||
95 100 */ RECURSE
|
||||
15 RIGHT
|
||||
98 100 */ RECURSE
|
||||
;
|
||||
|
||||
PENUP -500 -180 SETXY PENDOWN
|
||||
140 SPIRAL
|
||||
`,
|
||||
},
|
||||
|
|
|
@ -6,4 +6,6 @@
|
|||
: PENUP ( -- ) 0 S" pen" SCALL ;
|
||||
: HIDETURTLE ( -- ) 0 S" turtle" SCALL ;
|
||||
: SHOWTURTLE ( -- ) 1 S" turtle" SCALL ;
|
||||
: SETPENSIZE ( n -- ) S" setpensize" SCALL ;
|
||||
: SETPENSIZE ( n -- ) S" setpensize" SCALL ;
|
||||
: SETXY ( n1 n2 -- ) S" setxy" SCALL ;
|
||||
: SETHEADING ( n -- ) S" setheading" SCALL ;
|
|
@ -154,6 +154,14 @@ const rootEl = (
|
|||
<code>RIGHT ( n -- )</code>: Turn right by <code>n</code>{" "}
|
||||
degrees.
|
||||
</li>
|
||||
<li>
|
||||
<code>SETXY ( n1 n2 -- )</code>: Move to position{" "}
|
||||
<code>n1,n2</code>.
|
||||
</li>
|
||||
<li>
|
||||
<code>SETHEADING ( n -- )</code>: Set heading <code>n</code>{" "}
|
||||
degrees clockwise from Y axis.
|
||||
</li>
|
||||
<li>
|
||||
<code>PENUP ( -- )</code>: Disable drawing while moving.
|
||||
</li>
|
||||
|
@ -243,6 +251,11 @@ function rotate(deg: number) {
|
|||
updateTurtle();
|
||||
}
|
||||
|
||||
function setRotation(deg: number) {
|
||||
rotation = deg;
|
||||
updateTurtle();
|
||||
}
|
||||
|
||||
function forward(d: number) {
|
||||
const dx = d * Math.cos((rotation * Math.PI) / 180.0);
|
||||
const dy = d * Math.sin((rotation * Math.PI) / 180.0);
|
||||
|
@ -258,6 +271,19 @@ function forward(d: number) {
|
|||
updateTurtle();
|
||||
}
|
||||
|
||||
function setXY(x: number, y: number) {
|
||||
pathEl.setAttribute(
|
||||
"d",
|
||||
pathEl.getAttribute("d")! +
|
||||
" " +
|
||||
[pen === PenState.Down ? "l" : "M", x, y].join(" ")
|
||||
);
|
||||
|
||||
position.x = x;
|
||||
position.y = y;
|
||||
updateTurtle();
|
||||
}
|
||||
|
||||
function setPen(s: PenState) {
|
||||
pen = s;
|
||||
}
|
||||
|
@ -306,6 +332,14 @@ async function run() {
|
|||
forth.bind("setpensize", (stack) => {
|
||||
setPenSize(stack.pop());
|
||||
});
|
||||
forth.bind("setxy", (stack) => {
|
||||
const y = stack.pop();
|
||||
const x = stack.pop();
|
||||
setXY(x, -y);
|
||||
});
|
||||
forth.bind("setheading", (stack) => {
|
||||
setRotation(-90 - stack.pop());
|
||||
});
|
||||
forth.interpret(thurtleFS);
|
||||
forth.onEmit = (c) => outputEl.appendChild(document.createTextNode(c));
|
||||
forth.interpret(programEl.value);
|
||||
|
|
Loading…
Reference in a new issue