Drawing and Filling a Star with Different Colors in Python

Drawing geometric shapes, especially stars, and filling them with different colors can be an engaging and educative programming task. Python, with its powerful libraries like Turtle and Matplotlib, provides an excellent platform for such tasks. In this article, we will explore how to draw a five-pointed star and fill it with different colors using Python.

Using Turtle Graphics

Turtle graphics is one of the simplest ways to draw shapes in Python. It’s part of Python’s standard library, making it easily accessible for beginners. Here’s how you can use Turtle to draw and fill a five-pointed star with different colors:

pythonCopy Code
import turtle # Set up the screen screen = turtle.Screen() screen.bgcolor("white") # Create a turtle star_turtle = turtle.Turtle() star_turtle.speed(10) # Function to draw a filled star def draw_filled_star(turtle, size, color): turtle.fillcolor(color) turtle.begin_fill() for _ in range(5): turtle.forward(size) turtle.right(144) turtle.end_fill() # Drawing the star draw_filled_star(star_turtle, 100, "blue") # Hide the turtle cursor star_turtle.hideturtle() # Keep the window open turtle.done()

This code snippet creates a simple five-pointed star filled with blue color. You can change the color by modifying the color parameter in the draw_filled_star function.

Using Matplotlib

While Turtle is great for simple graphics and educational purposes, Matplotlib offers more advanced plotting capabilities. Here’s how you can draw and fill a star with Matplotlib:

pythonCopy Code
import matplotlib.pyplot as plt import matplotlib.patches as patches import numpy as np # Create a figure and an axes fig, ax = plt.subplots() # Function to generate the vertices of a star def star_vertices(center_x, center_y, size, n_points=5): outer_circle = np.linspace(0, 2 * np.pi, n_points + 1) inner_circle = np.linspace(np.pi / n_points, 2 * np.pi - np.pi / n_points, n_points) vertices_x = np.concatenate((size * np.cos(outer_circle)[:-1], size / 2 * np.cos(inner_circle))) + center_x vertices_y = np.concatenate((size * np.sin(outer_circle)[:-1], size / 2 * np.sin(inner_circle))) + center_y return vertices_x, vertices_y # Get the vertices of the star vertices_x, vertices_y = star_vertices(0.5, 0.5, 0.2) # Create a polygon from the vertices star = patches.Polygon(np.column_stack([vertices_x, vertices_y]), closed=True, edgecolor='black', facecolor='red') # Add the polygon to the axes ax.add_patch(star) # Show the plot plt.show()

This code generates a five-pointed star and fills it with red color. You can change the color by modifying the facecolor parameter in the patches.Polygon function.

Drawing and filling shapes with colors in Python is not only fun but also educational, especially for those learning programming. It helps in understanding basic programming concepts like functions, loops, and libraries.

[tags]
Python, Drawing, Shapes, Stars, Turtle, Matplotlib, Colors, Programming

78TP is a blog for Python programmers.