Skip to main content
added 17 characters in body
Source Link
Maarten Fabré
  • 9.4k
  • 1
  • 16
  • 27
all( np.array_equal( np.hstack(hexit_60(n_max)), np.hstack(list(hexit_60_gen(n_max))) ) for n_max in range(10) ) 

if pairs is empty, this returns None, this returns None implicitly.

draw_polygon(7) 
all( np.array_equal( np.hstack(hexit_60(n_max)), np.hstack(list(hexit_60_gen(n_max))) ) for n_max in range(10) ) 

if pairs is None, this returns None implicitly.

all( np.array_equal( hexit_60(n_max), np.hstack(list(hexit_60_gen(n_max))) ) for n_max in range(10) ) 

if pairs is empty, this returns None implicitly.

draw_polygon(7) 
added 89 characters in body
Source Link
Maarten Fabré
  • 9.4k
  • 1
  • 16
  • 27
def draw_polygon(sides=6): fig = plt.figure() for i, n_max in enumerate([-1, 0, 1, 2, 3, 4]): ax = fig.add_subplot(2, 3, i + 1) # plt.subplot(2, 3, i+1) ax.set_title("n_max: " + str(n_max)) ax.set_xlim(-4.2, 4.2) ax.set_ylim(-4.2, 4.2) if n_max < 0: continue all_points = points(n_max, sides=sides) ax.scatter(*all_points) ax.plot(*all_points, "-k", linewidth=0.5) ax.set_aspect("equal") plt.show()return fig 

enter image description here

def draw_polygon(sides=6): fig = plt.figure() for i, n_max in enumerate([-1, 0, 1, 2, 3, 4]): ax = fig.add_subplot(2, 3, i + 1) # plt.subplot(2, 3, i+1) ax.set_title("n_max: " + str(n_max)) ax.set_xlim(-4.2, 4.2) ax.set_ylim(-4.2, 4.2) if n_max < 0: continue all_points = points(n_max, sides=sides) ax.scatter(*all_points) ax.plot(*all_points, "-k", linewidth=0.5) ax.set_aspect("equal") plt.show() 
def draw_polygon(sides=6): fig = plt.figure() for i, n_max in enumerate([-1, 0, 1, 2, 3, 4]): ax = fig.add_subplot(2, 3, i + 1) # plt.subplot(2, 3, i+1) ax.set_title("n_max: " + str(n_max)) ax.set_xlim(-4.2, 4.2) ax.set_ylim(-4.2, 4.2) if n_max < 0: continue all_points = points(n_max, sides=sides) ax.scatter(*all_points) ax.plot(*all_points, "-k", linewidth=0.5) ax.set_aspect("equal") return fig 

enter image description here

added regular polygon
Source Link
Maarten Fabré
  • 9.4k
  • 1
  • 16
  • 27

--

alternative approach

Instead of the approach of the hexit_60, you can have a function to assemble the first side of the hexagon, and then apply rotations to this:

ANGLE = np.pi / 3 COS_ANGLE = np.cos(ANGLE) SIN_ANGLE = np.sin(ANGLE) ROTATION_MATRIX = [[COS_ANGLE, -SIN_ANGLE], [SIN_ANGLE, COS_ANGLE]] 

Then to get one side, using np.linspace to do the interpolation:

def side_points(n): if n == 0: return np.array([[0], [0]]) p0 = np.array([n, 0]) p1 = n * np.array([COS_ANGLE, SIN_ANGLE]) return np.linspace(p0, p1, n, endpoint=False).T 

And then the rotations is a simple generator

def rotations(side): yield side if np.array_equal(side, np.array([[0], [0]])): return for _ in range(5): side = np.dot(ROTATION_MATRIX, side) yield side 

Note that this can be easily adjusted to any regular polygon, by adjusting the angle, and passing in the appropriate cosinus, sinus and or rotation_matrix as function parameter instead of using the global

from functools import lru_cache @lru_cache(None) def cos_angle(sides): return np.cos(2 * np.pi / sides) @lru_cache(None) def sin_angle(sides): return np.sin(2 * np.pi / sides) @lru_cache(None) def rotation_matrix(sides): return np.array( [ [cos_angle(sides), -sin_angle(sides)], [sin_angle(sides), cos_angle(sides)], ] ) def side_points(n, sides=6): if n == 0: return np.array([[0], [0]]) p0 = np.array([n, 0]) p1 = n * np.array([cos_angle(sides), sin_angle(sides)]) return np.linspace(p0, p1, n, endpoint=False).T def rotations(side, sides=6): yield side if np.array_equal(side, np.array([[0], [0]])): return rot = rotation_matrix(sides) for _ in range(sides - 1): side = np.dot(rot, side) yield side def points(n_max, sides=6): return np.hstack( list( itertools.chain.from_iterable( rotations(side_points(n, sides), sides) for n in range(n_max + 1) ) ) ) 

Then drawing the polygon is simply:

def draw_polygon(sides=6): fig = plt.figure() for i, n_max in enumerate([-1, 0, 1, 2, 3, 4]): ax = fig.add_subplot(2, 3, i + 1) # plt.subplot(2, 3, i+1) ax.set_title("n_max: " + str(n_max)) ax.set_xlim(-4.2, 4.2) ax.set_ylim(-4.2, 4.2) if n_max < 0: continue all_points = points(n_max, sides=sides) ax.scatter(*all_points) ax.plot(*all_points, "-k", linewidth=0.5) ax.set_aspect("equal") plt.show() 

--

alternative approach

Instead of the approach of the hexit_60, you can have a function to assemble the first side of the hexagon, and then apply rotations to this:

ANGLE = np.pi / 3 COS_ANGLE = np.cos(ANGLE) SIN_ANGLE = np.sin(ANGLE) ROTATION_MATRIX = [[COS_ANGLE, -SIN_ANGLE], [SIN_ANGLE, COS_ANGLE]] 

Then to get one side, using np.linspace to do the interpolation:

def side_points(n): if n == 0: return np.array([[0], [0]]) p0 = np.array([n, 0]) p1 = n * np.array([COS_ANGLE, SIN_ANGLE]) return np.linspace(p0, p1, n, endpoint=False).T 

And then the rotations is a simple generator

def rotations(side): yield side if np.array_equal(side, np.array([[0], [0]])): return for _ in range(5): side = np.dot(ROTATION_MATRIX, side) yield side 

Note that this can be easily adjusted to any regular polygon, by adjusting the angle, and passing in the appropriate cosinus, sinus and or rotation_matrix as function parameter instead of using the global

from functools import lru_cache @lru_cache(None) def cos_angle(sides): return np.cos(2 * np.pi / sides) @lru_cache(None) def sin_angle(sides): return np.sin(2 * np.pi / sides) @lru_cache(None) def rotation_matrix(sides): return np.array( [ [cos_angle(sides), -sin_angle(sides)], [sin_angle(sides), cos_angle(sides)], ] ) def side_points(n, sides=6): if n == 0: return np.array([[0], [0]]) p0 = np.array([n, 0]) p1 = n * np.array([cos_angle(sides), sin_angle(sides)]) return np.linspace(p0, p1, n, endpoint=False).T def rotations(side, sides=6): yield side if np.array_equal(side, np.array([[0], [0]])): return rot = rotation_matrix(sides) for _ in range(sides - 1): side = np.dot(rot, side) yield side def points(n_max, sides=6): return np.hstack( list( itertools.chain.from_iterable( rotations(side_points(n, sides), sides) for n in range(n_max + 1) ) ) ) 

Then drawing the polygon is simply:

def draw_polygon(sides=6): fig = plt.figure() for i, n_max in enumerate([-1, 0, 1, 2, 3, 4]): ax = fig.add_subplot(2, 3, i + 1) # plt.subplot(2, 3, i+1) ax.set_title("n_max: " + str(n_max)) ax.set_xlim(-4.2, 4.2) ax.set_ylim(-4.2, 4.2) if n_max < 0: continue all_points = points(n_max, sides=sides) ax.scatter(*all_points) ax.plot(*all_points, "-k", linewidth=0.5) ax.set_aspect("equal") plt.show() 
Source Link
Maarten Fabré
  • 9.4k
  • 1
  • 16
  • 27
Loading