Archive

Archive for June 11, 2020

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 = []
  t.goto(0,0)
  t.setheading(-90)
  
  for p in range (0,p+1) :

    t.goto(0,0)
    t.forward(circle_r)
    nx = t.xcor()
    ny = t.ycor()
  
     # If the user want to reduce the radius.
    circle_r = circle_r - fix_r
    p_list.append([nx,ny])
    t.right(-arc_angle)

  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):
  
  t.goto(x1,y1)
  t.pendown()
  t.goto(x2,y2)
  t.penup()


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))

  try:
    draw_line(p_list[d][0], p_list[d][1],p_list[e1][0], p_list[e1][1])
  except:
    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