Loading...
Logo
Processing Forum

linear value to PVector

in General Discussion  •  Other  •  9 months ago  


hello all,

I am looking for something similar to this:
The equivalent statement to get(x, y) using pixels[] is pixels[y*width+x].

only in 3D and other way round.

Let's assum I have a cube 3x3x3 and
value going up from 0 to 27.

I want to assign each cell in the cube to a number.
So I need a function value-> PVector

e.g. 23 -> 2,3,2 or so.

I Tried %  3 but no success

Can Anyone help?

Thanks!

Chrisir     


Replies(2)

Actually the values will go from 0 - 26 inclusive as you have 27 cubes

Lets assume that we are using an XYZ coordinate system then

XYZ > cell number
x * 9 + y * 3 + z

and

cell number > XYZ
z = n %3;
y = (n/3) % 3
x = (n/9);

To prove that it works run the sketch below it performs both calculations (one per line) and shows that the transformations are reversible.

Output from sketch
n >>  x  y  z   >>   n
0     0  0  0        0
1     0  0  1        1
2     0  0  2        2
3     0  1  0        3
4     0  1  1        4
5     0  1  2        5
6     0  2  0        6
7     0  2  1        7
8     0  2  2        8
9     1  0  0        9
10    1  0  1        10
11    1  0  2        11
12    1  1  0        12
13    1  1  1        13
14    1  1  2        14
15    1  2  0        15
16    1  2  1        16
17    1  2  2        17
18    2  0  0        18
19    2  0  1        19
20    2  0  2        20
21    2  1  0        21
22    2  1  1        22
23    2  1  2        23
24    2  2  0        24
25    2  2  1        25
26    2  2  2        26


Copy code
  1. int xp, yp, zp, n;

  2. void setup() {
  3.   println("n >> \tx  y  z   >>\tn");
  4.   for (int x = 0; x < 3; x++)
  5.     for (int y = 0; y < 3; y++)
  6.       for (int z = 0; z < 3; z++) {
  7.         // XYZ >> n
  8.         n = x * 9 + y * 3 + z;
  9.         // n >> XYZ
  10.         zp = n % 3;
  11.         yp = (n/3) % 3;
  12.         xp = (n/9);
  13.         println(n + "\t" + xp + "  " + yp + "  " + zp +"\t\t" + n);
  14.       }
  15. }



thank you!

that's it!

great.