【问题标题】:Drawing a filled circle in python在python中绘制一个实心圆
【发布时间】:2015-10-12 00:46:42
【问题描述】:

我需要知道如何制作一个实心圆圈。 我现在有一个空心圆圈,但我需要填充它。 我不知道该怎么做。 有人知道如何用这个填充圆圈。 我也受条件约束。

允许的关键字、运算符等有:if、elif、else、while、for ... in range、=、变量、值、**、*、/、+、-、%、math.sqrt()、 and, or, not, ==, !=, , >=, +=, def and return

但是,您不能在字符串和 int 上使用 *-operator。因此,例如 ''***'' * 3 是不允许的,但 3 * 4 是 有一个 abs 函数和一个 join line 函数,有人知道它的替代方法吗?

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)

【问题讨论】:

    标签: python draw geometry


    【解决方案1】:

    你几乎猜对了。我认为这就是您要寻找的:

    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))
    

    祝你好运!

    【讨论】:

    • 对不起,我忘了问我的问题,我还需要用户能够输入一个 sice,然后我应该缩放到那个数字。您还知道 .join(line)) 函数的另一个选项吗?我不允许使用那个
    • oeps 尚未完成,但我不确定 .join(line) 函数我;不确定我是否可以使用该函数。它说运算符和关键字是。功能选项与关键字或运算符不同吗?所以我可以使用那个功能吗?
    【解决方案2】:

    疯狂的方式(找到第一次出现和最后一次出现的索引并在这些元素之间用#填充列表:

    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)
    

    *是的,这不符合您的限制。

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

    【讨论】:

      【解决方案3】:

      更改条件,使其接受到中心的距离小于或等于半径的所有坐标。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-31
        • 2021-04-19
        • 2019-01-21
        • 1970-01-01
        • 2021-01-30
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        相关资源
        最近更新 更多