Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Converting 2D coordinates to integers?

Author
Message
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 16:51 Edited at: 24th May 2010 18:10
Is there a way to convert 2D coordinates to integer numbers?

For example:
x=0 and y = 0 converted to 0
x=1 and y = 0 converted to 1
x=2 and y = 0 converted to 2
x=3 and y = 0 converted to 3

x=0 and y = 1 converted to 4
x=1 and y = 1 converted to 5
x=2 and y = 1 converted to 6
x=3 and y = 1 converted to 7

Where there is a will, there is a way.
I often edit my posts, that`s who i am
=PRoF=
23
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 24th May 2010 17:01
You could use an array.

dim arrayName(3,3)

arrayName(0,0)=1

Hope that helps.

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 17:11
I need some formula if the grid was 3x3 probably won`t think twice and do it hard way, typing them manually.But my grid is 10x10 i need to type 100 values coresponding to their integers.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 24th May 2010 17:19 Edited at: 24th May 2010 17:30
When I have needed to return x,y in a single digit from a function, I use xy=(x*n)+y (where `n` is maximum possible y value+1) and then divide them back down again. Obviously not good for large numbers, or speed if you're writing a game because of all the multiplication and division, but simple to use.

Edit: Use to seperate again:

Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 24th May 2010 17:43
seems you have this:

val = x + y * 5

"everyone forgets a semi-colon sometimes." - Phaelax
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 18:08
I can`t make it right with this formulas. Zotoaster is simple.sladeiw it`s hard to understand somehow

Zotoaster do you mean var or something in your example?
Because there is comand "val" which return integer from a string.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 24th May 2010 18:22 Edited at: 24th May 2010 18:30
Here is a quick example
This will only work for x,y range 0-99.
Zotoaster is the same (but should be *4), just simplified for the data in your example.

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 18:42
This example produce the sam values in the end as it`s do nothing?
I`m not sure i explain well so there is a shot, i didn`t fill all the nubers but you can see it.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
=PRoF=
23
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 24th May 2010 18:43 Edited at: 24th May 2010 18:43
This is how I do it...



sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 24th May 2010 18:48
Quote: "This example produce the sam values in the end as it`s do nothing?
I`m not sure i explain well so there is a shot, i didn`t fill all the nubers but you can see it."


Yes, the example does nothing
In your picture the only formula you need then is



(As Zotoaster posted)

But what do you want to do with that number afterwards, and do you want to convert it back into coordinates later?
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 18:58
It`s very confusing that everyone give me diferent code...
Are you sure it must be integer = x + y * 9 instead of integer = x + y * 10?
Are you counting x = 0 and y = 0?

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 24th May 2010 19:04
Yes, sorry *10
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th May 2010 19:10
So that`s why i cant understand nothing because one says 5, other says 9, 10...it`s hard to follow like that really

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 24th May 2010 19:52
Quote: "So that`s why i cant understand nothing because one says 5, other says 9, 10...it`s hard to follow like that really"


In your original post you wanted x=0, y=1 to become 4. So the formula for that would be integer = x + y * 4. Why *4? Because it is 1+the maximum x value.

In your second example the max value of x is 9, so the formula becomes integer = x + y * 10.

Sorry for confusing you with the erronous 9

My original examples were for if you wanted to convert that integer back into seperate x,y coordinates. I can't really see any useful function in putting x,y into a single value, but that's just me
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 25th May 2010 14:57
In the first post i write an example to show what i need.On the screen i made is the real grid i`m working basicly you needed to write it like this: var_integer = x + y * maxField

About convering purpose.I need it so when i convert it to pass data in an array and the integer would be used to know which element in the array i need to work with.

I`m still not sure that this is gona work, i need to clear my head and write down the picture as i see it, then code.As i advance with the project it becomes more complicated.I hope not to much, so i can finish it.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 25th May 2010 15:33
Quote: "About convering purpose.I need it so when i convert it to pass data in an array and the integer would be used to know which element in the array i need to work with."


So is there a reason you can't use an array such as `coords(x,y)` like prof suggested? Seems much more straightforward and less maths.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 25th May 2010 15:37
I've been doing something like this with frames for sprites, like using a sprite sheet that might be 10x10 tiles of animated sprites, and the frame number is just a single integer, but it relates to a coordinate using pretty much what Zoto' has.

Say your grid is 5 elements wide...

Frame=(Y*5)+X

So if you take grid point 3x,2y - Frame=(2*5)+3, or 13

Now, knowing the width is 5...

Y=int(Frame/5)
X=Frame-(Y*5)

So if Frame is 13, then Y=2, and 13-(2*5) = 3


I use the Frame to decide the tile position then work out UV coordinates from the calculated X and Y.

So....

Multiply Y by the number of X components, then add X, and you have a unique reference number for that position. Then divide etc if you need to work back and get the original coordinates again.


Health, Ammo, and bacon and eggs!
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 25th May 2010 16:00
That`s very good answer Van, i just started to bang my head on the reverse situation.Now with the formulas you provide it`s simple.Just knowing the Frame i can say what are x and y positions.Repositioning of elements on the grid should be easy now.

Thanks a lot again!

I can propose something, can we open a "Thank you IanM and VanB" thread with a counter in, so we know how many times you and IanM saved our asses?

@sladeiw:
I bet on simplicity.Multidimensional arrays are complicated and hard to understand.I try them once, all become big mess even using 2dimensional one!I needed it just for the same purpose, no thanks.Once someone in the forum asked for more dimensions.He was using some enourmous 5 dimensional array which was eating space like a b****, while actually he needed, 1 custom type with 5 elements and simple UDT array to store the data.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 25th May 2010 16:25 Edited at: 25th May 2010 16:37
Quote: "Once someone in the forum asked for more dimensions."


I saw that post and it was an excessive use of dimensions, however 2 and 3 dimension arrays for 2d and 3d are a natural fit. Even 3 dimensions does start to hurt!

As for reversing your integer back to coordinates, does this look familiar?


The same code as vanb! However he did explain it much more succiently than I could.

edit: Oh and a thanking counter would be a cool idea, Moneysavingexpert forums have a similar thing where you can click to thank someone for their post.
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 25th May 2010 21:42 Edited at: 26th May 2010 10:56
Somehow Van put it in more understandable way to me, it`s not your fault that i`m a bit stupid...

Ok there is a video on youtube, so you can actually see how the system behaive after implementing coordinate convertion.

http://www.youtube.com/watch?v=Uv54l5W972g

It runs with ~1200 FPS normaly but fraps lock it, to 60.

I think it runs pretty smoothly and it`s not heavy at all.It drops FPS when the inventory goes full with 100 sprites(i say sprites because for now it works with images only, later when the Item system is ready it`ll work with real stuff)it goes arround ~1000FPS

To baxslash:
Man if you watch this thread you can see now the sprite system i talked about in action.

I want to thank you all for helping me to make one step ahead to my ultimate goal!

Small step for the comunity, big step for me!

Where there is a will, there is a way.
I often edit my posts, that`s who i am

Login to post a reply

Server time is: 2026-07-25 08:30:06
Your offset time is: 2026-07-25 08:30:06