Skip to content

VihaShomikha/Elegant-Heart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

ElegantHearts: Drawing Hearts with Mathematical Precision

This Python code generates a beautiful heart-shaped drawing using mathematical functions to define the curve shape.

Features:

  • Creates smooth and elegant heart shapes.
  • Utilizes mathematical functions for precise control over the curve.
  • Allows for customization by adjusting parameters within the code.

Instructions:

  1. Prerequisites:
    • Ensure you have Python 3 and the turtle library installed.
    • To install turtle, run pip install turtle in your terminal.
  2. Running the Code:
    • Save this code as a Python file (e.g. hearts.py).
    • Open a terminal, navigate to the directory where you saved the file, and run:
      python elegant_hearts.py

Explanation:

The code leverages the turtle library to draw shapes based on mathematical functions. Here's a breakdown of the key parts:

  • Import: The import math line imports the math library for mathematical calculations.
    • The from turtle import* line imports all functions from the turtle library for convenience.
  • Heart Functions:
    • hearta(k) = 15*math.sin(k)**3 defines a mathematical function to calculate the x-coordinate of a point on the heart curve using sine and cube operations.
    • heartb(k) = 12*math.cos(k)-5*math.cos(2*k)-2*math.cos(3*k)-math.cos(4*k) defines a mathematical function to calculate the y-coordinate of a point on the heart curve using cosine and multiple angles.
  • Turtle Setup:
    • speed(0) sets the turtle's drawing speed to the fastest for a smoother animation.
    • bgcolor("black") sets the background color to black for a contrasting effect.
  • Heart Drawing Loop:
    • The for i in range(6000) loop iterates 6000 times, creating many small line segments to form the smooth heart shape.
    • Inside the loop:
      • goto(hearta(i)*20, heartb(i)*20) uses the defined functions to calculate the coordinates and moves the turtle to that point on the curve, scaled by 20 for larger size.
  • Fill and Reset (Optional):
    • The commented-out section (for j in range(5): color("red"); goto(0,0)) can be uncommented to fill the heart shape with red color using another loop and then reset the turtle's position for potential additional drawing.
    • Consider adjusting the number of iterations in the fill loop and the color as desired.
  • Hold the Window Open: done() keeps the turtle window open after drawing is complete, allowing you to view the finished artwork.

This combination of mathematical functions and turtle graphics creates a visually appealing and mathematically defined heart shape.

Customization:

  • You can adjust the scaling factor (currently 20) in the goto calls to control the size of the heart.
  • Experiment with different mathematical functions in the hearta and heartb definitions to create variations in the heart shape.

Have fun exploring and customizing the code!

About

Drawing Heart with Mathematical Precision

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages