r/Python • • Dec 15 '19

Made my first fractal

1.7k Upvotes

51 comments sorted by

35

u/Gawd_is_great Dec 15 '19

2

u/MartyMacGyver from * import * Dec 16 '19

The gif on github (and the code that generates it) is different than the gif here with the blue lines.... why is that?

3

u/Gawd_is_great Dec 16 '19

The trails are disabled in the gif on github

0

u/MartyMacGyver from * import * Dec 16 '19

How does one enable them?

3

u/Gawd_is_great Dec 16 '19

They are enabled in the code . If you want to disable them then comment out maketrails() and drawtrails() functions

0

u/MartyMacGyver from * import * Dec 16 '19

Those aren't in your current code on GitHub...

3

u/Gawd_is_great Dec 16 '19

Oh.. I just noticed. I'll update the code soon

3

u/Gawd_is_great Dec 16 '19

updated the code

1

u/MartyMacGyver from * import * Dec 17 '19

Still not the same code, and it crashes at the end... but it has trails (it has nothing BUT trails) so there's that.

2

u/Gawd_is_great Dec 17 '19

Okay it's Perfectly fixed now. Doesn't crash and shows Everything

1

u/MartyMacGyver from * import * Dec 17 '19

Shows everything, but it does still crash in the end (python 3.6-3.8 in Windows):

File "Fractal\fractal.py", line 90, in <module>
    makeTrail(objects,color)
File "Fractal\fractal.py", line 44, in makeTrail
    del trail
UnboundLocalError: local variable 'trail' referenced before assignment

This is probably because the last call to makeTrail() before exit has an empty list of objects.

There's no real need for del trail there... Python's ref counter + garbage collection should deal with that. Likewise I'd get rid of del object too.

At the very end, does it delete all the white dots for you? I'm curious why that happens.

→ More replies (0)

1

u/Gawd_is_great Dec 17 '19

Uncomment the line #draw(screen)

1

u/[deleted] Dec 15 '19

!Remindme 20 h

1

u/RemindMeBot Dec 16 '19 edited Dec 16 '19

I will be messaging you in 6 hours on 2019-12-16 18:37:52 UTC to remind you of this link

2 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

100

u/Namensplatzhalter Dec 15 '19 edited Dec 15 '19

Looks nice.

I've just had a look on your code and have to say it looks nicely organised and is easily readable. Kudos to you for writing it like that.

Two little suggestions, if you don't mind:

  • Consider using a if __name__ == "__main__" block to initialise your script when it's run directly

  • Consider using tuples for your vel_list vel_table as they are immutable

Both of those definitely not necessary, just things to think about. :)

Best wishes

26

u/Gawd_is_great Dec 15 '19

Thanks for the suggestions =) I have a couple of questions:

  1. Why should I use if(__name__=="main") ? It wont make a difference..will it?

  2. is there a better way than to use vel_list?

50

u/Namensplatzhalter Dec 15 '19
  1. if __name__ == "__main__" is just a best practice for coding in python. not necessary for such simple apps but good to get in the habit of using it.

  2. Using vel_table as a mapper for the direction of the new points is perfectly fine. Is was just suggesting using tuples instead of lists because tuples are immutable and your directions are a fixed set (i.e. 8 directions)

8

u/Gawd_is_great Dec 15 '19

Oh ok Regarding the vel_table I was wondering if there was a way to do it mathematically. In my code I split the points at 45 degree. But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.

3

u/jespernielsen1982 Dec 15 '19

You can do it using the trigonometric functions "sine" and "cosine". It's not exactly hard, but it is more math than I want to be explaining on my phone.

1

u/Gawd_is_great Dec 15 '19

Ah yes okay thanks. I'll see into it. Thanks for guiding me onto the right path.

18

u/Endemoniada Dec 15 '19

Just to clarify on #1: it’s best practice because there’s two ways to execute code in a python file, either by importing its functions or classes, or by executing the entire file. The thing is, even when you import it, it executes all the code that is not specifically written inside a function or class, which can be a problem when all you expect is to be silently importing something from it.

Hence, by wrapping all active code in the script inside a function and then calling the execution of that function only when the code is run as a script, not being imported, you make it usable in both instances without surprise output or variables.

9

u/Namensplatzhalter Dec 15 '19

But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.

You could for example ask for a user input in the beginning where the user provides a number between 1 and 45. This sets the degree at which the splits should occur.

For your code to work as it is, you would always have to make sure that the degree value don't leave a residue value (e.g. 13° wouldn't work because after 27 splits you'd have a residue of 9). This could be achieved by using the % operator in python, e.g. check for something like 360%input == 0. Go ahead and read up on it if you don't know it already.

If this check returns true, you can then use the user input to determine the amount of directions you need to provide for your script by running 360/input.

Maybe this will get you on the way of what you need to do. :) Best of luck

2

u/djimbob Dec 16 '19 edited Dec 17 '19

I'm not saying you need it for this, but they didn't really explain the purpose of the clause if __name__ == 'main' clause.

Looking at your script lines 1-56 seem to just define classes and functions, and then lines 57 and beyond seem to be doing things (and executing the program).

Its good software design to refactor and have the first section defining everything, then the run part of the program in a function

# define stuff above
def draw_fractal(max_splits = 2**7, size=(700,700)):
    vel_table = [[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1]]
    [...]

if __name__ == '__main__':
    draw_fractal()

When you run your code python fractal.py it works the same. But now you can open the interpreter up and import fractal without actually popping up a window to draw anything. Then you could maybe call draw_fractal(max_splits=256) or call from another program. Or maybe you decide to create fractal2.py with a Dot2 class that inherits from Dot and is brought in with from fractal import Dot. You wouldn't want that import to start drawing the first fractal. By having 'executing' code (not just function/class/variable definitions) at the top level auto-run, you make code-reuse impossible.

E.g.,

$ ipython

In [1]: from fractal import Dot
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
8

At which point a window pops up and starts drawing.

Also, I'm not sure why you have del object and del trail at the ends of create and makeTrail as they don't actually do anything (no memory is freed up or anything). And in makeTrail, when objects is an empty list it creates an UnboundLocalError when you try deleting trail.

The code del some_var just unsets some_var in memory (and if there are no other references to the memory referenced by some_var it lets the gc give back the allocated memory to the OS on its next collection; though in your case their are other references -- you add the objects to list) and would make it an error to try and reuse some_var. However, all local variables are lost when a function exits (and it would be illegal to use them) -- there's no reason to explicitly delete a local variable at the end of a function.

1

u/Gawd_is_great Dec 16 '19

Oh wow. Thanks a lot for the suggestions. It's amazing that people like you are willing to share knowledge with others =) Really appreciate it =)

3

u/graingert Dec 16 '19 edited Dec 16 '19

It's even better to use:

def main():
    ...

if __name__ == "__main__":
    main()

Then you can use module.main as a setuptools entry point

1

u/MB1211 Dec 15 '19

I didn't look at your code but the way it would make a difference is if you wanted to import your module, it will essentially run the module when you import. If you have the if name = main part, that part won't run because the module is not main when it's imported

2

u/ned334 Dec 15 '19

Where did you look at his code?

3

u/Namensplatzhalter Dec 15 '19

He posted a github link in the comments.

2

u/ned334 Dec 15 '19

Oh. Didn't see it

17

u/sharkbound github: sharkbound, python := 3.8 Dec 15 '19

in Dot.__init__ you are using lists as default values, having mutable default value is very bad and can become a bug very quickly, since functions are objects too, the argument default will all be the same reference each __init__ call

so because of this, when you do anything to mutate the default argument, it will affect all the places where the default argument value is used

its much better to use a tuple or None or some other immutable value for a default value check do a check in the init

code here: https://gist.github.com/785534176eca51545ce7d6cbbbb18e62

console output here: https://gist.github.com/50fbc58ed04fc1ac8089d3497b80c5f1

EDIT 0:

also spotted a name inconsistancy for function updatePos in Dot, everything except types/classes should use snake_case, update_pos

2

u/oetker Dec 16 '19

Nice, thank you for illustrating this!

1

u/otterom Dec 16 '19

Pretty sweet! Ive never used pygame, so it's always fun to see some of the techniques used to make it run.

Unrelated: Does that maze repo generate a solvable maze each time?

2

u/Gawd_is_great Dec 16 '19

Yep it does. Someone here solved it with a maze solver algorithm

3

u/vnglst Dec 15 '19

lovely!

3

u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Dec 15 '19

4

u/[deleted] Dec 15 '19

woat u use it for

2

u/otterom Dec 16 '19

Finding order in apparent chaos.

1

u/Teleswagz Dec 15 '19

Nice you made it look like a brain

1

u/[deleted] Dec 16 '19

What is this used for?

2

u/MrK_HS Dec 17 '19

Getting internet points on Reddit

1

u/bikinakarmi Dec 16 '19

Can I have a code

0

u/Iam_That_Iam_ Dec 15 '19

Good start , you beat the Hello World starters

0

u/MrK_HS Dec 17 '19

/r/Python is slowly becoming an alias for /r/fractals and /r/mandelbrot

0

u/[deleted] Dec 15 '19

[deleted]

7

u/Gawd_is_great Dec 15 '19

Just upload the gif to your repo and in the README.md file put this line:

alt text

Alt text - the text you want to display in place of the image of it fails to laid

Link to image - Well...link to image in repo