我需要帮助开发圆形图案

时间:2016-10-24 12:12:34

标签: python geometry computational-geometry

我正在尝试生成此模式,而不是添加for loops和近似角度(我认为朝向末端的角度可能会关闭)。我想知道是否有人可以用更少的代码来提供生成这种模式的一些见解。

最终目标是简单地说明半径以及我希望生成多少个圆圈

import numpy as np
import matplotlib.pyplot as plt

__author__ = 'George Pamfilis'

def rotate_around_origin(r,angle):
    # http://tutorial.math.lamar.edu/Classes/CalcII/PolarCoordinates.aspx
    x = r * np.cos(angle)
    y = r * np.sin(angle)
    return x, y

if __name__ == '__main__':
    r = 1
    r_cylinder = 1
    p1 = (0, 0)
    ps = [p1]

angle = 60

# first layer
for i in range(6):
    p_new = rotate_around_origin(2*r + (r/2) * 0, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60

# second layer


angle = 30

for i in range(6):
    p_new = rotate_around_origin(3*r+(r/2) * 1, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60
for i in range(6):
    p_new = rotate_around_origin(4*r+(r/2)*0, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60 + 30/2 + 4

for i in range(6):
    p_new = rotate_around_origin(5*r+(r/3), np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60 +30/2 + 4 -8 + 30

for i in range(6):
    p_new = rotate_around_origin(5*r+(r/3), np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60
for i in range(6):
    p_new = rotate_around_origin(5*r+(r/1)*1, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60

angle = 30
for i in range(6):
    p_new = rotate_around_origin(6 * r + (r / 1) * 1, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60 +13.75
for i in range(6):
    p_new = rotate_around_origin(6*r+(r/1)*1+r/4, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 46.25
for i in range(6):
    p_new = rotate_around_origin(6*r+(r/1)*1+r/4, np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


angle = 60
for i in range(6):
    p_new = rotate_around_origin(6*r+(r/1)*1+r/4 + (r/1.25), np.deg2rad(angle))
    ps.append(p_new)
    angle += 60


plt.figure(figsize=(20, 20 * (1/1)))

fig = plt.gcf()
# circle1 = plt.Circle((0, 0), 6*r+r+r+r/2+r, color="r")
# this is added just to enclose the pattern.
circle1 = plt.Circle((0, 0), 6*r+r+r+r, color="r")

fig.gca().add_artist(circle1)
for p in ps:
    circle1 = plt.Circle((p[0], p[1]), r)
    fig.gca().add_artist(circle1)


plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

找到给定大R和层数(环包括中心小圆)的小圆半径

找到水平图层的数量

每个水平层的

计算第一个和最后一个小圆圈位置

使用Delphi示例:

var
  cx, cy, R, NL, irs: Integer;
  rs, yshift, dx: Double;
  iymax, ix, iy, mx, my, il, ir, Nl4: Integer;
begin
  R := 180;
  NL := 8;
  cx := 200;
  cy := 200;
  Canvas.Brush.Color := clRed;
  Canvas.Ellipse(cx - R, cy - R, cx + R + 1, cy + R + 1);
  Canvas.Brush.Color := clBlue;

  rs := R / (2 * NL - 1); // small radius
  irs := Round(rs); // integer radius for drawing
  Nl4 := 4 * (NL - 1) * (NL - 1);
  iymax := Floor((2 * NL - 2) / Sqrt(3)); // horizontal layers from center to top

  for iy := -iymax to iymax do
  begin
    yshift := iy * Sqrt(3) * rs; // relative to center
    my := cy + Round(yshift); // y-coordinate

    // find left small circle inside big one
    if Odd(iy) then
    begin
      il := Floor(0.5 * Sqrt(Nl4 - 3 * iy * iy) + 0.5);
      dx := rs;
      ir := 1;
    end
    else
    begin
      il := Floor(0.5 * Sqrt(Nl4 - 3 * iy * iy));
      dx := 0;
      ir := 0;
    end;

    for ix := -il to il - ir do
    begin
      mx := cx + Round(ix * 2 * rs + dx);
      Canvas.Ellipse(mx - irs, my - irs, mx + irs + 1, my + irs + 1);
    end;
  end;

enter image description here