Help with expanding pattern

This board is to show, discuss and archive useful combinator- and logic-creations.
Smart triggering, counters and sensors, useful circuitry, switching as an art :), computers.
Please provide if possible always a blueprint of your creation.
Post Reply
Nilaus
Long Handed Inserter
Long Handed Inserter
Posts: 72
Joined: Sat Aug 13, 2016 11:26 am
Contact:

Help with expanding pattern

Post by Nilaus »

Hi crafty combinator programmers :)

I've been working on an expanding pattern to use for Solar Panels and for automatic biter clearing, but I am having a hard time making this work. I feel this is a basic programming task, but it has been 10 years ago since I did any (shitty) programming. I hope someone on the forum will easily see a solution and can share it with me, since I have pretty much given up on this and I am about to go for somethin less cool :(

Here is the challenge
* Only input is the iteration number
* Output is in X,Y coordinates
* The expansion pattern should be like either of the pictures below e.g. expand diagonally outwards:
Solar expansion.PNG
Solar expansion.PNG (6.34 KiB) Viewed 4171 times
For reference then I have built another self-expanding pattern for solar panels, to show that I am not a complete noob :)
Imgur: http://imgur.com/a/C9n5h
YouTube: https://www.youtube.com/watch?v=Xly_55dQOnk

Initially I copied the pattern from the video above, but tried to have it "expand by 1 when it formed a square", but that didn't work. I've been considering just storing a string corresponding to the direction of the next square, but that doesn't really work either it only scales to the size I make the string.

Anyways, I hope someone can help me with this little trick, then I hook up the rest of the deployer blueprints to this one.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Help with expanding pattern

Post by DaveMcW »

Nilaus wrote:* Only input is the iteration number
If you insist on this, the answer is a complicated math formula that uses square roots and lots of rounding down. Rounding down is easy with combinators, but square roots are hard.

I recommend you use 3 inputs: x, y, and a hidden variable to track the length of the side.

Code: Select all

10 11 12 13
 5  6  7 14
 2  3  8 15
 1  4  9 16
This uses three simple rules:
  • If (X < L) then X = X + 1
  • If (X == L and Y > 0) then Y = Y - 1
  • if (Y == 0) then X = 0, Y = L + 1, L = L + 1
Last edited by DaveMcW on Tue Mar 28, 2017 6:34 pm, edited 1 time in total.

The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Help with expanding pattern

Post by The Eriksonn »

is it allowed to input a pulse instead and let it output the next x,y?

Nilaus
Long Handed Inserter
Long Handed Inserter
Posts: 72
Joined: Sat Aug 13, 2016 11:26 am
Contact:

Re: Help with expanding pattern

Post by Nilaus »

DaveMcW wrote:
Nilaus wrote:* Only input is the iteration number
If you insist on this, the answer is a complicated math formula that uses square roots and lots of rounding down. Rounding down is easy with combinators, but square roots are hard.
Perfect answer! I had a feeling I was making it more difficult for myself than it needed to be :)
What I meant what that I would like to start it and then it keeps track of which square to go to next. I can easily make the "one side is X wide and fill up in the other direction ad inifity", so this was without me specifying the height or width in advance.

I will give your intermediate L value a shot. I was working along those lines, but combinators are not as "crisp" as code, so it wasn't quite working as I intended i.e. didn't work at all :)

Thanks for the input.
The Eriksonn wrote:is it allowed to input a pulse instead and let it output the next x,y?
Sure. I have not set up arbitrary rules for the sake of it, but just tried to say that the system should figure out the height/width dynamically.

pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

Re: Help with expanding pattern

Post by pieppiep »

I did it!

It's a mess, and my first combinator creation more complex than choosing accumulators over steam, so it's up to someone else to clean up.
Expander.png
Expander.png (1.46 MiB) Viewed 4061 times
blueprintstring
The order I'm expanding is,

Code: Select all

17 19 21 23 25
10 12 14 16 24
 5  7  9 15 22
 2  4  8 13 20
 1  3  6 11 18
The arithmetic combinator most south facing up is set to output S = S+0, just a simple memory cell.
This is feeding the A = S*S and B = S+1 which will feed A = A+1 and B = B*B
This will give S*S+1 and (S+1)*(S+1), the starting and ending number for each step further away from 0,0.
S = 0 give A = 1, B = 1
S = 1 give A = 2, B = 4
S = 2 give A = 5, B = 9
etc.
At the top signal I for Input is generated, in this case a simple counter from 1 till 25 with each second the next number.
When A > I the signal A = 1 is given to the left part to signal A and B are to high.
When B < I the signal B = 1 is given to the left part to signal A and B are to low.
This A and B value are translated to A * -1 and B * 1 to S
If this value is added directly to the internal state S, the new A and B values aren't calculated fast enough, so at the most left is a clock generated that pulses each 6 cycles which is multiplied by the S correction signal so that signal doesn't reach the state to often.
This part will find the correct A and B values for the start for the row and column we need to generate.
Next P = I - A, this is to calculate the position between A and B we need.
P is divided by 2 to calculate L to see how far we are on the row/column and multiplied by 2 and then substracted from P to know if we need to be on a row or column.
Now we can use S and L for the row and column, depending on O which on is the row and which one the column.
The final 2 combinators and 25 lamps are just use to see if it works.

This design isn't perfect, the output isn't immediately and there are some intermediate outputs. Those can probably be easiely solved by the same trick I used to only let 1/6 from the correction signal to the state S combinator.
But for placing solar panels this design is probably good enough.

I've tested it with really big input values for I and it seems to work correctly.
When going from 1 to 1000 for the A value it takes over a minute and a half, but in the case for the solar panels that won't happen.

Nilaus
Long Handed Inserter
Long Handed Inserter
Posts: 72
Joined: Sat Aug 13, 2016 11:26 am
Contact:

Re: Help with expanding pattern

Post by Nilaus »

pieppiep wrote:I did it!
Wow!
That is so cool. I agree that it can probably be cleaned up a bit, but at least when the functionality is there it is easier to optimise it.
I wanted to get back to this problem, but I've been creating a few Angels Mods tutorials in the mean time so this got put on the backburner.
I'll give it a shot ingame and see what I can make of it.
Thank you

pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

Re: Help with expanding pattern

Post by pieppiep »

Another way to calculate square root, viewtopic.php?f=193&t=40747

Megatron
Inserter
Inserter
Posts: 47
Joined: Fri Apr 08, 2016 7:00 pm
Contact:

Re: Help with expanding pattern

Post by Megatron »

This is as compact as I could get it.

Image
Blueprint
Coordinates are sorted by distance from (1,1)

Image

Input is on the left, signal , starting with value 1.
Output is on the right, signals [X] and [Y] starting with coordinate (1,1) for index 1.

It has some limitations though.
The output is not stable, you would have to wait a few ticks (worst case for about 50) until the correct result is set.
Additional to [X] and [Y] some temporary signals are set on the ouput.
If the index has illegal values (smaller than 1 or bigger than 2,147,441,939) the output is undefined.
The input has to be set until the calculation is finished.

Post Reply

Return to “Combinator Creations”