Sephfox commited on
Commit
0611560
·
verified ·
1 Parent(s): 19ad91e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from bokeh.plotting import figure
4
+ from bokeh.models import ColumnDataSource
5
+ from bokeh.events import Tap
6
+
7
+ Constants
8
+ WIDTH, HEIGHT = 600, 600
9
+ ELASTICITY = 0.3
10
+ DAMPING = 0.7
11
+
12
+ Create touch points
13
+ num_points = 20
14
+ x = np.linspace(50, WIDTH-50, num_points)
15
+ y = np.linspace(50, HEIGHT-50, num_points)
16
+ xx, yy = np.meshgrid(x, y)
17
+ touch_points = np.column_stack((xx.ravel(), yy.ravel()))
18
+
19
+ original_points = touch_points.copy()
20
+ velocities = np.zeros_like(touch_points)
21
+
22
+ source = ColumnDataSource(data=dict(x=touch_points[:, 0], y=touch_points[:, 1]))
23
+
24
+ Set up the Bokeh plot
25
+ p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
26
+ p.circle('x', 'y', size=5, color="navy", alpha=0.5, source=source)
27
+
28
+ Streamlit app
29
+ st.title("Artificial Touch Simulation")
30
+
31
+ Create a Streamlit container for the Bokeh plot
32
+ chart = st.bokeh_chart(p)
33
+
34
+ def update_points():
35
+ global touch_points, velocities
36
+
37
+
38
+ # Apply spring force
39
+ force = (original_points - touch_points) * ELASTICITY
40
+
41
+ # Update velocity
42
+ velocities += force
43
+
44
+ # Apply damping
45
+ velocities *= DAMPING
46
+
47
+ # Update position
48
+ touch_points += velocities
49
+
50
+ # Update Bokeh data source
51
+ source.data = dict(x=touch_points[:, 0], y=touch_points[:, 1])
52
+ def on_tap(event):
53
+ global touch_points, velocities
54
+
55
+
56
+ x, y = event.x, event.y
57
+ distances = np.sqrt(np.sum((touch_points - [x, y])**2, axis=1))
58
+ affected = distances < 30
59
+
60
+ force = (touch_points[affected] - [x, y]) / distances[affected, np.newaxis]
61
+ velocities[affected] -= force * 10
62
+
63
+ st.write(f"Touch at ({x:.2f}, {y:.2f})")
64
+ update_points()
65
+ p.on_event(Tap, on_tap)
66
+
67
+ Main loop
68
+ while True:
69
+ update_points()
70
+ chart.bokeh_chart(p)
71
+ st.experimental_rerun()