Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
from typing import List
|
| 4 |
from bokeh.plotting import figure
|
| 5 |
from bokeh.models import ColumnDataSource
|
| 6 |
from bokeh.events import Tap
|
|
@@ -12,19 +11,11 @@ DAMPING = 0.7
|
|
| 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 |
-
|
| 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,39 +28,36 @@ st.title("Artificial Touch Simulation")
|
|
| 37 |
chart = st.bokeh_chart(p)
|
| 38 |
|
| 39 |
def update_points():
|
| 40 |
-
global
|
| 41 |
|
| 42 |
# Apply spring force
|
| 43 |
-
for
|
| 44 |
-
force_x = (original_points[
|
| 45 |
-
force_y = (original_points[
|
| 46 |
-
|
| 47 |
-
tp.velocity_y += force_y
|
| 48 |
|
| 49 |
# Apply damping
|
| 50 |
-
for
|
| 51 |
-
|
| 52 |
-
tp.velocity_y *= DAMPING
|
| 53 |
|
| 54 |
# Update position
|
| 55 |
-
for
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
# Update Bokeh data source
|
| 60 |
-
source.data = dict(x=[
|
| 61 |
|
| 62 |
def on_tap(event):
|
| 63 |
-
global
|
| 64 |
|
| 65 |
x, y = event.x, event.y
|
| 66 |
-
for
|
| 67 |
-
distance = ((
|
| 68 |
if distance < 30:
|
| 69 |
-
force_x = (
|
| 70 |
-
force_y = (
|
| 71 |
-
|
| 72 |
-
tp.velocity_y -= force_y * 10
|
| 73 |
|
| 74 |
st.write(f"Touch at ({x:.2f}, {y:.2f})")
|
| 75 |
update_points()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from typing import List, Tuple
|
|
|
|
| 3 |
from bokeh.plotting import figure
|
| 4 |
from bokeh.models import ColumnDataSource
|
| 5 |
from bokeh.events import Tap
|
|
|
|
| 11 |
|
| 12 |
# Create touch points
|
| 13 |
num_points = 20
|
| 14 |
+
touch_points: List[Tuple[float, float]] = [(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)))]
|
| 15 |
original_points = touch_points.copy()
|
| 16 |
+
velocities: List[Tuple[float, float]] = [(0.0, 0.0)] * len(touch_points)
|
| 17 |
|
| 18 |
+
source = ColumnDataSource(data=dict(x=[x for x, y in touch_points], y=[y for x, y in touch_points]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Set up the Bokeh plot
|
| 21 |
p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
|
|
|
|
| 28 |
chart = st.bokeh_chart(p)
|
| 29 |
|
| 30 |
def update_points():
|
| 31 |
+
global touch_points, velocities
|
| 32 |
|
| 33 |
# Apply spring force
|
| 34 |
+
for i, (x, y) in enumerate(touch_points):
|
| 35 |
+
force_x = (original_points[i][0] - x) * ELASTICITY
|
| 36 |
+
force_y = (original_points[i][1] - y) * ELASTICITY
|
| 37 |
+
velocities[i] = (velocities[i][0] + force_x, velocities[i][1] + force_y)
|
|
|
|
| 38 |
|
| 39 |
# Apply damping
|
| 40 |
+
for i, (vx, vy) in enumerate(velocities):
|
| 41 |
+
velocities[i] = (vx * DAMPING, vy * DAMPING)
|
|
|
|
| 42 |
|
| 43 |
# Update position
|
| 44 |
+
for i, (x, y) in enumerate(touch_points):
|
| 45 |
+
vx, vy = velocities[i]
|
| 46 |
+
touch_points[i] = (x + vx, y + vy)
|
| 47 |
|
| 48 |
# Update Bokeh data source
|
| 49 |
+
source.data = dict(x=[x for x, y in touch_points], y=[y for x, y in touch_points])
|
| 50 |
|
| 51 |
def on_tap(event):
|
| 52 |
+
global touch_points, velocities
|
| 53 |
|
| 54 |
x, y = event.x, event.y
|
| 55 |
+
for i, (tx, ty) in enumerate(touch_points):
|
| 56 |
+
distance = ((tx - x)**2 + (ty - y)**2)**0.5
|
| 57 |
if distance < 30:
|
| 58 |
+
force_x = (tx - x) / distance
|
| 59 |
+
force_y = (ty - y) / distance
|
| 60 |
+
velocities[i] = (velocities[i][0] - force_x * 10, velocities[i][1] - force_y * 10)
|
|
|
|
| 61 |
|
| 62 |
st.write(f"Touch at ({x:.2f}, {y:.2f})")
|
| 63 |
update_points()
|