Hi i've been assigned with doing a torn square, and i've kinda got it working with the code below.

import turtle
turtle.reset()
length=100
degree=60
i=0
while i < 4:
    turtle.forward(length/length*40)
    turtle.right(degree)
    turtle.forward(length/2)
    turtle.left(degree+degree)
    turtle.forward(length/2)
    turtle.right(degree)
    turtle.forward(length/length*40)
    i+=1
    turtle.right(90)
else:
    raw_input("there's hopefully a square, all torn like!")

as you can see i can change the degree so the torn square looks different but i know i'm supposed to do some math equations to work out the degree of the torn length.

pythagorean theorem is what i need. (** is to the power of isnt it?) a**2 + b**2 = c**2 right?

So my length for

a=40
b=10
.....(all the other code)
turtle.forward(a**2+b**2)

but that sends it really really far, do i need to square route the result ?

sorry if i yabbered i tried to use this as my thought pad, still trying to work it out while i'm typing this :P

You can do (a**2+b**2)**0.5 do get the proper result.

excellent that worked a treat,

it now draws a nice square wth 4 rips in it.

I would however like to keep this thread open because i want to now alter how many times it gets "torn" before it goes back to the original line.

http://mathworld.wolfram.com/images/eps-gif/TornSquare_620.gif

i'll of course attempt this and get back with any problems and or success!,
By looking at it, it seems i'll just have to fill in more turtle.direction(),
there's likly an easier way to simplify with "if" statements but i'll try to shorten bits down afterwards, unless you've any handy tips now.

Thanks again!
Matt

okay this is what it's turning out like on the graphical front.http://img.photobucket.com/albums/v617/thehivetyrant/TornSquareiteration2.jpg
Dont worry about the tears not going far enough i can fix that.
Here's what i need some advice on:

I have had to write the same piece of code several times for different parts of the if statement.(see below)

if repetition == 4:
        turtle.right(90)
    if repetition == 7:
        turtle.right(90)
    if repetition == 10:
        turtle.right(90)

and
Done let the code below blind you it IS all the same for 4 if statements

if repetition == 2:
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1
    if repetition == 5:
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1
    if repetition == 8:
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1
    if repetition == 11:
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.left(degree+degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward(((a**2+b**2)**0.5)/2)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1

Is there anyway i could shorten these? possibly write the instructions to a string(i think thats the right one) and call if up repeatedly.

or something like: if repetition == 2 or 5 or 8 or 11 ?
so i could use several if's with the same instructions in one section?\

help would be appreciated.

Also you should compute only once ((a**2+b**2)**0.5)/2 and store the value in a variable.

Exellent, thats exactly the stuff i was looking for!

I'll slip them in and change the code to shorten it down a ton.

Thanks guys!!

Also you should compute only once ((a**2+b**2)**0.5)/2 and store the value in a variable.

hmm to do that is it? def halfdiagline(((a*[U]*[/U]2+b**2)**0.5)/2) is that correct, or did i miss something cause i tried it and it didnt work,
The error it reports highlights this underlined star section.
i could have placed it in the wrong section?

Cheers me Dears!

No, I meant that you could write

x = ((a**2 + b**2)**0.5)/2
    if repetition in (2, 5, 8, 11):
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(x)
        turtle.right(degree)
        turtle.forward(x)
        turtle.left(degree+degree)
        etc...

This avoids computing repeatidly the same expression.

Thanks for all the help chums!

I've finished that particular torn square now and am happy on how it turned out. No doubt i could reduce it further but as i said i am happy.

import turtle
turtle.reset()
turtle.tracer(0)
length=250
degree=80
pythag=(a**2+b**2)**0.5
i=0
a=45
b=40
repetition= 1
while i < 12:
    if repetition in (4, 7, 10):
        turtle.right(90)
    if repetition in (2, 5, 8, 11):
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.left(degree+degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.left(degree+degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.left(degree+degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1
    else:
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(pythag)
        turtle.left(degree+degree)
        turtle.forward(pythag)
        turtle.right(degree)
        turtle.forward((length/4)-5)
        i+=1
        repetition+=1
else:
    raw_input("there's hopefully a square, all torn like!")

Again i would like to convey my appreciation for the help given from the members!

Thanks Matt!

commented: nice drawing +2

Not to be a blanket-wetter, but I noticed you have pythag=(a**2+b**2)**0.5 before the statements that give values to a and b. The program as presented will not run without minor edits.

Still, it looks much nicer than the original code!

I have moved the pythag bit below the a=45 b=10 bit.

You were correct that it didnt run as is before i did that.
It now runs as is.
But as knowledge goes i haven't a clue on why that is.

For the sake of the learning experience could you tell me why it didnt work before i moved the pythag below the a= and b= bit?

Thanks again.
Matt

... could you tell me why it didnt work before i moved the pythag below the a= and b= bit?

Because a and b have no value when you go to calculate pythag.

The statement pythag=(a**2+b**2)**0.5 is, after all, just a bunch of arithmetic operations resulting in a value that is stored in pythag. It specifically does NOT create some sort of magical relationship where pythag changes value when a and b change value. It's really no different from this:

a = 3
b = 4
pythag = a + b # Something a little simpler, but fundamentally the same
a = 12
b = 5
print pythag # produces 7, the last value stored in pythag

Oh i understand now,
that example was very useful as well.

Thanks for the help once more.

Consider this a solved post!

Thanks Matt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.