henry
henry

Reputation: 965

Making a square spiral

I am trying to make a square spiral.

Here is a Python code:

import matplotlib.pyplot as plt

g = 5 #step
w = 0 #width of spiral

point_list_x = []
point_list_y = []
for n in range(1,4):

    # The math part
    p1 = [n*(g+w),-(g+w)*n]
    p2 = [n*(g+w), (g+w)*n]
    p3 = [-(g+w)*n,(g+w)*n]
    p4 = [-(g+w)*n,-2*(g+w)*n]

    # Just collecting the points    
    point_list_x.append(p1[0])
    point_list_x.append(p2[0])
    point_list_x.append(p3[0])
    point_list_x.append(p4[0])

    point_list_y.append(p1[1])
    point_list_y.append(p2[1])
    point_list_y.append(p3[1])
    point_list_y.append(p4[1])

# Just plotting           
plt.scatter(point_list_x, point_list_y)
plt.plot(point_list_x, point_list_y)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()

but I get:

enter image description here

Where is my mistake? It's mostly a math issue.

Upvotes: 1

Views: 723

Answers (1)

Luke Andrews
Luke Andrews

Reputation: 61

It looks like the problem is in your calculation of p4. You've made it so that it increases at a larger rate than any of the others by putting that 2* in front of it. To fix the problem, the y ordinate of p4 should become -(g+w)*(n+1) as this point is the start of the next loop so n should go up.

Upvotes: 2

Related Questions