From a25b2a8ce5fe8d94183de6ccfaf620c301f06568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 5 Feb 2019 16:54:50 +0100 Subject: [PATCH] [python/turtle] Fix unwanted dotted lines Script: fd(200) left(90) fd(10) left(90) fd(600) The final line should be continuous but is dotted --- python/port/mod/turtle/turtle.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index 3a3499750..f7cd49c5c 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -76,16 +76,23 @@ void Turtle::circle(mp_int_t radius, mp_float_t angle) { bool Turtle::goTo(mp_float_t x, mp_float_t y) { mp_float_t oldx = m_x; mp_float_t oldy = m_y; - mp_float_t length = maxF(absF(floor(x) - floor(oldx)), absF(floor(y) - floor(oldy))); + mp_float_t xLength = absF(floor(x) - floor(oldx)); + mp_float_t yLength = absF(floor(y) - floor(oldy)); + bool principalDirectionIsX = xLength >= yLength; + mp_float_t length = principalDirectionIsX ? xLength : yLength; if (length > 1) { // Tweening function for (int i = 1; i < length; i++) { mp_float_t progress = i / length; erase(); - if (dot(x * progress + oldx * (1 - progress), y * progress + oldy * (1 - progress)) - || draw(false)) - { + /* We make sure that each pixel along the principal direction is drawn. If + * the computation of the position on the principal coordinate is done + * using a barycenter, roundings might skip some pixels, which results in + * a dotted line. */ + mp_float_t currentX = principalDirectionIsX ? oldx + (x > oldx ? i : -i) : x * progress + oldx * (1 - progress); + mp_float_t currentY = principalDirectionIsX ? y * progress + oldy * (1 - progress) : oldy + (y > oldy ? i : -i); + if (dot(currentX, currentY) || draw(false)) { // Keyboard interruption. Return now to let MicroPython process it. return true; }