-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_function3.py
105 lines (85 loc) · 3.23 KB
/
plot_function3.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Import pygame into our program
import pygame
import pygame.freetype
from helpers import *
grid_base = (80, 560)
grid_divs = 80
grid_center = (320, 320)
def convert_coord(x,y):
global grid_center
global grid_divs
return (grid_center[0] + x * grid_divs, grid_center[1] - y * grid_divs)
def draw_function(screen, color, func, domain = None):
if (domain == None):
x = prev_x = -3
prev_y = func(prev_x)
else:
x = prev_x = domain[0]
prev_y = func(prev_x)
while (x < 3):
x = x + 0.025
if (domain != None):
if ((x < domain[0]) or (x > domain[1])):
continue
pygame.draw.aaline(screen, color, convert_coord(prev_x, prev_y), convert_coord(x, func(x)))
prev_x = x
prev_y = func(x)
def draw_function_y(screen, color, func, domain = None):
if (domain == None):
y = prev_y = -3
prev_x = func(prev_y)
else:
y = prev_y = domain[0]
prev_x = func(prev_y)
while (y < 3):
y = y + 0.025
if (domain != None):
if ((y < domain[0]) or (y > domain[1])):
continue
pygame.draw.aaline(screen, color, convert_coord(prev_x, prev_y), convert_coord(func(y), y))
prev_y = y
prev_x = func(y)
def draw_function_t(screen, color, func, domain = None):
t = 0
t_max = 1
t_step = 0.025
if (domain != None):
t = domain[0]
t_max = domain[1]
while (t <= t_max):
v1 = func(t)
v2 = func(t + t_step)
pygame.draw.aaline(screen, color, convert_coord(v1[0],v1[1]), convert_coord(v2[0], v2[1]))
t = t + t_step
# Define a main function, just to keep things nice and tidy
def main():
# Initialize pygame, with the default parameters
pygame.init()
# Define the size/resolution of our window
res = (640, 640)
# Create a window and a display surface
screen = pygame.display.set_mode(res)
# Load a font
axis_labels_font = pygame.freetype.Font("NotoSans-Regular.ttf", 12)
# Game loop, runs forever
while (True):
# Process OS events
for event in pygame.event.get():
# Checks if the user closed the window
if (event.type == pygame.QUIT):
# Exits the application immediately
exit()
# Clears the screen with a very dark blue (0, 0, 20)
screen.fill((0,0,20))
# Draw grid
draw_grid(screen, grid_base[0], grid_base[1], grid_divs, 6, 6, 5, (64, 128, 64, 255), axis_labels_font, [str(i) for i in range(-3, 4)], [str(i) for i in range(-3, 4)])
p1 = (1,1)
p2 = (-2, 2)
draw_point(screen, (0, 255, 255, 255), convert_coord(p1[0], p1[1]), 5, axis_labels_font, "t = 0")
draw_point(screen, (0, 255, 255, 255), convert_coord(p2[0], p2[1]), 5, axis_labels_font, "t = 1")
draw_point(screen, (255, 0, 255, 255), convert_coord(p1[0] + 0.5*(p2[0]-p1[0]), p1[1] + 0.5*(p2[1]-p1[1])), 5, axis_labels_font, "t = 0.5")
draw_function_t(screen, (255, 255, 0, 255), lambda t : (p1[0] + t*(p2[0]-p1[0]), p1[1] + t*(p2[1]-p1[1])))
# Swaps the back and front buffer, effectively displaying what we rendered
pygame.display.flip()
# Run the main function
main()