Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.label import Label
- from numpy import linspace as linspace
- from numpy import arange as arange
- import sympy
- from kivy.app import App
- from kivy.lang import Builder
- from kivy.uix.screenmanager import ScreenManager, Screen
- import kivy
- i = 1
- def plot(m, b):
- fig = plt.figure()
- ax = fig.add_subplot(111)
- x = linspace(-20, 20, 100)
- y = m * x + b
- plt.plot(x, y, '-r', 'bo', label='graph')
- for i in range(4):
- print(f"{i * -1}, {formatted(m * (i * -1) + b)}")
- for i in range(4):
- print(f"{i}, {formatted(m * i + b)}")
- ax.set_box_aspect(1)
- plt.xlabel('x', color='red')
- plt.ylabel('y', color='red')
- ax.axhline(linewidth=1, color="black")
- ax.axvline(linewidth=1, color="black")
- ax.set_xticks(arange(-50, 50, 1))
- ax.set_yticks(arange(-50, 50, 1))
- plt.xlim(lim * -1, lim)
- plt.ylim(lim * -1, lim)
- plt.grid()
- plt.show()
- def formatted(f):
- return float(format(f, '.2f').rstrip('0').rstrip('.'))
- class MenuWindow(Screen):
- def change_lim(self):
- global lim
- lim = self.ids.slider.value
- def graph_toggle(self):
- global i
- if i == 1:
- i = 0
- self.ids.graphbutton.text = 'Graph OFF'
- self.ids.graphbutton.background_color = 1, 0, 0, 1
- else:
- i = 1
- self.ids.graphbutton.text = 'Graph ON'
- self.ids.graphbutton.background_color = 0, 1, 0, 1
- class TwoWindow(Screen):
- def find2p(self):
- global m
- global b
- global bs
- point1 = self.ids.pone.text
- point2 = self.ids.ptwo.text
- x1, y1 = point1.split(' ') # Point 1
- x2, y2 = point2.split(' ') # ..... 2
- x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
- m1 = y2 - y1
- m2 = x2 - x1
- if m1 and m2 != 0:
- m = m1 / m2
- else:
- m = 0
- b = sympy.symbols('b')
- # Find b
- b_out = (sympy.solve(y1 - x1 * m + b))
- b_out[0] *= -1
- # init new b
- b = float(b_out[0])
- if not b.is_integer():
- b = self.root.formatted(b)
- else:
- b = int(b)
- if not m.is_integer():
- m = self.rootformatted(m)
- else:
- m = int(m)
- print(f"m = {m}, b = {b}")
- if b >= 0:
- bs = f'+{b}'
- else:
- bs = b
- ResultWindow().displayresult()
- if i == 1:
- plot(m, b)
- class OneWindow(Screen):
- def find1p(self):
- global m
- global b
- global bs
- point1 = self.ids.pone.text
- x1, y1 = point1.split(' ') # Point 1
- x1, y1 = float(x1), float(y1)
- m = float(self.ids.mvalue.text)
- b = sympy.symbols('b')
- # Find b
- b_out = (sympy.solve(y1 - x1 * m + b))
- b_out[0] *= -1
- # init new b
- b = float(b_out[0])
- if not b.is_integer():
- b = self.root.formatted(b)
- else:
- b = int(b)
- if not m.is_integer():
- m = self.rootformatted(m)
- else:
- m = int(m)
- print(f"m = {m}, b = {b}")
- if b >= 0:
- bs = f'+{b}'
- else:
- bs = b
- ResultWindow().displayresult()
- if i == 1:
- plot(m, b)
- class GraphWindow(Screen):
- def graph(self):
- global m
- global b
- global bs
- m = int(self.ids.mvalue.text)
- b = int(self.ids.bvalue.text)
- if i == 1:
- plot(m, b)
- class ResultWindow(Screen):
- def displayresult(self):
- out = f"out: \n y = {m}x{bs}"
- print(out)
- self.ids.out.text = out
- class WindowManager(ScreenManager):
- pass
- kv = Builder.load_file("graph.kv")
- class graphApp(App):
- def build(self):
- return kv
- graphApp().run()
- WindowManager:
- MenuWindow:
- TwoWindow:
- OneWindow:
- GraphWindow:
- ResultWindow:
- <TwoWindow>:
- name: "2p"
- BoxLayout:
- orientation: "vertical"
- padding: 4
- size_hint: 1, 1
- spacing: "10dp"
- Label:
- text: 'Seperate x and y with space'
- text_size: self.width, None
- size_hint: 1, .4
- TextInput:
- hint_text: 'Point one:'
- size_hint: 1, .5
- id: pone
- TextInput:
- hint_text: 'Point two:'
- size_hint: 1, .5
- id: ptwo
- BoxLayout:
- orientation: "vertical"
- size_hint: 1, 5
- Button:
- size_hint: 1, None
- height: "60dp"
- text: "Done"
- on_press:
- app.root.current = "result"
- root.find2p()
- <OneWindow>:
- name: "1p"
- BoxLayout:
- orientation: "vertical"
- padding: 4
- size_hint: 1, 1
- spacing: "10dp"
- Label:
- text: 'Seperate x and y with space'
- text_size: self.width, None
- size_hint: 1, .4
- TextInput:
- hint_text: 'Point one:'
- size_hint: 1, .5
- id: pone
- TextInput:
- hint_text: 'M Value:'
- size_hint: 1, .5
- id: mvalue
- BoxLayout:
- orientation: "vertical"
- size_hint: 1, 5
- Button:
- size_hint: 1, None
- height: "60dp"
- text: "Done"
- on_press:
- app.root.current = "result"
- root.find1p()
- <GraphWindow>:
- name: "gp"
- BoxLayout:
- orientation: "vertical"
- padding: 4
- size_hint: 1, 1
- spacing: "10dp"
- Label:
- text: 'Graph a linear function with m & b'
- text_size: self.width, None
- size_hint: 1, .4
- TextInput:
- hint_text: 'M Value:'
- size_hint: 1, .5
- id: mvalue
- TextInput:
- hint_text: 'B Value:'
- size_hint: 1, .5
- id: bvalue
- BoxLayout:
- orientation: "vertical"
- size_hint: 1, 5
- Button:
- size_hint: 1, None
- height: "60dp"
- text: "Done"
- on_press:
- app.root.current = "result"
- root.graph()
- <ResultWindow>:
- name: "result"
- BoxLayout:
- orientation: "vertical"
- Button:
- id: out
- text: 'error'
- <MenuWindow>:
- name: "menu"
- GridLayout:
- padding: 2
- cols: 2
- rows: 2
- Button:
- text: "2p"
- on_press:
- app.root.current = "2p"
- root.change_lim()
- Button:
- text: "1p&M"
- on_press:
- app.root.current = "1p"
- root.change_lim()
- Button:
- text: "M & B"
- on_press:
- app.root.current = "gp"
- root.change_lim()
- BoxLayout:
- orientation: "vertical"
- BoxLayout:
- Label:
- id: label
- text: str(int(slider.value))
- size_hint: .2, 1
- Slider:
- min: 3
- max: 16
- #step: 1
- value: 7
- id: slider
- Button:
- text: 'Graph ON'
- id: graphbutton
- background_color: 0,1,0,1
- on_release:
- root.graph_toggle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement