[Swansea Hackspace] openscad maths

Ceri Clatworthy ceri.clatworthy at gmail.com
Thu Oct 30 15:52:03 GMT 2014


Wanted something like this:


// upside down :(



w = 102; //170;


//data=[1,2,4,8,16,32,1,2,3,4,5,6,7,8,9,0,16];


//data =[32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1];


//data=[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768];


data0 =[

9728 ,

28492 ,

26734 ,

24902 ,

16386 ,

16386 ,

16384 ,

24578 ,

28930 ,

16384 ,

16384 ,

0 ,

0 ,

1124 ,

1126 ,

30284


];


data1=[0 ,

26190 ,

65535 ,

62431 ,

61839 ,

57735 ,

57735 ,

24963 ,

29635 ,

32767 ,

60267 ,

49155 ,

49154 ,

58370 ,

58623 ,

65534


];


data2=[49155 ,

49155 ,

49155 ,

49155 ,

49154 ,

16387 ,

16387 ,

49155 ,

49155 ,

49155 ,

49154 ,

49155 ,

49155 ,

49155 ,

49155 ,

65535 ];


data3=[65535 ,

32767 ,

32766 ,

65535 ,

65535 ,

65534 ,

32766 ,

65535 ,

65535 ,

65535 ,

65535 ,

32767 ,

32639 ,

65534 ,

65535 ,

65534 ];


data4=[31710 ,

32222 ,

31710 ,

31710 ,

31710 ,

31710 ,

31698 ,

29658 ,

31698 ,

28626 ,

28626 ,

28630 ,

28630 ,

28630 ,

28630 ,

28670 ];


//test = chr() -> #0a99f;


CubeSize = 4;



MyCube (0,0,10);


echo (data);



//color ("RED")

//{

// MyCube ( 1, 1, 1);

// MyCube ( 1, 1,16);

// MyCube ( 1,16, 1);

// MyCube ( 1,16,16);

//

// MyCube (16, 1, 1);

// MyCube (16, 1,16);

// MyCube (16,16, 1);

// MyCube (16,16,16);

//

//}


for (k=[0:15])

{

color ("Lime") line (0,data0[k],k);

}


for (k=[0:15])

{

color ("Green") line (1,data1[k],k);

}


// 12 lines ..

for (bod=[2:13])

{

for (k=[0:15])

{

color ("gray") line (bod,data2[k],k);

}

}



for (k=[0:15])

{

color ("Blue") line (14,data3[k],k);

}


for (k=[0:15])

{

color ("DodgerBlue") line (15,data4[k],k);

}


module line (xln,test, zCnt)

{

echo ("data ", test, " count ", zCnt);


 bit15 =(( (test - ( 0 )) / 32768 ) >= 1) ? 32768 :0;

bit14 =(( (test - ( bit15 )) / 16384 ) >= 1) ? 16384 :0;

bit13 =(( (test - ( bit15 + bit14) ) / 8192 ) >= 1) ? 8192 :0;

bit12 =(( (test - ( bit15 + bit14 + bit13) ) / 4096 ) >= 1) ? 4096 :0;

bit11 =(( (test - ( bit15 + bit14 + bit13 + bit12) ) / 2048 ) >= 1) ? 2048
:0;

bit10 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11) ) / 1024 ) >= 1)
? 1024 :0;

bit9 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 )) / 512 )
>= 1) ? 512 :0;

bit8 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 ))
/ 256 ) >= 1) ? 256 :0;

bit7 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 )) / 128 ) >= 1) ? 128 :0;

bit6 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 )) / 64 ) >= 1) ? 64 :0;

bit5 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 )) / 32 ) >= 1) ? 32 :0;

bit4 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 + bit5 )) / 16 ) >= 1) ? 16 :0;

bit3 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 + bit5 + bit4 )) / 8 ) >= 1) ? 8 :0;

bit2 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 + bit5 + bit4 + bit3 )) / 4 ) >= 1) ? 4 :0;

bit1 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 + bit5 + bit4 + bit3 + bit2) ) / 2 ) >= 1) ? 2 :0;

bit0 =(( (test - ( bit15 + bit14 + bit13 + bit12 + bit11 + bit10 + bit9 +
bit8 + bit7 + bit6 + bit5 + bit4 + bit3 + bit2 + bit1 )) / 1 ) >= 1) ? 1 :0;




if (bit15) MyCube (xln, 15,zCnt);

if (bit14) MyCube (xln, 14,zCnt);

if (bit13) MyCube (xln, 13,zCnt);

if (bit12) MyCube (xln, 12,zCnt);

if (bit11) MyCube (xln, 11,zCnt);

if (bit10) MyCube (xln, 10,zCnt);

if (bit9) MyCube (xln, 9,zCnt);

if (bit8) MyCube (xln, 8,zCnt);

if (bit7) MyCube (xln, 7,zCnt);

if (bit6) MyCube (xln, 6,zCnt);

if (bit5) MyCube (xln, 5,zCnt);

if (bit4) MyCube (xln, 4,zCnt);

if (bit3) MyCube (xln, 3,zCnt);

if (bit2) MyCube (xln, 2,zCnt);

if (bit1) MyCube (xln, 1,zCnt);

if (bit0) MyCube (xln, 0,zCnt);


}
























module MyCube (xx,yy,zz)

{

translate ([xx*CubeSize, yy*CubeSize, zz*CubeSize]) cube (CubeSize);

}
















On Thu, Oct 30, 2014 at 1:48 PM, Justin Mitchell <justin at discordia.org.uk>
wrote:

> On Thu, 2014-10-30 at 13:37 +0000, Ceri Clatworthy wrote:
> > Hi, going crazy here ...
> > I need something like:
>
> > test = 102; // 0x66
> > if (test AND 1) {do something usefull @ poz 1};
> > if (test AND 2) {do something usefull @ poz 2};
>
> > e.g. draw the binary representation of a 16 bit number in
> > cubes/spheres !!!
>
> Doesn't look possible,
> there are no bitwise operators that i can see, and
> variables are evaluated at compile time so you cant even run a loop to
> do it the long way around.
>
> Either preprocess it, i.e. write a program that writes openscad.
> or switch to openjscad which is an almost identical syntax except that
> it uses javascript as the language, so can do less noddy operations
>
>
>
> _______________________________________________
> Hackspace mailing list
> Hackspace at swansea.hackspace.org.uk
> http://swansea.hackspace.org.uk/mailman/listinfo/hackspace
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://swansea.hackspace.org.uk/pipermail/hackspace/attachments/20141030/b8719656/attachment.html>


More information about the Hackspace mailing list