Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Drawing Math Equations

Python: Drawing Math Equations

Learning : Python and Math
Subject: Python Project to Draw a Math Equations

In the past week or so I saw a tweet from @matthen2 it was about drawing half circles on 99 points, the 99 points was distributed on a hidden circle, each line (or half circle) connecting x (a point on the circle) and it’s double, so point num 2 is connected to P num 4; (P3 to P6, P4 to P8, …) and so-on

This inspired me to write a Python app (code) to perform same action. Then I just upgrade the idea to draw any mathematical Equation. (Lets say simple mathematical Equations)

In our project, the user will enter the number of points and the starting circle radius, also I fond that we can do more by reducing the circle radius after each point, so i add this to the project.

Coding: We will write a function to collect the points in a list as [x,y], and returns the list. Then we pass each two points of (x1,y1) and (x2,y2) to another Function to draw a line between those points. In this version we will write the Equations as hard-code in our project.

So First Function will be the get_points() here the systems will ask the user to Enter some variables. We will collect the number of points P, the raidus of the circle circumference that points will be distributed On it’s, also we will ask the user if circle radius is fixed or decreased by a given factor.
Using mathematics we know that a circle has 360degree, so if we divide 360 over P we will have (if we can call it) the arc_angle. .. Here is the code for the function …

# Function to collect the x,y points

def get_points() :
  p = int(input('  Enter number of Points:  ')) # number of points.

  arc_angle = 360 / p
  circle_r = int(input('  Enter the Cricle Radius: ') )  
  fix_r = int(input('  Enter a number to reduce the Radius:'))
  #To return the x,y list of the points that we want to connect. 
  p_list = []
  for p in range (0,p+1) :

    nx = t.xcor()
    ny = t.ycor()
     # If the user want to reduce the radius.
    circle_r = circle_r - fix_r

  return p_list

By now, we have a list of points [x,y], and we will passing two points to another function def draw_line(x1, y1, x2, y2): to draw a line between the points. Very simple and easy Function …

# Function to draw a line

def draw_line(x1, y1, x2, y2):

Now the tricky part, How to select the points?. To do this we assume we have an Equation (e1) and if we got any errors like (division by zero, or out of index) we will apply another Equation (e2), e1 and e2 will be hard-coded and each time we need to change it and run the application again to see the result. And to draw a line between the points based on e1 and e2 we will run a for loop …. Here is the code ..

# drawing the equations 


for d in range(len(p_list)-1) :
  e1 = d * 2
  e2 =  abs( len(p_list) - (d*2))

    draw_line(p_list[d][0], p_list[d][1],p_list[e1][0], p_list[e1][1])
    if e1 > len(p_list) :
      draw_line(p_list[d][0], p_list[d][1],p_list[e2][0], p_list[e2][1])

Here is some output

This is the end of this project, Do we need any upgrading in any part of it?

To Download my Python code (.py) files Click-Here

Follow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: