Sephfox commited on
Commit
c8eca0d
·
verified ·
1 Parent(s): 4d3fc54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -1,5 +1,6 @@
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
@@ -11,15 +12,19 @@ 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, dtype=np.bool_)
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)
@@ -32,32 +37,39 @@ st.title("Artificial Touch Simulation")
32
  chart = st.bokeh_chart(p)
33
 
34
  def update_points():
35
- global touch_points, velocities
36
 
37
  # Apply spring force
38
- force = (original_points - touch_points) * ELASTICITY
39
-
40
- # Update velocity
41
- velocities += force
 
42
 
43
  # Apply damping
44
- velocities *= DAMPING
 
 
45
 
46
  # Update position
47
- touch_points += velocities
 
 
48
 
49
  # Update Bokeh data source
50
- source.data = dict(x=touch_points[:, 0], y=touch_points[:, 1])
51
 
52
  def on_tap(event):
53
- global touch_points, velocities
54
 
55
  x, y = event.x, event.y
56
- distances = np.sqrt(np.sum((touch_points - [x, y])**2, axis=1))
57
- affected = distances < 30
58
-
59
- force = (touch_points[affected] - [x, y]) / distances[affected, np.newaxis]
60
- velocities[affected] -= force * 10
 
 
61
 
62
  st.write(f"Touch at ({x:.2f}, {y:.2f})")
63
  update_points()
 
1
  import streamlit as st
2
+ from dataclasses import dataclass, field
3
+ from typing import List
4
  from bokeh.plotting import figure
5
  from bokeh.models import ColumnDataSource
6
  from bokeh.events import Tap
 
12
 
13
  # Create touch points
14
  num_points = 20
15
+ touch_points = [(x, y) for x in range(50, WIDTH-50, int((WIDTH-100)/(num_points-1))) for y in range(50, HEIGHT-50, int((HEIGHT-100)/(num_points-1)))]
 
 
 
 
16
  original_points = touch_points.copy()
 
17
 
18
+ @dataclass
19
+ class TouchPoint:
20
+ x: float
21
+ y: float
22
+ velocity_x: float = 0.0
23
+ velocity_y: float = 0.0
24
+
25
+ touch_point_objects: List[TouchPoint] = [TouchPoint(x, y) for x, y in touch_points]
26
+
27
+ source = ColumnDataSource(data=dict(x=[tp.x for tp in touch_point_objects], y=[tp.y for tp in touch_point_objects]))
28
 
29
  # Set up the Bokeh plot
30
  p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
 
37
  chart = st.bokeh_chart(p)
38
 
39
  def update_points():
40
+ global touch_point_objects
41
 
42
  # Apply spring force
43
+ for tp in touch_point_objects:
44
+ force_x = (original_points[touch_point_objects.index(tp)][0] - tp.x) * ELASTICITY
45
+ force_y = (original_points[touch_point_objects.index(tp)][1] - tp.y) * ELASTICITY
46
+ tp.velocity_x += force_x
47
+ tp.velocity_y += force_y
48
 
49
  # Apply damping
50
+ for tp in touch_point_objects:
51
+ tp.velocity_x *= DAMPING
52
+ tp.velocity_y *= DAMPING
53
 
54
  # Update position
55
+ for tp in touch_point_objects:
56
+ tp.x += tp.velocity_x
57
+ tp.y += tp.velocity_y
58
 
59
  # Update Bokeh data source
60
+ source.data = dict(x=[tp.x for tp in touch_point_objects], y=[tp.y for tp in touch_point_objects])
61
 
62
  def on_tap(event):
63
+ global touch_point_objects
64
 
65
  x, y = event.x, event.y
66
+ for tp in touch_point_objects:
67
+ distance = ((tp.x - x)**2 + (tp.y - y)**2)**0.5
68
+ if distance < 30:
69
+ force_x = (tp.x - x) / distance
70
+ force_y = (tp.y - y) / distance
71
+ tp.velocity_x -= force_x * 10
72
+ tp.velocity_y -= force_y * 10
73
 
74
  st.write(f"Touch at ({x:.2f}, {y:.2f})")
75
  update_points()