Spaces:
Sleeping
Sleeping
File size: 1,800 Bytes
0611560 6e1639c 0611560 6e1639c 0611560 a91f6dd 0611560 6e1639c 0611560 6e1639c 0611560 6e1639c 0611560 6e1639c 0611560 6e1639c 0611560 6e1639c |
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 |
import streamlit as st
import numpy as np
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.events import Tap
# Constants
WIDTH, HEIGHT = 600, 600
ELASTICITY = 0.3
DAMPING = 0.7
# Create touch points
num_points = 20
x = np.linspace(50, WIDTH-50, num_points)
y = np.linspace(50, HEIGHT-50, num_points)
xx, yy = np.meshgrid(x, y)
touch_points = np.column_stack((xx.ravel(), yy.ravel()))
original_points = touch_points.copy()
velocities = np.zeros_like(touch_points, dtype=np.bool_)
source = ColumnDataSource(data=dict(x=touch_points[:, 0], y=touch_points[:, 1]))
# Set up the Bokeh plot
p = figure(width=WIDTH, height=HEIGHT, tools="", toolbar_location=None)
p.circle('x', 'y', size=5, color="navy", alpha=0.5, source=source)
# Streamlit app
st.title("Artificial Touch Simulation")
# Create a Streamlit container for the Bokeh plot
chart = st.bokeh_chart(p)
def update_points():
global touch_points, velocities
# Apply spring force
force = (original_points - touch_points) * ELASTICITY
# Update velocity
velocities += force
# Apply damping
velocities *= DAMPING
# Update position
touch_points += velocities
# Update Bokeh data source
source.data = dict(x=touch_points[:, 0], y=touch_points[:, 1])
def on_tap(event):
global touch_points, velocities
x, y = event.x, event.y
distances = np.sqrt(np.sum((touch_points - [x, y])**2, axis=1))
affected = distances < 30
force = (touch_points[affected] - [x, y]) / distances[affected, np.newaxis]
velocities[affected] -= force * 10
st.write(f"Touch at ({x:.2f}, {y:.2f})")
update_points()
p.on_event(Tap, on_tap)
while True:
update_points()
chart.bokeh_chart(p)
st.experimental_rerun() |