villares, Portuguese
@villares@ciberlandia.pt avatar

So nice to see how @rougier used Shiffman's Flocking example as inspiration for the spatial vectorization techniques on https://www.labri.fr/perso/nrougier/from-python-to-numpy/#spatial-vectorization

Such a pity that the Processing Foundation has in all these years abandoned Python... but... now we have and + + has a future!

I'll see if I can remove the matplotlib dependecy and use py5 for boids in the next weeks. I have both a bio-simulation educational demo and a "hey, I'm learning numpy in the context of creative coding" presentation in the coming weeks.

villares,
@villares@ciberlandia.pt avatar

Rougier's book could almost be called "How to Apply to The Nature of Code" 😂​

Just kidding, but it start s with random walkers, and tackles cellular automata, fractals, and agents (flocking boids), all themes found in Shiffman's book!

villares,
@villares@ciberlandia.pt avatar

Almost there, I'm very happy! I have removed the matplotlib dependency from the wonderful @rougier's flocking example code, and added very little for drawing.

https://github.com/villares/sketch-a-day/blob/4a7a0d6d65b35556f7726678a73740e34fab2308/2023/sketch_2023_11_22/sketch_2023_11_22.py

I'm just struggling with "normalizing" the velocity for drawing my triangular boids the same size...
I didn't want to use atan2 to get the heading direction. I thought normalizing velocity would be simpler, but I must be making some silly conceptual mistake. The varying sizes are nice, but I want to understand what I'm doing wrong.

trianglular white "boids" flocking on a black background (the simulation is inspired on murmurations of birds)

davidr,
@davidr@hachyderm.io avatar

@villares @rougier I only skimmed, but the lines with "norm" in them around lines 111-116 look like clamping, not normalization.

villares,
@villares@ciberlandia.pt avatar

@davidr Humm, that's probably clamping alright in the flocking algo, mine is line 134:
norm_vel = flock.velocity / np.linalg.norm(flock.velocity)<br></br>
:-S

davidr,
@davidr@hachyderm.io avatar

@villares aha, I see that now.

I guess perpend_a/_b are the other points of the triangle. but those are just using individual components, which aren't normalized....?

villares, (edited )
@villares@ciberlandia.pt avatar

@davidr yes those will be used to the other points, but heading and perpend_a & _b all 3 come from the norm_vel array...
The perpends (for perpendiculars) swap norm_vel components and change the sign of one of them each.

davidr,
@davidr@hachyderm.io avatar

@villares Right, but individual components of a normalized vector can be any length at all. Or maybe I'm not understanding what '[::-1]' means....

villares,
@villares@ciberlandia.pt avatar

@davidr it is for swapping x,y into y,x (which, I think, won't affect the magnitude) later I'll get -y, x and y, -x, also magnitude preserving. Another thing is that the triangles seem the right proportion/shape, I guess, so it seems to me like just norm_vel is not normalized...

davidr,
@davidr@hachyderm.io avatar

@villares derp, I've been thinking of 3D vectors so I was like...you aren't using all the components.

I agree about the shape apparently meaning it's just the normalization. I wonder if the linalg is doing the wrong direction or something.

numpy makes this hard for no reason, but I usually take row-wise magnitudes like this:

mag = np.sqrt(np.sum(v**2, axis=1))

villares,
@villares@ciberlandia.pt avatar

@davidr I'll try that when I get home, thanks!

villares, (edited )
@villares@ciberlandia.pt avatar

@davidr ok, now with the mag array in hand, shaped (500,) how do I divide the velocity, shaped (500, 2) by mag ?

Also... this seems to work:
norm_vel = flock.velocity / np.linalg.norm(flock.velocity, axis=1).reshape(-1,1)

(but sadly I have no idea of what I'm doing :alarmed:​)

davidr,
@davidr@hachyderm.io avatar

@villares Oh yeah, this is tricksy as well. You need to tell numpy where the broadcastable dimensions are

norm = v/mag[:,None]

villares,
@villares@ciberlandia.pt avatar

@davidr thank you so much for staying with me :)

I guess one day I'll learn some numpy... for now I feel like I'm scratching the surface, it is so hard, yet so powerful, it makes some things so much easier, others not so much :D

davidr,
@davidr@hachyderm.io avatar

@villares Oh, it worked? Great!

I've only scratched the surface as well. I just happened to have been working with lots o' rows of data this summer needing to do similar vector ops.

villares,
@villares@ciberlandia.pt avatar

@davidr very cool!

Both work, the linalg.norm + reshape and your method...

(I'm lucky there seems to be never zero velocity boids, otherwise your method might raise ZeroDivision I guess).

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • tacticalgear
  • Youngstown
  • ngwrru68w68
  • ethstaker
  • slotface
  • PowerRangers
  • hgfsjryuu7
  • khanakhh
  • kavyap
  • tsrsr
  • InstantRegret
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • Leos
  • rosin
  • GTA5RPClips
  • thenastyranch
  • Durango
  • osvaldo12
  • vwfavf
  • cubers
  • everett
  • modclub
  • cisconetworking
  • tester
  • normalnudes
  • anitta
  • All magazines