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

By: Ali Radwani

## Python : Triangle Parameters

**Learning : Python to Draw a Triangle **

** Subject: To get the Parameters of a Triangle**

A week ago I start helping a friend in a mathematics geometric to draw a Triangle from Two points that represent the The Hypotenuse of a Triangle, then I deside to write a python project to Draw a triangle based on two given points (x1,y1) and (x2,y2) and to print it’s parameter (other sides and angles). We will use the **Trinket.io** as a Python interrapter and will draw with turtle library.

**Project Details**: The system will ask the user to Enter the coordinates of Two points To draw a Right Angle Triangle. In our Triangle, we will call the (x1,y1) as Point A, and (x2,y2) as Point C. From our starting poins we can calculate the distance between point A and point (B), also the length of Hypotenuse and the angels in the Triangle.

So, What we have:

tri_opposite = abs( y2 – y1 )

tri_adjacent = abs( x2 – x1 )

From a mathimatics triangle formula we know that:

Opposite^2 + Adjacent^2 = Hypotenuse^2

where: x^2 = square(x)

So:

tri_hypo = tri_opposite**2 + tri_adjacent**2

tri_hypo = math.sqrt(tri_hypo)

tri_hypo is the distance between point A and Point C (the opposite

side of the right angle)

**Now the Angels:**

**A**s we know that in a triangle the summation on all inside angles is 180deg, and in a Right Triangle we will have a one fixed 90 degree angle (ABC), so the first thing we will work on calculating the Opposite angle (BAC).

From a Triangle math:

tri_opposite = abs( y2 – y1)

tri_adjacent = abs(x2 -x1)

the_deg (BAC) = inverse tan for (tri_opposite / tri_adjacent)

# get the inverse of Tan

the_ang = math.degrees(math.atan(the_deg))

**Now, lets do some coding ..**

First thing we will import two libraries turtle and math, also we will do some setting for our turtle. … Here is the code ..

# Turtle setting import turtle, math # turtle_setting t = turtle.Turtle() t.penup() t.shape("non") #t.speed(9) t.speed('fastes') #t.visible = False t.hideturtle()

Then we will write a code to draw a ‘Gray’ coordinates cross lines for our project. Here is the code for it ..

def the_cross() : # To draw the cross represents the coordinate. t.penup() t.pencolor('gray') t.goto(-200,0) t.setheading(0) t.pendown() t.forward(400) t.penup() t.goto(0,200) t.setheading(90) t.pendown() t.forward(-400) t.penup() t.goto(2,0) t.pendown() t.circle(2) t.penup() t.goto(0,0) t.setheading(0) # calling the function the_cross()

**Now** we will write the main function code, First we will ask the user to enter the x,y for two points, then we will do our calculations to get other Triangle parameters and angles, finally we will draw the Triangle. .. Here is the code..

def draw_triangle(x1, y1, x2, y2) : # draw the Triangle. style = ('Courier', 20) # font style. t.penup() t.goto(x1,y1) t.setheading(0) t.pendown() t.circle(1) # To draw small circle on point A. t.write('A',font = style) # To write A next to the point. t.forward((x2-x1)) x3 = t.xcor() # here is the x for point B y3 = t.ycor() # here is the y for point B t.right(-90) t.forward((y2-y1)) t.write('C',font = style) # To write C next to the point. t.goto(x2,y2) t.setheading(0) t.pendown() t.circle(1) # To draw small circle on point C. t.penup() t.goto(x3,y3) t.pendown() t.circle(1) # To draw small circle on point B. t.penup() t.setpos(x3-15,y3-15) t.write('B',font = style) # To write B next to the point. # To calculate the angle of BAC using Triangle Math Equations. tri_opposite = abs( y2 - y1) tri_adjacent = abs(x2 -x1) the_deg = tri_opposite / tri_adjacent # get the inverse ot Tan using Triangle Math Equations. the_ang = math.degrees(math.atan(the_deg)) #atan(x) is the inverse of math Tan function. t.goto(x1,y1) # To correct the rotation angle based on it's coordinates. if (x1 > x3) and (y2 > y1) : rotation = the_ang - 180 elif (x1 y1): rotation = 360 - the_ang elif (x1 > x3) and (y2 < y1) : rotation = 180 - the_ang elif (x1 < x3) and (y2 < y1) : rotation = the_ang - 360 #t.right(rotation) #t.pendown() # From using Triangle Math Equations, we know that Hypotenuse^2 = Opposite^2 + adjacent^2. tri_hypo = tri_opposite**2 + tri_adjacent**2 tri_hypo = math.sqrt(tri_hypo) t.right(rotation) t.pendown() t.forward(tri_hypo) t.penup() print('\n The Triangle Information:') print(' Point A= ({},{})'.format(x1,y1)) print(' Point B= ({},{})'.format(x3,y3)) print(' Point C= ({},{})'.format(x2,y2)) print(' Adjacent',tri_adjacent) print(' Opposite',tri_opposite) print(' Hypotenuse',tri_hypo) print(' Angle: ABC = 90') print(' Angle: BAC = ',the_ang) print(' Angle: ACB = ',180 - the_ang - 90)

**F**inally, we need to call the function and pass the two point to it, to do this first we will ask the user to Enter the Two Points and we will make sure that the points are not the same.. Here is the code ..

# calling the draw_triangle function .. print('\n To draw the Triangle you need to Enter Two Points as X,Y for each one.') x1 = int(input(' Enter x for First point: ')) y1 = int(input(' Enter y for First point: ')) x2 = int(input(' Enter x for Second point: ')) y2 = int(input(' Enter y for Second point: ')) if (x2==x1) or (y2==y1) : print(' Your Points are not valid, (x1,x2) or (y1,y2) can't be equal ') else : draw_triangle(x1, y1, x2, y2)

To Download my Python code (.py) files

Click-Here

By: Ali Radwani

## Python: The Factors

**Python: Factors of the Number N**

This is a short task to get the factors of a given number. The Definition of Factors of N is: The pairs of numbers you multiply to get the N number.

For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4

In this task we will write a Python code to ask the user for a number N then will get all the pairs number that if we multiply them will get that N number, we will store the pairs in a array ‘factors’.

**The Code:**

def factors_of_n(num):

a=1

factors=[]

while a <= num:

if num%a==0:

if (num/a,a) not in factors:

factors.append((a,int(num/a)))

a = a + 1

return factors

#Ask the user for a number

num=int(input(“Enter a number: “))

print(factors_of_n(num))