#x, y, and z offsets from a single number

1 messages Β· Page 1 of 1 (latest)

spiral flax
#

I start off with a single block, which is the origin. If i input the number 0, it should return the origin coords, (0, 0, 0)

If i enter the numbers 1-26, it'll go over a list of blocks that surround the origin. For example:
1 -> 1, 0, 0
2 -> 1, 0, -1
3 -> 1, 0 1
4 -> 1, -1, 0
... and so on until all blocks around the origin have been covered

Then, when I reach number 27, it should start searching the next "layer". For example:
27 -> 2, 0, 0
28 -> 2, 0, -1
29 -> 2, 0, -2
.. and so on

I want this to repeat until it searches all blocks within a 20x20x20 cube around the origin

#

Here's what I mean by layer

#

If I have a 10x10x10 cube

vocal mist
#

ok

spiral flax
#

each "layer" would be considered each inner cube within it. For example, layer 1 is the 1x1x1 cube in the middle. Layer 2 is the 3x3x3 cube surrounding the middle cube

#

and so on

vocal mist
#

ok thats quite simple.

#

forget to start that you are in 3d. simplify it to 2d for an algorithm

spiral flax
#

what I'm trying to do here is make a steppable algorithm, each step would check one block of this large 20x20x20 cube

#

BUT it is important that it starts searching from the origin first

vocal mist
#

yes

#

I did somethign similar with java final static List<Coord> cardinal = Arrays.asList( new Coord(0, 1), new Coord(1, 0), new Coord(0, -1), new Coord(-1, 0) ); final static List<Coord> ordinal = Arrays.asList( new Coord(1, 1), new Coord(1, -1), new Coord(-1, -1), new Coord(-1, 1) );

#

Coord being offsets

spiral flax
#

Isn’t that only for blocks surrounding a single block

vocal mist
#

yes, but that is what you are doing, at its base

#

wha t you want to do is first get all the surounding blocks

#

then you want to do the same but multiply the offset

#

and so on until you have reached 20

#

so for (Coord direction : cardinal) {

spiral flax
#

sorry what do you mean multiple by the offset

#

won't that miss some blocks

vocal mist
#

no you take each Coord and multiply it

spiral flax
#

even then that wouldn't reall ywork-

vocal mist
#

so a Coord of (0, 1) multiplied by 10 would be (0, 10)

#

so your first offsets are 1

spiral flax
#

πŸŸ₯⬛πŸŸ₯⬛πŸŸ₯
⬛πŸŸ₯πŸŸ₯πŸŸ₯⬛
πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯
⬛πŸŸ₯πŸŸ₯πŸŸ₯⬛
πŸŸ₯⬛πŸŸ₯⬛πŸŸ₯

vocal mist
#

you can choose to do cardinal/ordinal or both at teh same time

spiral flax
#

all the red squares are the blocks checked using your system

#

the black ones are still being missed

vocal mist
#

then your implementation is incorrect

spiral flax
#

this is how im processing it in my head

vocal mist
#

oh I see what you mean

spiral flax
#

yeah

#

it misses some of the blocks in between

vocal mist
#

it will

spiral flax
#

and it'll miss more the further it checks

vocal mist
#

yep, in which case

#

um, thinking

#

ok we can do it still using offsets,

#

just from cardinals

spiral flax
#

wdmy by oridnals and cardinals

#

idk what those are

vocal mist
#

cardinal directions N,S,E,W. Ordinals are NE, SE, SW, NW

spiral flax
#

oh

vocal mist
#

so we have to search 1 to 20

spiral flax
#

i feel like this should be a stackoverflow question lmao

#

hm wait so i'm thinking

#

if input is 0, the layer is 0

#

if the input is less than or equal to 9, the layer is 1

vocal mist
#

so for step1 we do origin + each cardinal. From each cardinal we also grab the cardinal + the inverse of the cardinal times teh step

spiral flax
#

if the input is less than or equal to 25, the layer is 2

vocal mist
#

ok a layer at a time

#

so starting with List<Coord> cardinal = Arrays.asList( new Coord(0, 1), new Coord(1, 0), new Coord(0, -1), new Coord(-1, 0) );

#

origin + each cardinal. each cardinal + its rotation. so cardinal (0,1) would become (0,1) + (1,0)

spiral flax
#

isn't that just checking the oridnal as well then

vocal mist
#

I'll see if I can throw some code down

#

yes

spiral flax
#

by the wait i dont mind of the algorithm searches starting from ordinals

vocal mist
#

but doing it that way you can repeate teh offset/rotation multiplied per step so you can walk the line

spiral flax
#

i just want the closest layer to be searched first

vocal mist
#

let me see if I can throw some code down

spiral flax
#

alright

#

@vocal mist i got something

#
public StepResult step() {
    Vec3 offset = new Vec3(0, 0, 0);

    System.out.println("currentStep: " + currentStep);

    int layer = 1;

    while (currentStep > Math.pow(layer, 2)) {
        layer += 2;
    }

    System.out.println("    layer: " + layer);

    System.out.println("    offset: " + offset);
    currentStep++;
    
    return StepResult.PROGRESS;
}
vocal mist
#

Mines still writing

spiral flax
#
currentStep: 1
        layer: 1
        offset: (0, 0, 0)
currentStep: 2
        layer: 3
        offset: (0, 0, 0)
currentStep: 3
        layer: 3
        offset: (0, 0, 0)
currentStep: 4
        layer: 3
        offset: (0, 0, 0)
currentStep: 5
        layer: 3
        offset: (0, 0, 0)
currentStep: 6
        layer: 3
        offset: (0, 0, 0)
currentStep: 7
        layer: 3
        offset: (0, 0, 0)
currentStep: 8
        layer: 3
        offset: (0, 0, 0)
currentStep: 9
        layer: 3
        offset: (0, 0, 0)
currentStep: 10
        layer: 5
        offset: (0, 0, 0)
#

It doesn't print the correct layer number or offset yet

#

but it can at least distinguish different layers apart

#

ok well adding layer = (layer / 2) + 1; underneath the while loop made it print the correct layer

vocal mist
#

ok I think I have teh basis for finding teh adjacent coords

#

nothign for layers yet

#

that should allow you to call findCoords(new Coord(0,0), 10) and it will return you all Coords around the origin up to 10 steps out

spiral flax
#

will this work for steps greater than 10 too?

vocal mist
#

it can search as far as you want

#

but it is only on a flat plane. no layers

spiral flax
#

wait but this fully working for a 2d plane?

vocal mist
#

it should be yes

spiral flax
#

ah damn

#

lemme try

vocal mist
#

brb dog out

spiral flax
#

although i still need to input a single number and get a single set of coords as output

vocal mist
#

back

#

Yep, you specify how far out you want your surrounding coords to be

#

but using that you can easily do that by layer

spiral flax
#

wait are you saying like

#

I just get the index from the list?

vocal mist
#

?

spiral flax
#

like you immediately process all the coordinates, then if i want a specific single number i just index the result list?

vocal mist
#

if you call that method with a distance of 1 you would get back a Set containing 9 Coords

spiral flax
#

yeah but like thats not what i was rlly looking for

#

for example if I input the number "2" it would give me the second coord

#

and the distance is already assumed to be 20, set by a constant

vocal mist
#

2 as in the second layer?

#

I'm not understanding

#

you said you wanted to get all teh surrounding Coords around an origin going out to 20 distance.

#

then the layer above and below

spiral flax
#

uhh

#

sorry i might have communicated this terribly

vocal mist
#

why 1-26 if you are searching to a 20x20x20?

spiral flax
#

well no thats the first layer

#

what i meant by layer is

vocal mist
#

how is 26 the first layer if your max range is 20?

spiral flax
#

there are 27 blocks in a 3x3 cube

#

by max range of 20, I meant the whole cube to search is 20x20x20

vocal mist
#

ah, onion layers

spiral flax
#

yes

#

thats the word i was looking for

vocal mist
#

well thats a little haarder as you have no way of prioritizing which coords to include below a full layer

#

you can easily specify the layer you want

#

is there a reason you want ot be able to request partial layers 1-25?

spiral flax
#

what do you mean request

vocal mist
#

if you input 20 you would not be getting a full layer

spiral flax
#

i mean

vocal mist
#

The simplest is 0 = just the origin, 1 = the layer surrounding the origin and so on.

spiral flax
#

the point is that i get a single coord set from a single number

#

and the coords returned from numbers 1-26 should be a full layer

spiral flax
vocal mist
#

oh ok so it doesn;t matter what you typed, be it 1 or 26 you would get teh same number of Coords back

spiral flax
#

I'm determining the layer from the block number

spiral flax
#

For example inputting the number 0 would give me the origin, (0, 0, 0)

#

inputting the number 1 would give me (-1, -1 , -1)

#

inputting the number 2 would give me (-1, -1, 0)

vocal mist
#

ok there you are not making sense again

spiral flax
#

etc, until the first layer is complete

#

lmfao ok look

#

the layer is not input from the client's end

vocal mist
#

you are saying you want individual coords

spiral flax
#

its an internal variable to figure out the block

vocal mist
#

a specific block

spiral flax
#

yes

#

one block

vocal mist
#

then you have to decide on a search pattern

spiral flax
#

yeah

#

which is fine

vocal mist
#

and not so easy

spiral flax
#

yeah

#

but im going for it

#

this is what I've got right now

#

it's for 2D planes right now, don't mind the Y coordinate in Vec3

vocal mist
#

well to calculate what layer its on just cubroot the value (round down)

#

from there its just how you search for teh block

#

if you always move -1,-1,-1 from origin to get to teh layer

#

cuberoot of 27 is 3

#

which would be the first block in the 3rd layer

#

origin being counted as a layer

spiral flax
#

because it aint working

#

layer 3 should begin at step 27

vocal mist
#

you need to floor the cuberoot

#

not cast

spiral flax
#

doesnt casting to int do the same thing for positive numbers

vocal mist
#

not sure

spiral flax
vocal mist
#

but you don;t want it rounded up

spiral flax
#

well now it's all layer 1

vocal mist
#

is currentstep 5 just 5 so the first layer?

#

what layer does it say for 27?

spiral flax
#

oh nvm 27 is layer 3

#

i didn't realize that you're still doing it for 2D plane

#

when you said cube root i assumed u were doing 3d

vocal mist
#

yes, that is for 3d

spiral flax
#

wait what

#

isn't there 26 blocks that can fit in a cube around a single block

vocal mist
#

yes

#

1 block is layer 1, 26 is layer 2, 27 is layer 3

#

so 27 to 64

spiral flax
#

but its saying layer 1 at step 7

vocal mist
#

um

spiral flax
#

and it goes to layer 2 at step 8

vocal mist
#

then ciel the cuberoot

#

one sec let me think

spiral flax
vocal mist
#

yeah, my head spinning

spiral flax
#

this is breaking my brain

spiral flax
# vocal mist yeah, my head spinning
int sideLength = 1;
while (currentStep > Math.pow(sideLength, 3)) {
    sideLength += 2;
}
int layer = (sideLength / 2) + 1;

what I have here works

vocal mist
#

there must be a cleaner way

#

6*SideLength^2

#

sorry squared

#

surface area is 6 * (length * length)

#

which is how many blocks in each layer

spiral flax
#

wait so what do i assign to layer

#

so far i managed to find the layer and the position in each layer

#

and the maximum position for each layer

vocal mist
#

trying to tink of a simple way to calculate teh layer

vocal mist
#

we shoudl be able to do it without the while

spiral flax
spiral flax
#

I'm losing brain cells though and I might attempt it later

vocal mist
#

Same here πŸ™‚

spiral flax
#

anyways

#

now i have 3 bits of information

#

layer, position within the layer, maximum position within the layer

#

now i just have to convert that into an offset

#

holy shit

#

i think i got something

#

but its hard to tell if its even working correctly

#

it might

#

now i just need to figure out if this print output is correct

#

im just gonna place the blocks in minecraft and see if it works

#

no its wrong

vocal mist
#

Yeah your code isn;t getting the layer right for me

#

step 27 should be in layer 2 and step 28 should be in layer 4currentStep: 27 layer: 4 position: 2 origin: (0, 0, 0) offset: (0, 0, 0) currentStep: 28 layer: 4 position: 3 origin: (0, 0, 0) offset: (0, 0, 0)

spiral flax
vocal mist
#

layer is wrong

spiral flax
#

you sure?

vocal mist
#

it shows 27 is in layer 4

spiral flax
#

how is step 27 layer 4 for you

#

its layer 2 for me

vocal mist
#

did I copy your code wrong?

spiral flax
#

maybe?

vocal mist
#
    public StepResult step() {
        
        Vec3 offset = new Vec3(0, 0, 0);

        System.out.println("currentStep: " + currentStep);
        
        int sideLength = 1;
        while (currentStep > Math.pow(sideLength, 2)) {
            sideLength += 2;
        }
        int layer = (sideLength / 2) + 1;

        System.out.println("    layer: " + layer);
        
        int position = currentStep - (int) Math.pow(sideLength - 2, 2);
        System.out.println("    position: " + position);

        System.out.println("    origin: " + origin + " offset: " + offset);
        currentStep++;
        
        return StepResult.PROGRESS;
    }```
spiral flax
#

hmm

vocal mist
#

ah I see you changed the pow

spiral flax
#

oh yeah

#

im trying to solve the 3d thing rn

#

this is what i have right now

#

the y and z are wrong though

#

im so close

vocal mist
#

position seems right to me

#
    layer: 2
    position: 26
    origin: (0, 0, 0) offset: (0, 0, 0)
currentStep: 28
    layer: 3
    position: 1
    origin: (0, 0, 0) offset: (0, 0, 0)```
spiral flax
#

yeah it works

vocal mist
#

26 blocks in layer 2

spiral flax
#

im just stuck on converting it into an offset now

vocal mist
#

ah ok

spiral flax
vocal mist
#

nice

#

yeah offset is way off. almost no positive values

spiral flax
#

i mean negative numbers is expected

vocal mist
#

its an offset it shoudl have half negative, half positive

spiral flax
#

yeah

#

well no actually

vocal mist
#

yes, its all blocks around the origin

spiral flax
#

for example a legal offset would be (-1, -1, -1)

#

and (1, 1, 1)

vocal mist
#

yes

spiral flax
#

oh thats what you meant

#

okay

#

i feel like im so close man

#

just the y and z coord

#

my brain is on overdrive rn

vocal mist
#

layer 2 pos 1 and layer 2 pos 4 have the same offset. one of those is negative where it should be positive

spiral flax
#

yeah i realized

#

honestly i cant do this rn

#

my brain is fried

#

im gonna play some video games and go back to this later

vocal mist
#

yeah you have to count the total blocks on each side to work out which plane the target block is on, then calculate the offset

spiral flax
#

i cant think properly rn lmao

vocal mist
#

3 times to get teh plane

#

yep same. I'm watching YT

spiral flax
#

YES

#

@vocal mist i got it finally

#

the break was very necessary

#
int position = currentStep > 1 ? currentStep - (int) Math.pow(sideLength - 2, 3) - 1 : 0;

int x = 0, y = 0, z = 0;
for (int i = 0; i < position; i++) {
    z++;
    if (z == sideLength) {
        z = 0;
        y++;
        if (y == sideLength) {
            y = 0;
            x++;
        }
    }

    // cause the loop to repeat one more time if all 3 values are not min or max
    if (x != 0 && x != sideLength - 1 && y != 0 && y != sideLength - 1 && z != 0 && z != sideLength - 1) {
        i--;
    }
}

Vec3 offset = new Vec3(x - (sideLength / 2), y - (sideLength / 2), z - (sideLength / 2));