add setpos() command

This commit is contained in:
lee2sman 2021-02-07 03:14:11 -05:00
parent 02a08761da
commit 40fb446348
1 changed files with 18 additions and 0 deletions

View File

@ -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);