Pi
Quote: notyouraveragejoe wrote on Tue, 19 February 2008 19:21
----------------------------------------------------
> What I didn't explain was the purpose of this, sorry I should have explained
> this earlier. I'm putting this code into an arduino to control the PWM pins
> so it needs tobe something I can convert into simple C coding.
>
still comes down to mapping right? or am i not understanding you?
i'm not sure if you need ints or floats for the 0-255 range. in case you need floats take out the int() parts.
hope this helps.
correction(/359 instead of 360):
maybe this is a theoretical question, but I think you have to use
zmap 0 256 0 360
otherwise with 0 255 0 359 the last step (from 255 back to 0(=256))
would be unproportionally smaller in the mapped values (from 359 to 360/0).
therefore: expr int(((360.*($i1/256.)+90.)%360.)/360.*256.).
or am I wrong???
marius.
Klaas-Jan Govaart wrote:
> correction(/359 instead of 360):
>
> #P origin 418 0;
> #P window setfont "Sans Serif" 9.;
> #P number 31 173 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P window linecount 1;
> #P newex 31 151 254 196617 expr int(((359.*($i1/255.)+90.)%360.)/359.*255.);
> #P number 31 129 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 31 107 254 196617 expr int(((359.*($i1/255.)+60.)%360.)/359.*255.);
> #P number 31 85 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 31 63 254 196617 expr int(((359.*($i1/255.)+30.)%360.)/359.*255.);
> #P number 31 41 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P hidden connect 0 0 1 0;
> #P connect 1 0 2 0;
> #P hidden connect 0 0 3 0;
> #P connect 3 0 4 0;
> #P hidden connect 0 0 5 0;
> #P connect 5 0 6 0;
>
Quote: marius.schebella wrote on Tue, 19 February 2008 21:46
----------------------------------------------------
> maybe this is a theoretical question, but I think you have to use
> zmap 0 256 0 360
Hi Marius,
I think you have to divide/multiply by the maximum value in the new range.
Or when using zmap: old minimum to new minimum and old maximum to new maximum.
Anybody please correct me if I'm wrong.
Regards,
kjg
This is my take at it:
Quote: kjg wrote on Wed, 20 February 2008 00:25
----------------------------------------------------
> Quote: marius.schebella wrote on Tue, 19 February 2008 21:46
> ----------------------------------------------------
> > maybe this is a theoretical question, but I think you have to use
> > zmap 0 256 0 360
>
> Hi Marius,
>
> I think you have to divide/multiply by the maximum value in the new range.
sorry, that is not correct:
you have to divide by the maximum value in the old range to scale to the range 0-1, then multiply by the maximum value in the new range to get the range 0-newmax.
Or in one go: multiply by newmax/oldmax
as I said before, it is only a very small differenced, but the maximum
values are 360 and 256.
in a distance from 0 to 1 your highest integer number is 0, in a
distance from 0 to 2, it is 1...
0-----------------------(1)
0-----------1-----------(2)
0-------1-------2-------(3)
0-----1-----2-----3-----(4)
0---1---2---3---4---5---(6)
0--1--2--3--4--5--6--7--(8)
0-1-2-3-4-5-6-7-8-9-0-1-(12)
and if you have a circle circumference from 0 to 2pi and divide it into
360 parts then they are labeled 0/360*2pi, 1/360*2pi, 2/360*2pi,
3/360*2pi, ..., 357/360*2pi, 358/360*2pi, 359/360*2pi.
but 359 is not the maximum, it is 360, the last step is back to
360/360*2pi or 0/360*2pi. if you divide by 359 or 255 then the last
distance (to close the circle has a wrong value).
marius.
Klaas-Jan Govaart wrote:
> Quote: marius.schebella wrote on Tue, 19 February 2008 21:46
> ----------------------------------------------------
>> maybe this is a theoretical question, but I think you have to use
>> zmap 0 256 0 360
>
> Hi Marius,
>
> I think you have to divide/multiply by the maximum value in the new range.
>
> Or when using zmap: old minimum to new minimum and old maximum to new maximum.
>
> Anybody please correct me if I'm wrong.
>
> Regards,
> kjg
>
>
> This is my take at it:
>
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P newex 202 246 43 196617 % 360.;
> #P comment 134 541 67 196617 or use floats;
> #P newex 131 246 29 196617 t b f;
> #P comment 317 192 149 196617 offset in degrees (-360 - 360);
> #P comment 342 209 20 196617 ==;
> #P message 85 46 26 196617 513;
> #P message 35 24 14 196617 0;
> #P message 52 40 26 196617 256;
> #P comment 244 248 221 196617 no negative values for modulo..;
> #P number 360 209 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P number 307 209 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P user hslider 162 207 18 128 721 1 -360 0;
> #P newex 162 246 40 196617 + 360.;
> #P newex 232 184 72 196617 loadmess 390;
> #P comment 218 398 242 196617 multiply by maximum value in new range (0-255);
> #P flonum 54 541 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 143 416 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 49 416 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P window setfont "Sans Serif" 10.;
> #P newex 54 514 272 196618 expr ($i1 /255. * 359. + $f2) %360. /359. *255.;
> #P window setfont "Sans Serif" 9.;
> #P number 8 517 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P number 116 88 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P number 79 88 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P window setfont "Sans Serif" 10.;
> #P newex 8 492 326 196618 expr int(($i1 /255. * 359. + $f2) %360. /359. *255. +0.5);
> #P window setfont "Sans Serif" 9.;
> #P newex 129 434 34 196617 + 0.5;
> #P newex 35 434 34 196617 + 0.5;
> #P comment 212 457 217 196617 round and convert back to ints;
> #P number 129 470 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P number 35 470 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 129 452 27 196617 i;
> #P newex 35 452 27 196617 i;
> #P comment 212 340 141 196617 divide by the maximum value;
> #P newex 129 395 41 196617 * 255.;
> #P newex 35 395 84 196617 zmap 0 1 0 255.;
> #P comment 212 360 131 196617 range of 256 values (0-1.);
> #P flonum 143 359 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 129 337 40 196617 / 359.;
> #P flonum 49 359 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 35 337 84 196617 zmap 0 359 0 1.;
> #P comment 212 91 289 196617 % to make 256 wrap back to 0 - range of 256 values (0-255);
> #P newex 35 86 40 196617 % 256;
> #P flonum 143 295 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 49 295 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P comment 212 272 221 196617 modulo to make 360 wrap back to 0 (or offset);
> #P newex 35 272 43 196617 % 360.;
> #P newex 129 272 43 196617 % 360.;
> #P comment 171 230 149 196617 offset in degrees (-360 - 360);
> #P newex 35 207 27 196617 + 0.;
> #P newex 129 207 27 196617 + 0.;
> #P comment 212 163 242 196617 multiply by maximum value in new range (0-359);
> #P flonum 143 184 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P flonum 49 184 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 129 161 41 196617 * 359.;
> #P newex 35 161 87 196617 zmap 0 1. 0 359.;
> #P comment 212 135 131 196617 range of 256 values (0-1.);
> #P comment 212 114 277 196617 divide by the maximum value (not by the number of values);
> #P flonum 143 134 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 129 112 40 196617 / 255.;
> #P flonum 49 134 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 35 112 84 196617 zmap 0 255 0 1.;
> #P user hslider 35 64 18 128 513 1 0 0;
> #P comment 212 69 138 196617 range of 513 values (0-512);
> #P comment 57 23 193 196617 value number 0 256 or 513 (0 , 0 and 0);
> #P connect 59 0 39 0;
> #P fasten 2 0 39 0 40 84 13 84;
> #P connect 39 0 42 0;
> #P connect 56 0 2 0;
> #P connect 55 0 2 0;
> #P connect 54 0 2 0;
> #P connect 2 0 22 0;
> #P connect 22 0 3 0;
> #P connect 3 0 9 0;
> #P connect 59 0 15 0;
> #P connect 9 0 15 0;
> #P connect 15 0 18 0;
> #P connect 18 0 24 0;
> #P connect 24 0 29 0;
> #P connect 29 0 37 0;
> #P connect 37 0 32 0;
> #P connect 32 0 34 0;
> #P connect 3 0 4 0;
> #P connect 9 0 11 0;
> #P connect 18 0 20 0;
> #P connect 24 0 25 0;
> #P connect 29 0 44 0;
> #P connect 59 1 15 1;
> #P connect 59 0 43 0;
> #P fasten 2 0 43 0 40 84 22 84 22 434 59 434;
> #P connect 43 0 46 0;
> #P connect 2 0 40 0;
> #P connect 22 0 41 0;
> #P connect 22 0 5 0;
> #P connect 5 0 10 0;
> #P connect 59 0 14 0;
> #P connect 10 0 14 0;
> #P connect 14 0 17 0;
> #P connect 17 0 26 0;
> #P connect 26 0 30 0;
> #P connect 30 0 38 0;
> #P connect 38 0 33 0;
> #P connect 33 0 35 0;
> #P connect 49 0 59 0;
> #P connect 5 0 6 0;
> #P connect 10 0 12 0;
> #P connect 17 0 21 0;
> #P connect 26 0 27 0;
> #P connect 30 0 45 0;
> #P connect 59 1 14 1;
> #P connect 48 0 50 0;
> #P connect 50 0 49 0;
> #P hidden connect 49 0 61 0;
> #P hidden connect 50 0 51 0;
> #P connect 59 1 43 1;
> #P connect 59 1 39 1;
> #P hidden connect 61 0 52 0;
>
>
>
Quote: marius.schebella wrote on Wed, 20 February 2008 02:23
----------------------------------------------------
> as I said before, it is only a very small differenced, but the maximum
> values are 360 and 256.
> in a distance from 0 to 1 your highest integer number is 0, in a
> distance from 0 to 2, it is 1...
>
> 0-----------------------(1)
> 0-----------1-----------(2)
> 0-------1-------2-------(3)
> 0-----1-----2-----3-----(4)
> 0---1---2---3---4---5---(6)
> 0--1--2--3--4--5--6--7--(8)
> 0-1-2-3-4-5-6-7-8-9-0-1-(12)
>
> and if you have a circle circumference from 0 to 2pi and divide it into
> 360 parts then they are labeled 0/360*2pi, 1/360*2pi, 2/360*2pi,
> 3/360*2pi, ..., 357/360*2pi, 358/360*2pi, 359/360*2pi.
> but 359 is not the maximum, it is 360, the last step is back to
> 360/360*2pi or 0/360*2pi. if you divide by 359 or 255 then the last
> distance (to close the circle has a wrong value).
> marius.
>
Marius you convinced me. The light went on... :)
Just to make sure I don't get confused again, do you agree with this general "formula" for scaling to a new range?
multiply by num_steps_new/num_steps_old
or, in this case:
0-255 * (360/256) > add offset > * (256/360) > 0-255
Thank you for the lesson! I've been confused about this, thanks for clearing it up.
Klaas-Jan
+ updated patch: