How can I draw a circle in a dataset / map in python

I have an array that will be 100 * 100, I can access any point, for example

map[x][y]

It will look like this:

 for i in map: for ii in i: print ii, print '\n', 

output:

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 

I want to create a circle in it:

 . . . . . # . . . . . . . . # # . # # . . . . . # . . . . . # . . . # . . . . . . . # . . # . . . . . . . # . # . . . . . . . . . # . # . . . . . . . # . . # . . . . . . . # . . . # . . . . . # . . . . . # # . # # . . . . . . . . # . . . . . 

How can i do this?

I want to try to create a triangulation system, where I will find the point at which 3 circles intersect. Is there any other way to achieve this.

I just want to get the distance (points from the center) and direction.

+6
source share
2 answers

Basic formula for a circle

 (x - a)**2 + (y - b)**2 = r**2 

Where (x, y) is the point, (a, b) is the center of the circle, and r is the radius.

 width, height = 11, 11 a, b = 5, 5 r = 5 EPSILON = 2.2 map_ = [['.' for x in range(width)] for y in range(height)] # draw the circle for y in range(height): for x in range(width): # see if we're close to (xa)**2 + (yb)**2 == r**2 if abs((xa)**2 + (yb)**2 - r**2) < EPSILON**2: map_[y][x] = '#' # print the map for line in map_: print ' '.join(line) 

The result is

 . . . # # # # # . . . . . # . . . . . # . . . # . . . . . . . # . # . . . . . . . . . # # . . . . . . . . . # # . . . . . . . . . # # . . . . . . . . . # # . . . . . . . . . # . # . . . . . . . # . . . # . . . . . # . . . . . # # # # # . . . 

With this method you will have to play with the value of EPSILON .

Alternatively, iterate over the angle and calculate the coordinate (x, y) along the way

 import math # draw the circle for angle in range(0, 360, 5): x = r * math.sin(math.radians(angle)) + a y = r * math.cos(math.radians(angle)) + b map_[int(round(y))][int(round(x))] = '#' 

gives:

 . . . # # # # # . . . . # # . . . . . # # . . # . . . . . . . # . # . . . . . . . . # # # . . . . . . . . . # # . . . . . . . . . # # . . . . . . . . . # # . . . . . . . . . # . # . . . . . . . # . . # # . . . . . # # . . . . # # # # # . . . 
+8
source

Circle formula

 (xa)^2 + (yb)^2 - r^2 = 0 

where x, y are coodrinates points, a, b are the coordinates of the center, and r is the radius of the circle. Just find all the points for which this equation is true. Since your field is integer, you need to replace =0 with <1 or even <= 1 , no matter what looks better.

+1
source

All Articles