From 40fb446348c29e23fb3511c9c48e19a3e29a9314 Mon Sep 17 00:00:00 2001 From: lee2sman Date: Sun, 7 Feb 2021 03:14:11 -0500 Subject: [PATCH] add setpos() command --- lib/plogo.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/plogo.js b/lib/plogo.js index ff2c70d..583f5d8 100644 --- a/lib/plogo.js +++ b/lib/plogo.js @@ -17,6 +17,24 @@ function resetDefaults() { x = width / 2; //start turtle in center y = height / 2; } +function setpos(_newX, _newY, startx = x, starty = y) { + let newX = _newX; + let newY = _newY; + + if (drawing) { + line(startx, starty, newX, newY); + } + x = newX; + y = newY; + + try { + if ((_newX == "") || (_newY == "")) throw "is missing input x,y coordinates"; + if (isNaN(_newX) || (isNaN(_newY))) throw "requires: x,y input coordinates"; + if ((_newX < 0) || (_newY < 0) || (_newX > windowWidth) || (_newY > windowHeight)) throw "drawing offscreen"; + } catch (err) { + console.log("Error: setpos " + err); + } +} function forward(d, startx = x, starty = y, _angle = 360 - angle) { let newX = startx + d * sin(_angle); let newY = starty + d * cos(_angle);