【Python×matplotlib×ChatGPT】シェルピンスキーのギャスケットとカーペットをアレンジ!鋭利な三角形になりました。

こんにちは!みなさん。

今日もpythonでプログラミングをして自分好みの図形を作っていていきます。

これまで作った図の中で、お気に入りの3つです。

好きな図を見ていると、なんだか落ち着いた気持ちになります。


こんな感じで、もっと何か作りたいなと幾何学模様をキーワードに探していると、シェルピンスキーのギャスケットという図が目に止まりました。

こういう図です。



pythonコードです。

import matplotlib.pyplot as plt

def draw_sierpinski_triangle(ax, x1, y1, x2, y2, x3, y3, depth):
    if depth == 0:
        ax.fill([x1, x2, x3], [y1, y2, y3], 'k')
        return

    # Calculate midpoints of the edges
    mid_x1, mid_y1 = (x1 + x2) / 2, (y1 + y2) / 2
    mid_x2, mid_y2 = (x2 + x3) / 2, (y2 + y3) / 2
    mid_x3, mid_y3 = (x1 + x3) / 2, (y1 + y3) / 2

    draw_sierpinski_triangle(ax, x1, y1, mid_x1, mid_y1, mid_x3, mid_y3, depth - 1)
    draw_sierpinski_triangle(ax, mid_x1, mid_y1, x2, y2, mid_x2, mid_y2, depth - 1)
    draw_sierpinski_triangle(ax, mid_x3, mid_y3, mid_x2, mid_y2, x3, y3, depth - 1)

# Set up the plot
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.axis('off')

# Define the initial triangle
x1, y1 = 0, 0
x2, y2 = 1, 0
x3, y3 = 0.5, 0.87  # Height of an equilateral triangle

# Number of iterations (increase this for more detail)
depth = 5

draw_sierpinski_triangle(ax, x1, y1, x2, y2, x3, y3, depth)

plt.show()



三角形が規則正しく並んでいます。
見れば見るほど、なぜか怖い感じがします。

三角形の頂点をランダムにすると、下のような図ができました。



個人的に好きな図です。
シャープな感じがかっこよいなと思いました。


三角形の数を増やして、色を付けると以下のような感じになりました。

笹の葉に見えます。
これもいいですね。

次に、シェルピンスキーのカーペットです。
四角なんですね。こんな感じです。色を付けてみました。





pythonコードはこちらです。

import numpy as np
import matplotlib.pyplot as plt

def generate_random_warm_color():
    # Generate a random warm color (e.g., shades of red, orange, and yellow)
    r = np.random.randint(200, 256)
    g = np.random.randint(100, 256)
    b = np.random.randint(0, 100)
    return (r, g, b)

def draw_carpet(image, x, y, size, depth):
    if depth <= 0:
        return

    # Draw the central square
    center_x = x + size // 3
    center_y = y + size // 3
    center_size = size // 3

    # Add texture to the central square
    texture_size = center_size // 2
    texture = np.zeros((texture_size, texture_size, 3), dtype=np.uint8)
    for i in range(texture_size):
        for j in range(texture_size):
            texture[i, j] = generate_random_warm_color()

    # Ensure texture fits within the central square
    if texture.shape != (center_size, center_size, 3):
        texture = np.zeros((center_size, center_size, 3), dtype=np.uint8)
        for i in range(center_size):
            for j in range(center_size):
                texture[i, j] = generate_random_warm_color()

    image[center_y:center_y + center_size, center_x:center_x + center_size] = texture

    # Recursive calls for the eight smaller squares
    new_size = size // 3
    for dx in range(3):
        for dy in range(3):
            if dx == 1 and dy == 1:
                continue
            new_x = x + dx * new_size
            new_y = y + dy * new_size
            draw_carpet(image, new_x, new_y, new_size, depth - 1)

def main():
    size = 729
    depth = 3  # Set the depth of recursion (adjust as needed)

    # Create a blank white image
    image = np.zeros((size, size, 3), dtype=np.uint8)

    # Draw the carpet
    draw_carpet(image, 0, 0, size, depth)

    # Display the image using matplotlib
    plt.imshow(image)
    plt.axis('off')
    plt.show()

if __name__ == "__main__":
    main()



そして、立体的に見えるようにしてみました。





四角がいっぱいですね。
なかなか可愛いです。