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; }