forked from HissenGneg/Plot-2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (45 loc) · 1.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import pygame
from colors import colorDictionary as Color
from curve import Curve
from window import Window
pygame.init()
width, height = 640, 480
x_interval = (-5, 15)
y_interval = (-5, 15)
window = Window(width, height, x_interval, y_interval)
# Generates the graph
def generate_graph():
# pygame.draw.lines(window, Color['black'], False, generate_pixels(), 1)
first_curve = Curve(lambda x: x, Color['black'], 1)
second_curve = Curve(lambda x: x ** 2, Color['red'], 1)
third_curve = Curve(lambda x: 4 * (x ** 3) + 3 * (x ** 2) + 2 * x + 1, Color['green'], 1)
window.draw_curve(first_curve)
window.draw_curve(second_curve)
window.draw_curve(third_curve)
def parse_function(string_input):
# Assuming string_input is a math function with one variable, x
# 'eval()' interprets a string like actual code
# https://stackoverflow.com/questions/40828921/parsing-a-string-input-into-a-lambda-function-python
# https://stackoverflow.com/questions/9383740/what-does-pythons-eval-do
# Lambda functions are useful when writing small anonymous functions
# They're come in handy when writing math expressions as writing math
# like a normal function wouldn't make much sense
# https://www.w3schools.com/python/python_lambda.asp
math_function = lambda x: eval(string_input)
return math_function
# Main function
def main():
while True:
window.generate_grid()
window.generate_axis()
window.generate_number_lines()
# print(window.new_point_to_pixel(10, None))
# print(window.new_point_to_pixel(None, 0))
generate_graph()
pygame.display.update()
# Makes sure the program doesn't crash when quitting
for events in pygame.event.get():
if events.type == pygame.QUIT:
sys.exit(0)
main()