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 / This might be a math question for the gurus.

Author
Message
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Mar 2011 17:23 Edited at: 11th Mar 2011 19:25
There's likely a simple answer, and I'm thinking it might be possible with a matrix. (the math kind, not the terrain kind..) I'm not sure because I haven't fully wrapped my head around how those work yet.

I have a set of fixed numbers in a 4 x 4 configuration.



I have a 3 x 3 set of numbers that I want to change by shifting the numbers in the above 4 x 4 inside it. Such as,



moving it one to the left, with wrapping,



I can of course do it with a long drawn out list of if/then statements, but I figured I'd ask if there's an easier way.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Mar 2011 19:06
I'm not entirely clear what you want to do. Do you just want to shift the main 4x4 block then extract a given 3x3 sub-block from it (such as the top left sub-block in your example)?
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Mar 2011 19:24
Probably should have put that in a code block to preserve the spacing.
(done)

Yes, basically I want to pull a set of 3 x 3 from the 4 x 4 with an offset, wrapping to the other side of the 4 x 4 if the 3 x 3 goes off one side.

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 11th Mar 2011 19:54 Edited at: 11th Mar 2011 19:57
Maybe use a 8x8 array, and shift by +5 when you hit zero going left, minus 4 when you hit 2 going right, etc.? Or is that what you meant by lots of IF tests?



KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Mar 2011 20:12
That's close to the solution I came up with so far.

I set up the array like this,


I set the desired center position A(5) = 1 for example, and then with this code find that position in the array and extrapolate the rest.


It's working fine, I just figured if there was an easier way I'd ask.

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 11th Mar 2011 20:27 Edited at: 11th Mar 2011 20:44
Yah, there's probably some kind of formula, but I have zero math skilz. You know who really gets into that? Neuro Fuzzy is made of math.

EDIT: IanM's matrix plugins has a ROTATE ARRAY command that does exactly what you want, but I think it only works on single dimension arrays.

KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Mar 2011 20:45
Yeah, if I had paid as much attention to the school work as I did to the girls in high school I wouldn't be asking questions like this..

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Mar 2011 21:07
You don't need an 8x8 array to do this - given the starting position, loop through the coordinates you want to copy, using the modulus operator to wrap back in on the left when you hit the right-hand side of the source array.

Probably easier to show you:


Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 11th Mar 2011 21:11

I would use the modulo command.

Most anything with wrapping boundary conditions should use the modulo command. For example, if you were using degrees, taking the value modulo 360 would clamp the result between (-360,360). If the number is below zero, you can add an extra condition to add 360 to the number (-x°=360°-x°)

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 11th Mar 2011 21:19 Edited at: 11th Mar 2011 21:20
@IanM - I'm looking at it, but I still don't get it. The array is 3x3 but you only are assigning values to a(i). How does the second dimension get filled? Seems like magic to me

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Mar 2011 21:22
Not magic at all - just the way that DBPro arrays were designed. You can treat any multi-dimensional array as if it were a 1D array, except that you can't use the insert/delete commands on them.

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 11th Mar 2011 21:34 Edited at: 11th Mar 2011 21:34
Quote: "You can treat any multi-dimensional array as if it were a 1D array"


Now that REALLY messes up my head. I'll have to play around with that for sure.

Here's my no-math, no-modulo solution for the original problem... brute force and not pretty.



KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Mar 2011 21:45
I knew someone would have an answer.

The nice thing is I wasn't that far off, just needed to know that one command that was missing...

Thanks guys.

Login to post a reply

Server time is: 2026-07-15 03:40:05
Your offset time is: 2026-07-15 03:40:05