John
John

Reputation: 91

Drawing a filled circle in python

I need to know how to make a filled circle. i Have a hollow circle now but i need it to be filled. I can not figure out how to do that. does someone know how to make from this a filled circle. also i'm bound by conditions.

Allowed keywords, operators, etc. are: if, elif, else, while, for … in range, =, variables, values, **, *, /, +, -, %, math.sqrt(), and, or, not, ==, !=, <, <=, >, >=, +=, def and return

However, you are not allowed to use the *-operator on string and int. So for example ''***'' * 3 is not allowed, but 3 * 4 is there is a abs function and a join line function in there does someone know a alternative for that?

width, height = 11, 11
a, b = 5, 5
r = 5
EPSILON = 2.2

map_= [[' ' for x in range(width)]for y in range(height)]

for y in range(height):
    for x in range(width):
        if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2:
            map_[y][x] = "#"
for line in map_:
    print ' '.join(line)

Upvotes: 1

Views: 6572

Answers (3)

xi&#186;
xi&#186;

Reputation: 4687

Crazy way (find index of first occurence and last occurence and fill list with # between these elements:

def first_occ(l):
    return l.index('#')

def last_occ(l):
    return len(l) - 1 - l[::-1].index('#')

for i, el in enumerate(map_):
    map_[i][first_occ(el):last_occ(el)] = ['#'] * (last_occ(el) - first_occ(el))

for line in map_:
    print ' '.join(line)

* Yes and this does not satisfy to your restrictions.

      # # # # #
    # # # # # # #
  # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # #
  # # # # # # # # #
    # # # # # # #
      # # # # #

Upvotes: 1

Sushant Kafle
Sushant Kafle

Reputation: 446

You almost got it right. I think this is what you are looking for:

width, height = 11, 11
a, b = 5, 5
r = 5
EPSILON = 2.2

map_= [[' ' for x in range(width)]for y in range(height)]

for y in range(height):
    for x in range(width):
        if (x-a)**2 + (y-b)**2 <= (r**2 - EPSILON**2):
            map_[y][x] = "#"
for line in map_:
    print(' '.join(line))

Goodluck!

Upvotes: 4

Kevin
Kevin

Reputation: 76194

Change your conditional so that it accepts all coordinates whose distance to the center is less than or equal to the radius.

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

Upvotes: 0

Related Questions