From dfb5b90e557dd8f3e703ce26a451e009b02159e4 Mon Sep 17 00:00:00 2001 From: fmOOmf <98671961+fmOOmf@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:43:33 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Modif=20pour=20le=20circle=20avec=20angle?= =?UTF-8?q?=20n=C3=A9gatif=20(modif=20for=20circle=20with=20negative=20ang?= =?UTF-8?q?le)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/port/mod/turtle/turtle.cpp | 52 +++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index abba04f4605..b3568db7090 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -74,19 +74,51 @@ void Turtle::left(mp_float_t angle) { void Turtle::circle(mp_int_t radius, mp_float_t angle) { mp_float_t oldHeading = heading(); mp_float_t length = std::fabs(angle * k_headingScale * radius); + /* if (length > 1) { - for (int i = 1; i < length; i++) { - mp_float_t progress = i / length; - // Move the turtle forward - if (forward(1)) { - // Keyboard interruption. Return now to let MicroPython process it. - return; + if (angle > 0) { + for (int i = 1; i < length; i++) { + mp_float_t progress = i / length; + // Move the turtle forward + if (forward(1)) { + // Keyboard interruption. Return now to let MicroPython process it. + return; + } + setHeadingPrivate(oldHeading+std::copysign(angle*progress, radius)); } - setHeadingPrivate(oldHeading+std::copysign(angle*progress, radius)); - } - forward(1); - setHeading(oldHeading+angle); + forward(1); + setHeading(oldHeading+angle); + } else { + for (int i = 1; i < length; i++) { + mp_float_t progress = i / length; + // Move the turtle backward + if (forward(-1)) { + // Keyboard interruption. Return now to let MicroPython process it. + return; + } + setHeadingPrivate(oldHeading-std::copysign(angle*progress, radius)); + } + forward(-1); + setHeading(oldHeading+angle); + } } + */ + int direction = 1; // 1=fwd, -1=backward + if (angle < 0) {direction =-1;} + if (length > 1) { + for (int i = 1; i < length; i++) { + mp_float_t progress = i / length; + // Move the turtle forward + if (forward(direction)) { + // Keyboard interruption. Return now to let MicroPython process it. + return; + } + setHeadingPrivate(oldHeading+std::copysign(angle*progress, direction*radius)); + } + forward(direction); + setHeading(oldHeading+angle); + } + } bool Turtle::goTo(mp_float_t x, mp_float_t y) { From 57e4cbe402602ec13870a3a61a803d52d85e03e9 Mon Sep 17 00:00:00 2001 From: fmOOmf <98671961+fmOOmf@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:22:56 +0200 Subject: [PATCH 2/4] Final Heading correction --- python/port/mod/turtle/turtle.cpp | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index b3568db7090..00568492b0c 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -74,35 +74,6 @@ void Turtle::left(mp_float_t angle) { void Turtle::circle(mp_int_t radius, mp_float_t angle) { mp_float_t oldHeading = heading(); mp_float_t length = std::fabs(angle * k_headingScale * radius); - /* - if (length > 1) { - if (angle > 0) { - for (int i = 1; i < length; i++) { - mp_float_t progress = i / length; - // Move the turtle forward - if (forward(1)) { - // Keyboard interruption. Return now to let MicroPython process it. - return; - } - setHeadingPrivate(oldHeading+std::copysign(angle*progress, radius)); - } - forward(1); - setHeading(oldHeading+angle); - } else { - for (int i = 1; i < length; i++) { - mp_float_t progress = i / length; - // Move the turtle backward - if (forward(-1)) { - // Keyboard interruption. Return now to let MicroPython process it. - return; - } - setHeadingPrivate(oldHeading-std::copysign(angle*progress, radius)); - } - forward(-1); - setHeading(oldHeading+angle); - } - } - */ int direction = 1; // 1=fwd, -1=backward if (angle < 0) {direction =-1;} if (length > 1) { @@ -116,7 +87,7 @@ void Turtle::circle(mp_int_t radius, mp_float_t angle) { setHeadingPrivate(oldHeading+std::copysign(angle*progress, direction*radius)); } forward(direction); - setHeading(oldHeading+angle); + setHeading(oldHeading+std::copysign(angle, direction*radius)); } } From 56ce3b0f42059b8cff5a2362ad89301702cd7ae0 Mon Sep 17 00:00:00 2001 From: fmOOmf <98671961+fmOOmf@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:10:15 +0200 Subject: [PATCH 3/4] Review changes (indentation, spaces) --- python/port/mod/turtle/turtle.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index 00568492b0c..e5bb0c39dc5 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -75,20 +75,20 @@ void Turtle::circle(mp_int_t radius, mp_float_t angle) { mp_float_t oldHeading = heading(); mp_float_t length = std::fabs(angle * k_headingScale * radius); int direction = 1; // 1=fwd, -1=backward - if (angle < 0) {direction =-1;} - if (length > 1) { - for (int i = 1; i < length; i++) { - mp_float_t progress = i / length; - // Move the turtle forward - if (forward(direction)) { - // Keyboard interruption. Return now to let MicroPython process it. - return; - } - setHeadingPrivate(oldHeading+std::copysign(angle*progress, direction*radius)); + if (angle < 0) { direction =-1; } + if (length > 1) { + for (int i = 1; i < length; i++) { + mp_float_t progress = i / length; + // Move the turtle forward + if (forward(direction)) { + // Keyboard interruption. Return now to let MicroPython process it. + return; } - forward(direction); - setHeading(oldHeading+std::copysign(angle, direction*radius)); + setHeadingPrivate(oldHeading+std::copysign(angle*progress, direction*radius)); } + forward(direction); + setHeading(oldHeading+std::copysign(angle, direction*radius)); + } } From ea89bc9aa5e6ff17ea72ff13847f04d95cde9d31 Mon Sep 17 00:00:00 2001 From: fmOOmf <98671961+fmOOmf@users.noreply.github.com> Date: Thu, 27 Oct 2022 11:26:01 +0200 Subject: [PATCH 4/4] code comment Co-authored-by: Yaya-Cout --- python/port/mod/turtle/turtle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index e5bb0c39dc5..b7d030fc01a 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -74,7 +74,7 @@ void Turtle::left(mp_float_t angle) { void Turtle::circle(mp_int_t radius, mp_float_t angle) { mp_float_t oldHeading = heading(); mp_float_t length = std::fabs(angle * k_headingScale * radius); - int direction = 1; // 1=fwd, -1=backward + int direction = 1; // 1=forward, -1=backward if (angle < 0) { direction =-1; } if (length > 1) { for (int i = 1; i < length; i++) {