python - algorithm to create a circle in a 2D array EDIT: diamond would be okay aswel -
i have 2d in python represents tile map, each element in array either 1 or 0, 0 representing land , 1 representing water. need algorithm takes 2 random coordinates center of circle, variable radius (max 5) , replace necessary elements in array form full circle.
x = random.randint(0,mapwidth) y = random.randint(0,mapheight) rad = random.randint(0,5) tilemap[x][y] = 1 #this creates center of circle
how this?
as said, can use definition of circle, so:
import math def dist(x1, y1, x2, y2): return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) def make_circle(tiles, cx, cy, r): x in range(cx - r, cx + r): y in range(cy - r, cy + r): if dist(cx, cy, x, y) <= r: tiles[x][y] = 1 width = 50 height = 50 cx = width // 2 cy = height // 2 r = 23 tiles = [[0 _ in range(height)] _ in range(width)] make_circle(tiles, cx, cy, r) print("\n".join("".join(map(str, i)) in tiles))
this outputs
00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000001000000000000000000000000 00000000000000000001111111111111000000000000000000 00000000000000001111111111111111111000000000000000 00000000000000111111111111111111111110000000000000 00000000000001111111111111111111111111000000000000 00000000000111111111111111111111111111110000000000 00000000001111111111111111111111111111111000000000 00000000011111111111111111111111111111111100000000 00000000111111111111111111111111111111111110000000 00000001111111111111111111111111111111111111000000 00000001111111111111111111111111111111111111000000 00000011111111111111111111111111111111111111100000 00000111111111111111111111111111111111111111110000 00000111111111111111111111111111111111111111110000 00001111111111111111111111111111111111111111111000 00001111111111111111111111111111111111111111111000 00001111111111111111111111111111111111111111111000 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00111111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00011111111111111111111111111111111111111111111100 00001111111111111111111111111111111111111111111000 00001111111111111111111111111111111111111111111000 00001111111111111111111111111111111111111111111000 00000111111111111111111111111111111111111111110000 00000111111111111111111111111111111111111111110000 00000011111111111111111111111111111111111111100000 00000001111111111111111111111111111111111111000000 00000001111111111111111111111111111111111111000000 00000000111111111111111111111111111111111110000000 00000000011111111111111111111111111111111100000000 00000000001111111111111111111111111111111000000000 00000000000111111111111111111111111111110000000000 00000000000001111111111111111111111111000000000000 00000000000000111111111111111111111110000000000000 00000000000000001111111111111111111000000000000000 00000000000000000001111111111111000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000
note deliberately used rather large array , radius - results in being able see circle bit better. radius around 5, pixelated beyond belief.
Comments
Post a Comment