Mast
Mast

Reputation: 1884

Turtle bending lines

In the following code, the second box drawn gets messed up. It's almost like the lines aren't drawn straight up and down but at a slight angle. I've tried to make the sample MCVE.

import turtle, os

turtle.speed(0)
iOneAndHalve = 1.5    # Increasing to 1.501 makes a big difference
Q = 0
iSize = 80

def box(x):
    for i in xrange(x):
        turtle.forward(i+1)
        turtle.left(90)
    global Q
    Q = i

box(iSize)

turtle.up()
turtle.forward(iOneAndHalve*Q)    # <----------
turtle.down()

box(iSize)

os.system("pause")

If I set iOneAndHalve to 1.501 instead of 1.5, the problem is solved. The size of the box (and therefore Q doesn't seem to make a difference).

I have no clue why this does the trick and this smells like a cheap fix.

What's causing this (irratic) behaviour and how should I properly fix this?

Note: I realize the style of the coding is sub-standard. I'm afraid that's the result of MCVE'ing it.

Upvotes: 1

Views: 669

Answers (1)

matsjoyce
matsjoyce

Reputation: 5844

It appears to be a problem with rounding, as replacing turtle.forward(iOneAndHalve*Q) with turtle.forward(round(iOneAndHalve * Q, 0)) fixes the problem.

Upvotes: 2

Related Questions