2 Replies - 1447 Views - Last Post: 07 November 2014 - 05:04 PM Rate Topic: ****- 2 Votes

#1 eliserxs   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-November 14

Python Fractal Programming

Posted 07 November 2014 - 04:03 PM

I am making a fractal with python code and turtles. How can I make this code more efficient?
def cross(side, x, y):  #makes the cross everytime
    for i in range(0, 4):
        thor.left(90)
        thor.forward(side)
        thor.left(180)
        thor.forward(side)

import turtle

wn = turtle.Screen()
wn.bgcolor("black")

thor = turtle.Turtle()
thor.pensize(5)
thor.speed(0)
thor.color("darkblue")

halfSide = 250
sideLength = 500  #length of the biggest square

x = -250
y = 250

thor.penup()  #positions the cursor so the square will be centered
thor.left(90)
thor.forward(halfSide)
thor.left(90)
thor.forward(halfSide)
thor.left(90)
thor.pendown()
thor.forward(sideLength)  #makes the biggest square only once
thor.left(90)
thor.forward(sideLength)
thor.left(90)
thor.forward(sideLength)
thor.left(90)
thor.forward(sideLength)

thor.left(90)    #repositions the cross to the center once it's done one cross
thor.penup()
thor.setpos(0, 0)
thor.pendown()

cross(250, 0, 0)  #length of cross side, x and y coordinates

for i in range(0, 4):  #creates second set of crosses
    thor.pensize(4)
    thor.color("RoyalBlue")
    thor.forward(halfSide/2)
    thor.left(90)
    thor.forward(halfSide/2)
    cross(halfSide/2, x+halfSide/2, y+halfSide/2)
    thor.penup()
    thor.setpos(0, 0)
    thor.pendown()

halfSide=halfSide/2

for j in range(0, 16):  #creates 3rd set of crosses
    thor.pensize(3)
    thor.color("lightblue")
    thor.penup()
    if j < 4:  #to reposition the crosses
        thor.setpos(125, 125)
    elif j < 8:
        thor.setpos(-125, 125)
    elif j < 12:
        thor.setpos(-125, -125)
    else:
        thor.setpos(125, -125)
    thor.pendown()
    thor.forward(halfSide/2)
    thor.left(90)
    thor.forward(halfSide/2)
    cross(halfSide/2, x+halfSide/2, y+halfSide/2)

for j in range(0, 32):  #creates 4th set of crosses
    thor.pensize(2)
    thor.color("azure")
    thor.penup()
    if j < 4:  #to reposition the crosses
        thor.setpos(62.5, 62.5)
    elif j < 8:
        thor.setpos(187.5, 62.5)
    elif j < 12:
        thor.setpos(187.5, 187.5)
    elif j < 16:
        thor.setpos(62.5, 187.5)
    elif j < 20:  #to reposition the crosses
        thor.setpos(-187.5, 62.5)
    elif j < 24:
        thor.setpos(-62.5, 62.5)
    elif j < 28:
        thor.setpos(-62.5, 187.5)
    elif j < 32:  #to reposition the crosses
        thor.setpos(-187.5, 187.5)



    thor.pendown()
    thor.forward(halfSide/4)
    thor.left(90)
    thor.forward(halfSide/4)
    cross(halfSide/4, x+halfSide/4, y+halfSide/4)

wn.exitonclick()


This post has been edited by macosxnerd101: 07 November 2014 - 04:04 PM
Reason for edit:: Please remember to use code tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Python Fractal Programming

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Python Fractal Programming

Posted 07 November 2014 - 04:30 PM

In what way do you think the code is inefficient? Or perhaps you mean just to reduce the number of code lines?

Reducing code lines is not a particularly fruitful exercise. However, it is useful to look for repeating code, or repeating patterns of code, and then make a decision as to whether it is worthwhile to factor these into a function. Moving such code to a function can (but not always) make the code easier to read, and easier to modify later.
Was This Post Helpful? 0
  • +
  • -

#3 eliserxs   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-November 14

Re: Python Fractal Programming

Posted 07 November 2014 - 05:04 PM

yes i do mean reducing the code, there are parts that are repeated namely the cross but i do not know how to reposition it without specifically setting the position every time.

This post has been edited by andrewsw: 08 November 2014 - 02:19 AM
Reason for edit:: Removed unnecessary previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1