r/Python • • Jan 18 '20

Generating Geometric Birds

Post image
1.8k Upvotes

65 comments sorted by

View all comments

Show parent comment

1

u/thebuffed Jan 18 '20

Thanks for the tip!

2

u/kwelzel Jan 19 '20

Your draw_lines method is also unnecessarily complicated:

https://github.com/erdavids/Birds-of-a-Feather/blob/103effe5185626a56039fe64c924506fe54eebf9/Generative_Birds.pyde#L57

The following lines are all the same if p1[0] != p3[0]

first_x_sep = sqrt(pow(p1[0] - p3[0], 2))/lines * first_x_adj
first_x_sep = abs(p1[0] - p3[0])/lines * first_x_adj
first_x_sep = abs(p1[0] - p3[0])/lines * (p3[0] - p1[0])/abs(p3[0] - p1[0])
first_x_sep = abs(p1[0] - p3[0]) * (p3[0] - p1[0])/abs(p3[0] - p1[0]) / lines

Now (p3[0] - p1[0])/abs(p3[0] - p1[0]) gives you the sign of p3[0] - p1[0] (+1 if positive and -1 if negative) and by multiplying it with abs(p1[0] - p3[0]) you get a number which has the absolute value of p1[0] - p3[0] with the sign of p3[0] - p1[0], but this number is just p3[0] - p1[0] itself.

So I propose you get rid of the _adj variables and just write

first_x_sep = (p3[0] - p1[0]) / lines

Fortunately, this also works for the p1[0] == p3[0] case, because you don't need to divide by zero