Well, I’ve got something - but it isn’t what I’m looking for. Let’s take a look at some concepts from the previous post first. Ok, fine. I will do away with the mouse-drawn graphics and use a camera for once.
Ok, so remember this thing that didn’t work out because it doesn’t include negatives:
My camera phone is not much better than my mouse-drawings when the flash decides not to go off.
Again, it wouldn’t have been better than using a list, because to get through the chain of arrows I’d have to start from (1, 1) and follow the pattern through until I got to whatever terrain I needed. If that terrain was at (100, 100) it would take a rather long time to do. So, I’d need some trick or equation to simply calculate the seed value from the terrain coordinate. I thought about it for a while. After a bit, I started seeing this triangle differently:
Instead of thinking of the triangle as a right triangle, we rotate it 45 degrees and it becomes equilateral
Not that huge of a change, to be honest. I just rotated the triangle 45 degrees. The upshot is, though, a pattern is now clearly recognizable. Going down the left side of the triangle, where the x coordinates are all ones, you can see that the seed value is simply the summation of the values 1 through the y coordinate, or, in more math-y symbols, seed = \(\sum_{i=1}^{y}i\) where \(x = 1\). For those of you who do not recognize the sigma symbol, this just means that you add up every number from 1 to the y-coordinate. For example, if the y coordinate is 5, then the seed value is 1+2+3+4+5.
Of course, this only gets us values along the left edge of the triangle. After I drew another row, however, Now, it becomes clear (for me, at least - maybe there’s people in the crowd who already came up with an answer). Going along a row from left to right, you can see that the adjacent seed value to the right is one less, its x coordinate is one greater, and its y coordinate is one lesser, as illustrated here:
Shifting along the rows, we can get to whatever coordinate we want
So, to get the seed value of a coordinate, you 1.) find the seed value of the coordinate on the left side of the triangle of the same row and 2.) shift over along the same row until you get to the coordinate you want.
Well, that’s one way, anyway. It’s somewhat inefficient to go one coordinate at a time using a loop. So, instead, we do some maths to do it. Here’s the final result of this entire thing:
seed = \(\sum_{i = 1}^{y+x-1}i - (x - 1)\)
\(y + x - 1\) gets us the y coordinate of the left-most point in the row. Subtracting \(x - 1\) gives us the seed value, since adding x values from 1 moves us across the row (refer to some of the upper figures).
Wait, wait, wait - This system doesn’t work though. I already realized it doesn’t work, even before I started this post. It doesn’t include negative numbers in it! Yeah, well, this is a simplified version of the same thing I did with another solution my dad gave me from the previous post. Remember this:
It’s the bunch of concentric box-shapes from the last post. The idea is very similar to that outlined above, except instead of taking the left side of the row, we take some arbitrary starting position of each box - the top-center, in the picture above. Really, I could have used any side or corner I wanted to to start off each box with. We’ll come back to this reference point in a bit.
Now that we’ve got some reference to work off of, the next step is finding some easy way to identify what box any given point is on. It’s actually easier than it looks. Simply take the absolute value of x and y, take whichever is larger, and that is the box you are on. For example, (0, 1) is on box 1, because 1 > 0. (-3, 2) is on box 3, because 3 > 2. (-289, 237) is on box 289, because 289 > 237.
Finally, we’ve got to find a way to tell what offset from the reference point any given point is. In the triangle example, it was easy, because everything was sorted into nice, neat rows. In the box example, it’s a little less apparent, since we’ve got negative numbers and curve-arounds and such. I’ll actually admit - this problem has got me beat up to this point. I’ve been sitting here and staring at this diagram for a while.
Anyone has an idea, let me know.
However, this is the last piece in the puzzle. Once I’ve got this solved, I can finally start implementing the “infinite” part of “infinite terrain generation.” ‘Til next time.