## Position item in an array larger than 3D

General FreeBASIC programming questions.
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hello everyone

This is a little experiment and possibly useful for 3D graphics with only whole numbers.
This example shows the vertices (X, Y, Z) of a 3D cube.

1 = 1,1,1
2 = 1,10,1
3 = 10,1,1
4 = 10,10,1
5 = 1,1,10
6 = 1,10,10
7 = 10,1,10
8 = 10,10,10

With this system we need to create a 3D array (x, y, z) = 24 items
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
dim as integer a,b,c,ro,co,le, p

cls
ro = 10
co = 10
le = 10

input "row position = ";a
input "col position = ";b
input "level position = ";c

p = ((a - 1) * co) + b + ((ro * co) * (c - 1))'position

print "Position = ";p
sleep
end

'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Now the same vertices, obtained with this application:

1 = 1
2 = 10
3 = 91
4 = 100
5 = 901
6 = 910
7 = 991
8 = 1000

As we see, in this case, we only need to create a 1D array (P) = 8 elements

To regain the coordinates of the vertices, we just have to break down the numbers of previous positions and get back the coordinates of the vertices.

':::::::::::::::::::::::::::::::::::::::::::::::::::::

dim as integer c,co,d,f,h,l,p,r

cls
'input "qty blocs = ";d
input "qty rows (rows <= cols) = "; r
input "qty cols (cols >=rows) = ";c
input "qty lev = ";l
input "find position = ";p
h = l

'calculate dimension/block
if p mod (r*c*l) <> 0 then
d = int(p/(r*c*l))+1
else
d = int(p/(r*c*l))
end if
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

'calculate level
l = int(p/(r*c))+1
if p mod (r*c) = 0 then l = l-1
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

'calculate col
if p < c then co = p
if p >= c then co = p mod c
if p mod c = 0 then co = c
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

'calculate row
f = ((p-co-((r*c)*(l-1)))/c)+1
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

print "blo = ";d;" row = ";f; " col = ";co; " lev = ";l-((d-1)*h)
sleep
end

'::::::::::::::::::::::::::::::::::::::::::::::::::::

This same example applied to a sphere, we would need only 1/3 of a 3D array.

I wish you like this idea.!

Regards
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hi all:

I've done some work on this subject.
Written in Spanish.
If someone wants to see, is in this link:

http://es.slideshare.net/ZkrouhnZyx/mat ... on-bloques

Regards
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hi all:

I've done some work on this subject.

Written in English.

If someone wants to see, is in this link:

http://es.slideshare.net/ZkrouhnZyx/math-blocks

Regard
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hi, all:

Question:

Any number in few DIM's all we can transform?

(R = Rows - C = Cols - L = Level)

Example: 27
27 = 3L * 3R * 3C
27 = 1R * 27C
27 = 3R * 9C

Decomposing the number:
27 | 3
9 | 3
3 | 3
1 |

27 = 3 ^ 3

Anyone know any way to solve this question?
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

HI, all:

Well, I have not found time to find how all forms (dimensions) possible.
But if the number of combinations.
Just finding all divisors of the number searching and combining them with themselves.
We can only use its divisors, if we use other numbers, the result is wrong.

Prime numbers = 1

Demonstration:

Code: Select all

`DIM AS INTEGER J,N,ZCLSINPUT "Number = ";ZCLSJ = -1FOR N = Z TO 1 STEP - 1IF Z MOD N = 0 THEN PRINT N;:J = J + 1NEXT NPRINTPRINT "Combinations = ";J SLEEPEND`

Example: 729
729 243 81 27 9 3 1
Combinations = 6

Example: 27
27 9 3 1
Combinations = 3

Example: 25
25 5 1
Combinations = 2

Example: 29 <<< >>> prime number = 1
29 1
Combinations = 1

Similarly, the value of the rows, columns, levels, dimensions, will be all the divisors of the number to find.

Regards
Last edited by lrcvs on Jun 15, 2015 19:49, edited 2 times in total.
Tourist Trap
Posts: 2792
Joined: Jun 02, 2015 16:24

### Re: Position item in an array larger than 3D

Hi,

I dont understand what is made for? Can you make a demo of drawing some 3D with your notation?
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hi,Tourist Trap:

Ok!
It's simple and complex to explain.
Look to understand this before we made up here or what makes this calculation.

It is seeking the position of an element in any Dimension / Array (2D, 3D, 4D, 5D, 6D, .....)
Similarly, we can transform any Dimension / 1D Array.

On the other hand, knowing the position, we can look at that Block / Dimension / Array, Row, Column, Level, it is an element of an array.

See:

http://es.slideshare.net/ZkrouhnZyx/math-blocks

Regards

Excuse my English!
Tourist Trap
Posts: 2792
Joined: Jun 02, 2015 16:24

### Re: Position item in an array larger than 3D

I've watched the slideshow, that was my first action. It doesn't do 3D :)

However from your last explanation I tend to think that you want to change a "n"D array in a 1D one. Is it true?

Why not chaining the dimensions so like this :

[quote]2D version

x1 y1
x2 y2
...
xn yn

1D version

x1 x2 .... xn y1 y2 .... yn[/quote]

But I still dont see why you would do that. Maybe I'm totally wrong.
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

HI,

I've watched the slideshow, That was my first action. It does not do 3D :) >>> Ok, Not 3D Graphics

However from your last explanation I Tend To Think That You want to change to "n" D array in a 1D one. Is it true? >>> Is it true

Maybe I'm totally wrong. >>> No, you is not wrong, but may not understand the whole program / project.

Imagine you, you have a block of 3 * 3 * 3 elements (3 rows, 3 columns, 3 levels).

We know that "position number" the element at row 2, column 2 and Level 2, is if we start counting from the top down and from left to right, we see that the search item is located at position 14.

On the other hand, we can reverse the problem, knowing the position and composition of the block.
Tourist Trap
Posts: 2792
Joined: Jun 02, 2015 16:24

### Re: Position item in an array larger than 3D

lrcvs wrote:On the other hand, we can reverse the problem, knowing the position and composition of the block.

Are you sure that we can? How do you know if 7 = 3 + 3 + 1 , or 7 = 2 + 2 + 3 for instance ?

Code: Select all

`I   I     I   I    I   II   I     I   I    I   II   I     I x I    I   I I   I     I x I    I   II x I     I x I    I   II x I     I x I    I   II x I     I x I    I x I\_x_/     \_x_/    \_x_/    == 12I   I     I   I    I   II   I     I   I    I x II   I     I   I    I x I I   I     I   I    I x II   I     I x I    I x II   I     I x I    I x II   I     I x I    I x I\_x_/     \_x_/    \_x_/    == 12`
lrcvs
Posts: 569
Joined: Mar 06, 2008 19:27
Location: Spain

### Re: Position item in an array larger than 3D

Hi,

Are you sure That we can? Yes !!!
How do you know if: 7 = 3 + 3 + 1, or 7 = 2 + 2 + 3 for instance? <<< Is to Error!

Is very simple...

1) The blocks are composed of: Rows * Columns * Levels * Dims

Tell me how many rows, many columns and how many levels does your block and then tell me what position is the element you want to search for?

2) you indicate drawing I do not understand, give me a block data (rows, columns and levels), tell me that row, column and want to put an item level to know his position.
dodicat
Posts: 6084
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Position item in an array larger than 3D

Hi lrcvs
I remember doing the offsets for multi-dimensional arrays earlier in this thread (a while ago).
The offsets (for pointer purposes) start at 0, your conversion to a single array (starts at one) will be offsets+1.
So it is easy going from an element in an array to your single dimension substitute element number.

Going the other way, well, that is what you are working on here?

I have done a brute force kinda way, what method would you use to get the 5 dimensional co-ordinates of a given position in the single dimension equivalent array.

Code: Select all

`'================= Offset macros ==============================#define ub ubound#define lb lbound#define _r(arr,n) (ub(arr,n)-lb(arr,n)+1)#define d1(a,n1) (n1-lb(a,1))#define d2(a,n1,n2) (_r(a,2)*d1(a,n1)+(n2-lb(a,2)))#define d3(a,n1,n2,n3) (_r(a,3)*d2(a,n1,n2)+(n3-lb(a,3)))#define d4(a,n1,n2,n3,n4) (_r(a,4)*d3(a,n1,n2,n3)+(n4-lb(a,4)))#define d5(a,n1,n2,n3,n4,n5) (_r(a,5)*d4(a,n1,n2,n3,n4)+(n5-lb(a,5)))#define d6(a,n1,n2,n3,n4,n5,n6) (_r(a,6)*d5(a,n1,n2,n3,n4,n5)+(n6-lb(a,6)))#define d7(a,n1,n2,n3,n4,n5,n6,n7) (_r(a,7)*d6(a,n1,n2,n3,n4,n5,n6)+(n7-lb(a,7)))#define d8(a,n1,n2,n3,n4,n5,n6,n7,n8) (_r(a,8)*d7(a,n1,n2,n3,n4,n5,n6,n7)+(n8-lb(a,8)))'========================================================dim shared as integer i(1 to 30,1 to 30,1 to 30,1 to 30,1 to 30)print d5(i,30,30,30,30,30)+1,"Size of one dimensional array" 'added 1 to the max offsetdim as integer position=21000000print "position required = ";position'for position 21000000 the offset will be one lessfor x1 as integer=1 to 30    for x2 as integer=1 to 30        for x3 as integer=1 to 30            for x4 as integer=1 to 30                for x5 as integer=1 to 30            if d5(i,x1,x2,x3,x4,x5)=position-1 then print "Element ";x1,x2,x3,x4,x5:goto fin            next:next:next:next:next            fin:sleep  `
Tourist Trap
Posts: 2792
Joined: Jun 02, 2015 16:24

### Re: Position item in an array larger than 3D

lrcvs wrote:2) you indicate drawing I do not understand, give me a block data (rows, columns and levels), tell me that row, column and want to put an item level to know his position.

Code: Select all

`dim as Integer someArray(1 to 10, 1 to 10, 1 to 10)someArray(4, 8, 2) = 999 `

If the position in 3D style is 4, 8, 2 , with a value of 999 stored there... What is the position in your counting system?
fxm
Posts: 9408
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Position item in an array larger than 3D

Does it will last long (position=372)!
position = @someArray(4, 8, 2) - @someArray(Lbound(someArray, 1), Lbound(someArray, 2), Lbound(someArray, 3)) + 1
dodicat
Posts: 6084
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Position item in an array larger than 3D

Two methods to retrieve the index.
The second method same as fxm.

Code: Select all

`'================= Offset macros ==============================#define ub ubound#define lb lbound#define _r(arr,n) (ub(arr,n)-lb(arr,n)+1)#define d1(a,n1) (n1-lb(a,1))#define d2(a,n1,n2) (_r(a,2)*d1(a,n1)+(n2-lb(a,2)))#define d3(a,n1,n2,n3) (_r(a,3)*d2(a,n1,n2)+(n3-lb(a,3)))#define d4(a,n1,n2,n3,n4) (_r(a,4)*d3(a,n1,n2,n3)+(n4-lb(a,4)))#define d5(a,n1,n2,n3,n4,n5) (_r(a,5)*d4(a,n1,n2,n3,n4)+(n5-lb(a,5)))#define d6(a,n1,n2,n3,n4,n5,n6) (_r(a,6)*d5(a,n1,n2,n3,n4,n5)+(n6-lb(a,6)))#define d7(a,n1,n2,n3,n4,n5,n6,n7) (_r(a,7)*d6(a,n1,n2,n3,n4,n5,n6)+(n7-lb(a,7)))#define d8(a,n1,n2,n3,n4,n5,n6,n7,n8) (_r(a,8)*d7(a,n1,n2,n3,n4,n5,n6,n7)+(n8-lb(a,8)))'========================================================dim shared as integer i(1 to 10,1 to 10,1 to 10)print d3(i,10,10,10)+1," = Size of one dimensional array" 'added 1 to the max offsetprint "position of array( 4, 8, 2)=  ";1+d3(i,4,8,2)'=============================================================='method 2sub Show(a() as integer,x as integer,y as integer,z as integer)    var k1=lbound(a,1),k2=lbound(a,2),k3=lbound(a,3)    var u1=ubound(a,1),u2=ubound(a,2),u3=ubound(a,3)     print @a(u1,u2,u3)-@a(k1,k2,k3)+1;" = Size of one dimensional array"       print "position of array(";x;",";y;",";z;")=  "; @a(x,y,z)-@a(k1,k2,k3)+1end subprintShow(i(),4,8,2)sleep  `