Skip to content

Commit 598843f

Browse files
committed
screen projection for all
1 parent 32a3ef2 commit 598843f

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

dirichlet/GravityEnergy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
gravity = [0.0, 9.81]
3+
gravity = [0.0, -9.81]
44

55
def val(x, m):
66
sum = 0.0

dirichlet/simulator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
k = 1e3 # spring stiffness
1414
n_seg = 4 # num of segments per side of the square
1515
h = 0.02 # time step size in s
16-
DBC = [0, (n_seg + 1) * n_seg] # fix the left and right top nodes
16+
DBC = [n_seg, (n_seg + 1) * (n_seg + 1) - 1] # fix the left and right top nodes
1717

1818
# initialize simulation
1919
[x, e] = square_mesh.generate(side_len, n_seg) # node positions and edge node indices
@@ -29,10 +29,15 @@
2929
is_DBC = [False] * len(x)
3030
for i in DBC:
3131
is_DBC[i] = True
32-
3332
# simulation with visualization
33+
resolution = np.array([900, 900])
34+
offset = resolution / 2
35+
scale = 200
36+
def screen_projection(x):
37+
return [offset[0] + scale * x[0], resolution[1] - (offset[1] + scale * x[1])]
38+
3439
time_step = 0
35-
screen = pygame.display.set_mode([900, 900])
40+
screen = pygame.display.set_mode(resolution)
3641
running = True
3742
while running:
3843
# run until the user asks to quit
@@ -45,9 +50,9 @@
4550
# fill the background and draw the square
4651
screen.fill((255, 255, 255))
4752
for eI in e:
48-
pygame.draw.aaline(screen, (0, 0, 255), 450 + x[eI[0]] * 200, 450 + x[eI[1]] * 200)
53+
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[0]]), screen_projection(x[eI[1]]))
4954
for xI in x:
50-
pygame.draw.circle(screen, (0, 0, 255), 450 + xI * 200, 0.1 * side_len / n_seg * 200)
55+
pygame.draw.circle(screen, (0, 0, 255), screen_projection(xI), 0.1 * side_len / n_seg * scale)
5156

5257
pygame.display.flip() # flip the display
5358

mass_spring/simulator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
x[i][0] *= initial_stretch
3131

3232
# simulation with visualization
33+
resolution = np.array([900, 900])
34+
offset = resolution / 2
35+
scale = 200
36+
def screen_projection(x):
37+
return [offset[0] + scale * x[0], resolution[1] - (offset[1] + scale * x[1])]
38+
3339
time_step = 0
34-
screen = pygame.display.set_mode([900, 900])
40+
screen = pygame.display.set_mode(resolution)
3541
running = True
3642
while running:
3743
# run until the user asks to quit
@@ -44,9 +50,9 @@
4450
# fill the background and draw the square
4551
screen.fill((255, 255, 255))
4652
for eI in e:
47-
pygame.draw.aaline(screen, (0, 0, 255), 450 + x[eI[0]] * 200, 450 + x[eI[1]] * 200)
53+
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[0]]), screen_projection(x[eI[1]]))
4854
for xI in x:
49-
pygame.draw.circle(screen, (0, 0, 255), 450 + xI * 200, 0.1 * side_len / n_seg * 200)
55+
pygame.draw.circle(screen, (0, 0, 255), screen_projection(xI), 0.1 * side_len / n_seg * scale)
5056

5157
pygame.display.flip() # flip the display
5258

0 commit comments

Comments
 (0)