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)
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack Internalall( 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)
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 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 --
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() --
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()