简体   繁体   中英

How can i make coordinate drawing spiral

I'd like to make bunch of coordinate on spiral line with python code or javascript. like this one Archimedean_spiral

Could anyone tell me how to do that?

Here you go

import math

# Define variables and create the grid
a = 0
b = 2
rounds = 5
# Size of the grid
y_max, x_max = 100, 100
# Center of the grid
origo_y, origo_x = 50, 50

# Every element in the grid is truly unique element
# If the grid is created like this
# Don't use for example [[" "]*x_max]*y_max
grid = [[" " for i in range(x_max)] for j in range(y_max)]

for angle in range(rounds*360):
    # Calculations for the spiral
    rads = math.radians(angle)
    r = a + b * rads
    y = r * math.sin(rads)
    x = r * math.cos(rads)
    x_coord = origo_x + round(x)
    y_coord = origo_y + round(y)

    if (0 <= x_coord < x_max) and (0 <= y_coord < y_max):
        grid[y_coord][x_coord] = "#"

# Print the whole grid
for line in grid:
    print("".join(line))

I don't know if this site is for asking questions broad as your's, but as a coding problem this was quite interesting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM