FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
py5-python-and-java/processing-mode/example3/example3.py at main · py5coding/py5-python-and-java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
py5coding
/
py5-python-and-java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
py5-python-and-java
/
processing-mode
/
example3
/
example3.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (40 loc) · 1.79 KB
Breadcrumbs
py5-python-and-java
/
processing-mode
/
example3
/
example3.py
Copy path
File metadata and controls
55 lines (40 loc) · 1.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Flocking + Clustering
# original Flocking Sketch by Daniel Shiffman.
#
# In this adaptation of Dan Shiffman's Flocking example, the boids are
# repeatedly clustered using the KMeans algorithm from scikit-learn. Each Boid
# will ignore Boids in other clusters, but each Boid will also change cluster
# membership over time. The cluster labels are used to give the boids in each
# cluster a unique color.
#
# An implementation of Craig Reynold's Boids program to simulate
# the flocking behavior of birds. Each boid steers itself based on
# rules of avoidance, alignment, and coherence.
#
# Click the mouse to add a new boid.
import
traceback
from
jpype
import
JClass
from
sklearn
.
cluster
import
KMeans
import
py5_tools
import
py5
NUMBER_OF_CLUSTERS
=
8
cluster_centers
=
None
def
cluster_count
():
# the number of clusters should be set in one place for both the Python and Java code
return
NUMBER_OF_CLUSTERS
def
cluster_boids
(
boid_locations
):
global
cluster_centers
try
:
init_arg
=
'k-means++'
if
cluster_centers
is
None
else
cluster_centers
kmeans
=
KMeans
(
n_clusters
=
NUMBER_OF_CLUSTERS
,
random_state
=
0
,
init
=
init_arg
,
n_init
=
1
).
fit
(
boid_locations
)
# save the cluster centers to initialize clustering in the next invocation
# this keeps cluster labels stable from one call to the next
cluster_centers
=
kmeans
.
cluster_centers_
.
copy
()
return
kmeans
.
labels_
except
Exception
as
e
:
traceback
.
print_exc
()
return
JClass
(
'java.lang.RuntimeException'
)(
str
(
e
))
# py5_tools.register_processing_mode_key("cluster_boids", cluster_boids)
# py5_tools.register_processing_mode_key("cluster_count", cluster_count)
# run the sketch in processing mode, specifying the Java class to instantiate
py5
.
run_sketch
(
jclassname
=
'test.Example3Sketch'
)
Back
|
FazBrowse Home
|
New Git URL