HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Drawing random smooth lines contained within a square

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomcontainedwithindrawingsquaresmoothlines

Problem

I'm trying to write a matlab function that creates random, smooth trajectories in a square of finite side length. Here is my current attempt at such a procedure:

```
function [] = drawroutes( SideLength, v, t)
%DRAWROUTES Summary of this function goes here
% Detailed explanation goes here

%Some parameters intended to help help keep the particles in the box
RandAccel=.01;
ConservAccel=0;
speedlimit=.1;
G=10^(-8);
%
%Initialize Matrices
Ax=zeros(v,10*t);
Ay=Ax;
vx=Ax;
vy=Ax;
x=Ax;
y=Ax;
sx=zeros(v,1);
sy=zeros(v,1);
%
%Define initial position in square
x(:,1)=SideLength.15ones(v,1)+(SideLength.7)rand(v,1);
y(:,1)=SideLength.15ones(v,1)+(SideLength.7)rand(v,1);
%
for i=2:10*t
%Measure minimum particle distance component wise from boundary
%for each vehicle
BorderGravX=[abs(SideLength*ones(v,1)-x(:,i-1)),abs(x(:,i-1))]';
BorderGravY=[abs(SideLength*ones(v,1)-y(:,i-1)),abs(y(:,i-1))]';
rx=min(BorderGravX)';
ry=min(BorderGravY)';
%
%Set the sign of the repulsive force
for k=1:v
if x(k,i)<.5*SideLength
sx(k)=1;
else
sx(k)=-1;
end
if y(k,i)<.5*SideLength
sy(k)=1;
else
sy(k)=-1;
end
end
%
%Calculate Acceleration w/ random "nudge" and repulive force
Ax(:,i)=ConservAccelAx(:,i-1)+RandAccel(rand(v,1)-.5ones(v,1))+sxG./rx.^2;
Ay(:,i)=ConservAccelAy(:,i-1)+RandAccel(rand(v,1)-.5ones(v,1))+syG./ry.^2;
%
%Ad hoc method of trying to slow down particles from jumping outside of
%feasible region
for h=1:v
if abs(vx(h,i-1)+Ax(h,i))<speedlimit
vx(h,i)=vx(h,i-1)+Ax(h,i);
elseif (vx(h,i-1)+Ax(h,i))<-speedlimit
vx(h,i)=-speedlimit;
else
vx(h,i)=speedlimit;
end
end
for h=1:v
if abs(vy(h,i-1)+Ay(h,i))<speedlimit
vy(h,i)=vy(h,i-1)+Ay(h,i);
elseif (vy(h,i-1)+Ay(h,i))<-speedlimit
vy(h,i)=-speedlimit;
else
vy(h,i)=speedlimit;
end
end
%
%Update position
x(:,i)=x(:,i-1)+(vx(:,i-1)+vx(:,i))/2;
y(:,i)=y(:,i-1)+(v

Solution

Random walks "wander all around" by definition. So you have to twist the odds of the walk staying in the area you want.

It would be much simpler if you'd just make an attractor point at the center, that would move each point infinitesimally (some floating point value between 0 to 1 or sqrt(2) pixels) towards itself at each step.

Another option would be to make the edges exponentially hard to reach. I'm thinking of an Exponential_map with a really steep slope at the edges and otherwise near linear curve. also see Gamma curve. So essentially map an "infinite" area to the restricted space.

Third option would be to think about how magnetism works. I mean, a magnet can't be any more magnetized to a certain direction after all it's particles are magnetized towards that direction. And the magnetization takes exponentially stronger field all the way towards that state. I've written a "tape compression" algorithm with Numpy using that idea.

Context

StackExchange Code Review Q#15996, answer score: 4

Revisions (0)

No revisions yet.