Advertisement
Guest User

Untitled

a guest
Jan 27th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from kivy.uix.boxlayout import BoxLayout
  3. from kivy.uix.label import Label
  4. from numpy import linspace as linspace
  5. from numpy import arange as arange
  6. import sympy
  7. from kivy.app import App
  8. from kivy.lang import Builder
  9. from kivy.uix.screenmanager import ScreenManager, Screen
  10. import kivy
  11.  
  12. i = 1
  13.  
  14.  
  15. def plot(m, b):
  16. fig = plt.figure()
  17. ax = fig.add_subplot(111)
  18.  
  19. x = linspace(-20, 20, 100)
  20. y = m * x + b
  21.  
  22. plt.plot(x, y, '-r', 'bo', label='graph')
  23.  
  24. for i in range(4):
  25. print(f"{i * -1}, {formatted(m * (i * -1) + b)}")
  26. for i in range(4):
  27. print(f"{i}, {formatted(m * i + b)}")
  28.  
  29. ax.set_box_aspect(1)
  30.  
  31. plt.xlabel('x', color='red')
  32. plt.ylabel('y', color='red')
  33.  
  34. ax.axhline(linewidth=1, color="black")
  35. ax.axvline(linewidth=1, color="black")
  36.  
  37. ax.set_xticks(arange(-50, 50, 1))
  38. ax.set_yticks(arange(-50, 50, 1))
  39.  
  40. plt.xlim(lim * -1, lim)
  41. plt.ylim(lim * -1, lim)
  42.  
  43. plt.grid()
  44.  
  45. plt.show()
  46.  
  47.  
  48. def formatted(f):
  49. return float(format(f, '.2f').rstrip('0').rstrip('.'))
  50.  
  51. class MenuWindow(Screen):
  52. def change_lim(self):
  53. global lim
  54. lim = self.ids.slider.value
  55.  
  56. def graph_toggle(self):
  57. global i
  58. if i == 1:
  59. i = 0
  60. self.ids.graphbutton.text = 'Graph OFF'
  61. self.ids.graphbutton.background_color = 1, 0, 0, 1
  62. else:
  63. i = 1
  64. self.ids.graphbutton.text = 'Graph ON'
  65. self.ids.graphbutton.background_color = 0, 1, 0, 1
  66.  
  67. class TwoWindow(Screen):
  68. def find2p(self):
  69. global m
  70. global b
  71. global bs
  72. point1 = self.ids.pone.text
  73. point2 = self.ids.ptwo.text
  74.  
  75. x1, y1 = point1.split(' ') # Point 1
  76. x2, y2 = point2.split(' ') # ..... 2
  77.  
  78. x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
  79.  
  80. m1 = y2 - y1
  81. m2 = x2 - x1
  82.  
  83. if m1 and m2 != 0:
  84. m = m1 / m2
  85. else:
  86. m = 0
  87.  
  88. b = sympy.symbols('b')
  89.  
  90. # Find b
  91. b_out = (sympy.solve(y1 - x1 * m + b))
  92.  
  93. b_out[0] *= -1
  94.  
  95. # init new b
  96. b = float(b_out[0])
  97.  
  98. if not b.is_integer():
  99. b = self.root.formatted(b)
  100. else:
  101. b = int(b)
  102. if not m.is_integer():
  103. m = self.rootformatted(m)
  104. else:
  105. m = int(m)
  106.  
  107. print(f"m = {m}, b = {b}")
  108.  
  109. if b >= 0:
  110. bs = f'+{b}'
  111. else:
  112. bs = b
  113.  
  114. ResultWindow().displayresult()
  115.  
  116. if i == 1:
  117. plot(m, b)
  118.  
  119.  
  120. class OneWindow(Screen):
  121. def find1p(self):
  122. global m
  123. global b
  124. global bs
  125. point1 = self.ids.pone.text
  126.  
  127. x1, y1 = point1.split(' ') # Point 1
  128.  
  129. x1, y1 = float(x1), float(y1)
  130.  
  131. m = float(self.ids.mvalue.text)
  132.  
  133. b = sympy.symbols('b')
  134.  
  135. # Find b
  136. b_out = (sympy.solve(y1 - x1 * m + b))
  137.  
  138. b_out[0] *= -1
  139.  
  140. # init new b
  141. b = float(b_out[0])
  142.  
  143. if not b.is_integer():
  144. b = self.root.formatted(b)
  145. else:
  146. b = int(b)
  147. if not m.is_integer():
  148. m = self.rootformatted(m)
  149. else:
  150. m = int(m)
  151.  
  152. print(f"m = {m}, b = {b}")
  153.  
  154. if b >= 0:
  155. bs = f'+{b}'
  156. else:
  157. bs = b
  158.  
  159. ResultWindow().displayresult()
  160. if i == 1:
  161. plot(m, b)
  162.  
  163.  
  164. class GraphWindow(Screen):
  165. def graph(self):
  166. global m
  167. global b
  168. global bs
  169. m = int(self.ids.mvalue.text)
  170. b = int(self.ids.bvalue.text)
  171.  
  172. if i == 1:
  173. plot(m, b)
  174.  
  175.  
  176. class ResultWindow(Screen):
  177. def displayresult(self):
  178. out = f"out: \n y = {m}x{bs}"
  179. print(out)
  180. self.ids.out.text = out
  181.  
  182.  
  183. class WindowManager(ScreenManager):
  184. pass
  185.  
  186.  
  187. kv = Builder.load_file("graph.kv")
  188.  
  189.  
  190. class graphApp(App):
  191. def build(self):
  192. return kv
  193.  
  194.  
  195. graphApp().run()
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. WindowManager:
  220. MenuWindow:
  221. TwoWindow:
  222. OneWindow:
  223. GraphWindow:
  224. ResultWindow:
  225.  
  226. <TwoWindow>:
  227. name: "2p"
  228.  
  229. BoxLayout:
  230. orientation: "vertical"
  231. padding: 4
  232. size_hint: 1, 1
  233. spacing: "10dp"
  234.  
  235. Label:
  236. text: 'Seperate x and y with space'
  237. text_size: self.width, None
  238. size_hint: 1, .4
  239.  
  240. TextInput:
  241. hint_text: 'Point one:'
  242. size_hint: 1, .5
  243. id: pone
  244. TextInput:
  245. hint_text: 'Point two:'
  246. size_hint: 1, .5
  247. id: ptwo
  248.  
  249. BoxLayout:
  250. orientation: "vertical"
  251. size_hint: 1, 5
  252.  
  253. Button:
  254. size_hint: 1, None
  255. height: "60dp"
  256. text: "Done"
  257. on_press:
  258. app.root.current = "result"
  259. root.find2p()
  260.  
  261. <OneWindow>:
  262. name: "1p"
  263.  
  264. BoxLayout:
  265. orientation: "vertical"
  266. padding: 4
  267. size_hint: 1, 1
  268. spacing: "10dp"
  269.  
  270. Label:
  271. text: 'Seperate x and y with space'
  272. text_size: self.width, None
  273. size_hint: 1, .4
  274.  
  275. TextInput:
  276. hint_text: 'Point one:'
  277. size_hint: 1, .5
  278. id: pone
  279. TextInput:
  280. hint_text: 'M Value:'
  281. size_hint: 1, .5
  282. id: mvalue
  283.  
  284. BoxLayout:
  285. orientation: "vertical"
  286. size_hint: 1, 5
  287.  
  288. Button:
  289. size_hint: 1, None
  290. height: "60dp"
  291. text: "Done"
  292. on_press:
  293. app.root.current = "result"
  294. root.find1p()
  295.  
  296. <GraphWindow>:
  297. name: "gp"
  298.  
  299. BoxLayout:
  300. orientation: "vertical"
  301. padding: 4
  302. size_hint: 1, 1
  303. spacing: "10dp"
  304.  
  305. Label:
  306. text: 'Graph a linear function with m & b'
  307. text_size: self.width, None
  308. size_hint: 1, .4
  309.  
  310. TextInput:
  311. hint_text: 'M Value:'
  312. size_hint: 1, .5
  313. id: mvalue
  314. TextInput:
  315. hint_text: 'B Value:'
  316. size_hint: 1, .5
  317. id: bvalue
  318.  
  319. BoxLayout:
  320. orientation: "vertical"
  321. size_hint: 1, 5
  322.  
  323. Button:
  324. size_hint: 1, None
  325. height: "60dp"
  326. text: "Done"
  327. on_press:
  328. app.root.current = "result"
  329. root.graph()
  330.  
  331. <ResultWindow>:
  332. name: "result"
  333.  
  334. BoxLayout:
  335. orientation: "vertical"
  336.  
  337. Button:
  338. id: out
  339. text: 'error'
  340.  
  341. <MenuWindow>:
  342. name: "menu"
  343.  
  344. GridLayout:
  345. padding: 2
  346. cols: 2
  347. rows: 2
  348.  
  349. Button:
  350. text: "2p"
  351. on_press:
  352. app.root.current = "2p"
  353. root.change_lim()
  354.  
  355. Button:
  356. text: "1p&M"
  357. on_press:
  358. app.root.current = "1p"
  359. root.change_lim()
  360.  
  361. Button:
  362. text: "M & B"
  363. on_press:
  364. app.root.current = "gp"
  365. root.change_lim()
  366.  
  367. BoxLayout:
  368.  
  369. orientation: "vertical"
  370.  
  371. BoxLayout:
  372. Label:
  373. id: label
  374. text: str(int(slider.value))
  375. size_hint: .2, 1
  376. Slider:
  377. min: 3
  378. max: 16
  379. #step: 1
  380. value: 7
  381.  
  382. id: slider
  383.  
  384. Button:
  385. text: 'Graph ON'
  386. id: graphbutton
  387. background_color: 0,1,0,1
  388. on_release:
  389. root.graph_toggle()
  390.  
  391.  
  392.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement