I want to create a function to get subobject absolute id of fractal object by layer/row and column id. In an fractal object there are repeating subobjects, you can split them into layers. Object with identifier W1 contains 7x G’s, each of G’s again contain 7x N’s etc.
if layer is ...
...3 (rowId=2) then only strings with "N"...
...2 (rowId=1) then only strings with "G"...
...
are counted.
The raw code:
const rawData = [["W1","W2","W3","W4","W5","W6","W7"],
["G1","G2","G3","G4","G5","G6","G7"],
["N1","N2","N3","N4","N5","N6","N7"],
["T1","T2","T3","T4","T5","T6","T7"]]
const selIds=[0,1,2,4] // selected indices of W1 G2 N3 T5 (range per array element: [0-6])
// to calculate absolute fractal "cell" counter
rowId=1 // row index (range: [0-6])
colId=2 // col index (range: [0-6])
I think result can be calculated this way?
result=((7^1*selIds[0])+selIds[1]*7^0)*7^(rowId+1)+3=(0+1)*49+3=52
On click the selIds
change with given rowId
& colIds
to selIds=[0,2,2,4]
; letters of rawData are just arbitrary, because in reality they are like identifiers of planets, solar systems, galaxies and so on
My functions so far:
const getFractalIdByRow = function(rowIndex) {
let result = 0
let factor = 0
let j = 0
for(let i = rowIndex; i >= 0; i--) {
factor += (selIds[j]) * Math.pow(7, i)
j++
}
result = factor*Math.pow(7, rowIndex+1)
return result
}
const getCurrentIndex = function(colIndex, rowIndex) {
let currentIndex = 0
currentIndex += getFractalIdByRow(rowIndex);
currentIndex += colIndex
currentIndex += 1
return currentIndex
}
Examples (layer/row & element/col = absolute id):
W1:G1:N1:T1 = 1
W1:G1:N1 = 1
W4:G5:N6 = 3*7*7 + 4*7 + 6 = 181
W2:G2:N3 = 1*7*7 + 1*7 + 3 = 59
W3:G4:N3 = 2*7*7 + 3*7 + 3 = 122
W2:G2:N3:T4 = 1*7*7*7 + 1*7*7 + 2*7 + 4 = 410
Column id is the number of the last string element split by :
.