给定半径和中心的圆圈上的所有点都是Python

时间:2018-03-29 08:35:35

标签: python center geometry radius

我写了这个函数:

[X-center, Y-center, Radius]

'circle'是一个image = reader.AcquireLatestImage(); ByteBuffer buffer = image.GetPlanes()[0].Buffer; byte[] bytes = new byte[buffer.Remaining()]; buffer.Get(bytes); // need to get the bitmap in order to compress Bitmap bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { bitmap.Compress(Bitmap.CompressFormat.Jpeg, 85, stream); Save(stream.GetBuffer()); }

的数组

我想提取圆圈中存在的整数分辨率的所有点。 现在,我意识到我正在创建一个位于圆圈BORDER上的点数组,但是我无法访问圆圈内的点。

我想到的只是缩小半径,并对半径的所有值进行迭代,直到半径为0

但我觉得有一种更有效的方式。欢迎任何帮助

1 个答案:

答案 0 :(得分:2)

from itertools import product
def points_in_circle(radius):
    for x, y in product(range(int(radius) + 1), repeat=2):
        if x**2 + y**2 <= radius**2:
            yield from set(((x, y), (x, -y), (-x, y), (-x, -y),))
list(points_in_circle(2))
[(0, 0), (0, 1), (0, -1), (0, -2), (0, 2), (1, 0), (-1, 0), (-1, 1), (1, -1), (1, 1), (-1, -1), (2, 0), (-2, 0)]

与numpy

def points_in_circle_np(radius):
    a = np.arange(radius + 1)
    for x, y in zip(*np.where(a[:,np.newaxis]**2 + a**2 <= radius**2)):
        yield from set(((x, y), (x, -y), (-x, y), (-x, -y),))

具有任意中心

def points_in_circle_np(radius, x0=0, y0=0, ):
    x_ = np.arange(x0 - radius - 1, x0 + radius + 1, dtype=int)
    y_ = np.arange(y0 - radius - 1, y0 + radius + 1, dtype=int)
    x, y = np.where((x_[:,np.newaxis] - x0)**2 + (y_ - y0)**2 <= radius**2)
    # x, y = np.where((np.hypot((x_-x0)[:,np.newaxis], y_-y0)<= radius)) # alternative implementation
    for x, y in zip(x_[x], y_[y]):
        yield x, y