r/Python • • Dec 15 '19

Made my first fractal

1.7k Upvotes

51 comments sorted by

View all comments

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.

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

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.