10

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

map[x][y]

It kinda will look like this:

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

output:

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

I want to make a circle in it like:

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

How can i do this?

I want to try and make a triangulation system where i will find the point that 3 circles will overlap. Is there any other way I can achieve this.

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

p99will
  • 250
  • 2
  • 3
  • 14

2 Answers2

11

The basic formula for a circle is

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

Where (x, y) is a 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 (x-a)**2 + (y-b)**2 == r**2
        if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2:
            map_[y][x] = '#'

# print the map
for line in map_:
    print ' '.join(line)

This results in

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

You'll have to fiddle with the value for EPSILON with this method.

Alternatively, iterate by angle and calculate the (x,y) coordinate as you go

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:

. . . # # # # # . . .
. # # . . . . . # # .
. # . . . . . . . # .
# . . . . . . . . # #
# . . . . . . . . . #
# . . . . . . . . . #
# . . . . . . . . . #
# . . . . . . . . . #
. # . . . . . . . # .
. # # . . . . . # # .
. . . # # # # # . . .
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
  • Hey Peter, I know this is an older question but I was wondering why I get in the circle when using the bottom solution? Even if I set the 5 to a 1 I get gaps. – G3tinmybelly Apr 07 '18 at 17:52
  • I'd suggest you ask a new question so you can post the code and details along with an example, then link to it here – Peter Gibson Apr 07 '18 at 21:53
  • 2
    @PeterGibson , if you dont mind : How do i fill the circle ? – Ian_De_Oliveira Sep 04 '18 at 10:53
  • @Ian_De_Oliveira change the if condition in the first example so that it's looking for x,y values where the distance from the centre is less than the radius, instead of close to the radius. – Peter Gibson Sep 04 '18 at 19:43
  • @PeterGibson , do you mean if abs((x-a)**2 + (y-b)**2 < r**2) < EPSILON**2: , itI fill the whole space.. What could do if the radius is None or zero to just put a point in the space? Sorry I'm on my first steps in programming . I did a work around using np, but is not elegant. elif abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2: map_[y][x] = '1' map_ = np.array(map_).astype(int) map_.cumsum(0) mask = map_.cumsum(0)==1 map_[mask] = 1 – Ian_De_Oliveira Sep 04 '18 at 22:23
  • @Ian_De_Oliveira Something like (untested) `if (x-a)**2 + (y-b)**2 <= r**2:`. If you want a point, either increase the radius a bit beforehand, or add the `EPSILON` back in – Peter Gibson Sep 04 '18 at 22:57
5

The formula of circle is

(x-a)^2 + (y-b)^2 - r^2 = 0

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