Thursday, 27 March 2025

Dragon Curve Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

def dragon_curve(iterations):

    points = np.array([[0, 0], [1, 0]])

    direction = np.array([[0, -1], [1, 0]])

    for _ in range(iterations):

        new_points = []

        for i in range(len(points) - 1):

            p1, p2 = points[i], points[i + 1]

            midpoint = (p1 + p2) / 2

            rotated_vector = np.dot(direction, p2 - midpoint)

            new_point = midpoint + rotated_vector

            new_points.extend([p1, new_point])

        new_points.append(points[-1])

        points = np.array(new_points)

    return points

def plot_dragon_curve(iterations):

    points = dragon_curve(iterations)

    plt.figure(figsize=(6, 6))

    plt.plot(points[:, 0], points[:, 1], color='blue', linewidth=0.7)

    plt.axis('equal')

    plt.title("Dragon Curve Pattern")

    plt.show()

plot_dragon_curve(12)

#source code --> clcoding.com

Code Explanation:

1. Importing Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: A library used for numerical computing. It helps handle arrays efficiently.

matplotlib.pyplot: A library for plotting graphs and visualizing the generated fractal pattern.

 2. Defining the Dragon Curve Function

 def dragon_curve(iterations):

This function generates the Dragon Curve points based on the given number of iterations.

Each iteration refines the shape, making it more complex.

    points = np.array([[0, 0], [1, 0]])

points: This initializes the Dragon Curve with a straight-line segment between two points:

(0,0) → starting point

(1,0) → ending point

 direction = np.array([[0, -1], [1, 0]])

direction: This is a rotation matrix that rotates a point 90 degrees counterclockwise.

 3. Iterating to Generate the Fractal

    for _ in range(iterations):

This loop runs for the given number of iterations.

 Each iteration adds new points to make the shape more complex.

        new_points = []

new_points: This will store the new set of points after each iteration.

 4. Iterating Over the Current Points

        for i in range(len(points) - 1):

This loop goes through all adjacent points in the current list.

 It processes each segment in the shape.

           p1, p2 = points[i], points[i + 1]

p1: The starting point of the segment.

 p2: The ending point of the segment.

  midpoint = (p1 + p2) / 2

Finds the midpoint of the segment between p1 and p2.

           rotated_vector = np.dot(direction, p2 - midpoint)

p2 - midpoint: Finds the vector from the midpoint to p2.

 np.dot(direction, vector): Rotates this vector 90 degrees counterclockwise using the rotation matrix.

           new_point = midpoint + rotated_vector

Finds the new rotated point that will be inserted into the shape.

             new_points.extend([p1, new_point])

Adds the old point p1 and the new rotated point to the list.

 5. Completing the Iteration

        new_points.append(points[-1])

Ensures that the last point of the previous iteration remains in the shape.

        points = np.array(new_points)

Updates points with the new set of points after this iteration.

 6. Returning the Generated Points

    return points

After all iterations, the function returns the final set of points forming the fractal.

 7. Function to Plot the Dragon Curve

def plot_dragon_curve(iterations):

This function is responsible for visualizing the generated fractal.

   points = dragon_curve(iterations)

Calls dragon_curve(iterations) to generate the points.

    plt.figure(figsize=(6, 6))

Creates a new figure with a 6x6 inch size.

  plt.plot(points[:, 0], points[:, 1], color='blue', linewidth=0.7)

Plots the points using:

 X-coordinates (points[:, 0])

Y-coordinates (points[:, 1])

Blue color

 Line width of 0.7

    plt.axis('equal')

Ensures that the aspect ratio is equal, so the pattern does not look stretched.

    plt.title("Dragon Curve Pattern")

Adds a title to the plot.

    plt.show()

Displays the final fractal plot

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1042) Python Coding Challenge (456) Python Quiz (117) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)