Mathematical Patterns (example)

60 views
Skip to first unread message

Symbroson Development

unread,
Aug 21, 2016, 4:43:11 AM8/21/16
to DroidScript
Here is my first version of mathematical patterns. For now I've 3 patterns: 5-gon Sierpinsky, Mandelbrot and the Lengton ant
(I couldn't remove SetPixelMode(true) on LengtonAnt because otherwise GetPixelColor() returns "0,0,0" every time)
If anyone knows a other pattern he would like to implement please tell me (and an example code if he can ;)
This should be a sample app so please tell me where I should comment more
Thank you and best regards,
Alex

ps: I switched my account. I made this one for Development only

w= app.GetScreenWidth(); h= app.GetScreenHeight();

//5-gon coordinates
tra= [[0.5,0.2],[0.02,0.4],[0.2,0.7],[0.8,0.7],[0.94,0.4]]
//start position
x=0.5; y=0.2;

rot=1;
ant=[w/4,h/4];

function OnStart() {

lay = app.CreateLayout( "linear" );
layHoriz= app.CreateLayout( "Linear", "Horizontal" );

sierpinsky= app.CreateImage( null, 0.5,0.5 );
sierpinsky.SetAutoUpdate( false );
sierpinsky.SetPaintColor( "yellow" );
layHoriz.AddChild( sierpinsky );

lengton= app.CreateImage( null,0.5,0.5 );
lengton.Scale( 8,8 )
lengton.SetPixelMode( true );
layHoriz.AddChild( lengton );
lay.AddChild( layHoriz );

mandelbrot=app.CreateImage(null,1,1);
mandelbrot.SetPaintColor( "blue" );
mandelbrot.SetAutoUpdate(false);
lay.AddChild(mandelbrot);

app.AddLayout( lay );

drawMandel();
setInterval( drawLeng );
}

//5-gon Sierpinski
function drawSierp() {
//Get random corner
ran= Math.round(Math.random()*4)

//new p= distance( p, corner ) /2
x+= (tra[ran][0]-x)*0.6
y+= (tra[ran][1]-y)*0.6
sierpinsky.DrawPoint( x,y );
}


//Lengton ant
function drawLeng() {
if( lengton.GetPixelColor(ant[0],ant[1] )>"0,0,0" )
{color="#ff000000"; rot--}
else {color="green"; rot++}

if (rot>4) rot=1; if (rot<1) rot=4;

if(ant=="") return
lengton.SetPaintColor( color );
lengton.DrawPoint( ant[0],ant[1] );

switch(rot) {
case 1:ant[0]++; break; case 3:ant[0]--; break;
case 2:ant[1]++; break; case 4:ant[1]--; break;
}
}

//Mandelbrot pattern
function drawMandel(){
var xs,ys,xc,yc,x,y,x0,mag,acc=100

for(ys=160; ys>=0; ys-=0.5){ yc=ys/160;
for(xs=80;xs<500;xs+=0.5){ xc=xs/160-2.5;
x=xc; y=yc; iter=0; mag=0;

while(mag<=4 && iter<acc){
x0=x;
x=sqr(x)-sqr(y)+xc;
y=2*x0*y+yc;
mag=sqr(x)+sqr(y);
iter++
}

if(iter==acc) {
mandelbrot.DrawPoint(2*xs/w-0.3,2*(240-ys)/h-0.1);
mandelbrot.DrawPoint(2*xs/w-0.3,2*(240+ys)/h-0.1);
drawSierp();
}
}
//draw other patterns
drawLeng();
mandelbrot.Update();
if( !(ys%5) ) updSierp()
}
}


function sqr(v){return Math.pow(v,2) }
function mod(v) {return (v*1+1)%5 }
function updSierp() {sierpinsky.Update() }

Mathematical Patterns.js

Symbroson Development

unread,
Aug 21, 2016, 4:50:39 AM8/21/16
to DroidScript
Heres an example how it looks at the end (ant will never stop)
Screenshot_2016-08-21-10-47-04.png

Steve Garman

unread,
Aug 21, 2016, 7:26:14 AM8/21/16
to DroidScript
The ant may never stop but on my phone it vanishes off the top left of the tile and I'm pretty sure it's never coming back.
Screenshot_20160821-122143.png

Symbroson Development

unread,
Aug 21, 2016, 7:29:40 AM8/21/16
to DroidScript
Yes^^
I tried to set a border in my ant app but it failed. So I haven't set a border and thus is the final picture.

Ttalk

unread,
Aug 21, 2016, 8:11:13 AM8/21/16
to DroidScript
what is ur phone ?samsung ?...

Symbroson Development

unread,
Aug 21, 2016, 8:13:19 AM8/21/16
to DroidScript
Mine?- yes

Symbroson Development

unread,
Aug 21, 2016, 8:19:50 AM8/21/16
to DroidScript
A question- I want to change an array using a temporal variable but I forgot how to make it working.
as example:

arr=[0,1,2]

temp=arr
for(i=0; i<3; i++)
arr[i]=temp[2-i]
alert(arr)


note that arr is global

Steve Garman

unread,
Aug 21, 2016, 8:30:00 AM8/21/16
to DroidScript
temp=arr does not make a new array, just a second reference to the first one.

Try this instead

arr=[0,1,2]

temp=arr.slice(0)

Symbroson Development

unread,
Aug 21, 2016, 9:00:05 AM8/21/16
to DroidScript
Thanks. It works. But I have a bit complex array. I'll make a new thread. I thougt it could work

DJ Saddo

unread,
Aug 27, 2016, 6:01:15 AM8/27/16
to DroidScript
Love it :)
Reply all
Reply to author
Forward
0 new messages