Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created May 27, 2017 12:38
Show Gist options
  • Save mentix02/674368551b18e734b24f30bc30d197b9 to your computer and use it in GitHub Desktop.
Save mentix02/674368551b18e734b24f30bc30d197b9 to your computer and use it in GitHub Desktop.
Some math functions in python
#!/usr/bin/python
import matplotlib.pyplot as plt
def find_distance(x1, y1, x2, y2):
d = ((x2-x1)**2+(y2-y1)**2)**0.5 # the algorithm
return d
def plot_graph(x,y):
try:
# take input from user
x1 = float(x[0])
y1 = float(x[1])
# display first co-ordinate
first_cor = "(" + str(int(x1)) + "," + str(int(y1)) + ")"
print "First co-ordinate : " + first_cor
# take input from user
x2 = float(y[0])
y2 = float(y[1])
# display second co-ordinate
second_cor = "(" + str(int(x2)) + "," + str(int(y2)) + ")"
print "Second co-ordinate : " + second_cor
# calculate and display distance
distance = find_distance(x1,y1,x2,y2)
print("Distance between " + first_cor + " and " \
+ second_cor + " is " + str(distance) + " units!")
# graph
print "Plotting graph..."
plt.plot([x1, x2], [y1, y2], 'r-*')
plt.title('Distance between ' + str(first_cor) + " & " + str(second_cor))
# plt.scale(1,2,3)
plt.grid(True)
plt.text(((x1+x2)/2)+1., ((y1+y2)/2)+1., str(distance)) # needs to be better
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
except KeyboardInterrupt:
print "\nBye!"
quit()
def trial_linear_equations():
operators = ["+", "-", "/", "*"] # list all operators
def find_variable(eq):
equation = list(eq)
for term in equation:
for operator in operators:
if term is operator:
equation.remove(term)
for term in equation:
try:
int(term)
except ValueError:
return term
equation = "25x+1" # define primary linear equation
mod_equation = list(equation) # convert it for easier solving
for term in mod_equation:
# print "Term: " , term
for operator in operators:
# print "Operator: " , operator
if term is operator:
index_of_term = mod_equation.index(term)
first_half = mod_equation[:index_of_term]
second_half = mod_equation[index_of_term:]
second_half.remove(term)
var = find_variable(equation)
first_half.remove(var)
first_half = ''.join(first_half)
# ''.join(first_half)
second_half = ''.join(reduce(lambda x, y: x*y, second_half))
print float(first_half)
print var
print float(second_half)
new_var = 1
# while True:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment