Iterate Over Circular Area Of Hexagonal Grid? - algorithm

I am currently working with a hexagonal grid of cells which are indexed like this:
I am trying to find the simplest way to iterate over a circular area of this grid. For example, with (3, 3) as the center cell and a radius of one cell, I would want the loop to iterate over the cells (3, 3), (4, 4), (4, 3), (4, 2), (3, 2), (2, 3), (3, 4) (in any order). Given the coordinate of the center cell and a radius (excluding the center cell), how would you construct a two-dimensional loop to iterate over each cell/coordinate?

The basic idea:
Let's scan each row of the hexagon left-to-right.
How to do it:
Let R be the "cell radius".
Start at the center and move left R cells. Call this cell "A". A's row has 2*R-1 cells in it.
Now step one cell up/right. This cell begins a row with one less
cell.
Repeat step 2 until you've stepped up/right R times.
This covers the top half. Now do the lower half with similar steps (stepping down/right starting from A, to find the beginning of each of those rows).
Details
How do you step up/right?
The y coordinate increases by 1. The x coordinate increases by 1 only if y was odd.
How do you step down/right?
The y coordinate decreases by 1. The x coordinate increases by 1 only if y was odd.

The choice of grid system is what makes this one complicated. Because, for example, a step down and right might change your coordinates by (1, -1) or (0, -1) depending on which row you are in.
Therefore I would move to a different grid system, do the calculation there, then switch back to the grid system that you're showing.
Here is a demonstration of that in Python.
def loop (center, radius):
if 0 == radius:
yield center
else:
directions = [
( 1, -1),
( 0, -1),
(-1, 0),
(-1, 1),
( 0, 1),
( 1, 0),
]
cell = list(to_normalized(center))
cell[1] += radius
for direction in directions:
for i in range(radius):
cell[0] += direction[0]
cell[1] += direction[1]
yield to_grid(cell)
def to_normalized (cell):
return (cell[0] - (cell[1] // 2), cell[1])
def to_grid (cell):
return (cell[0] + (cell[1] // 2), cell[1])
for cell in loop((3, 3), 2):
print(cell)

Related

Sampling without replacement along a 2D grid in Cython without gil (can be in C++)

I have essentially a 2D grid of N by M (i.e. N rows, and M columns). I would like to sample from this grid without replacement in Cython without requiring the GIL. I would want something like this
for j in range(n_samples):
row_idx, col_idx = sample_without_replacement(grid)
So for example, say my 2D grid is 3 x 4, then I might get the following samples
(0, 3),
(0, 2),
(2, 1),
(3, 0)
Is there a simple way to do this in Cython?

Use movement vector to detect impact in 2D field

Given a coordinate and a movement vector. How can I get the list of all blocks will be infected?
I think it's a little bit like "object in view" or "object collision"?
For example, the original point O is (2, 2), the movement vector "→" is (0, 1) and the blocks B should be [(1, 3), (1, 4), (2, 4), (3, 3), (3, 4)]
0123 y→
0.......
1...BB..
2..O→B..
3...BB..
x.......
↓.......
If the movement vector is (-1, 1), the B should be [(0, 2), (0, 3), (0, 4), (1, 4), (2, 4)]
0123 y→
0..BBB..
1...↗B..
2..O.B..
3.......
x.......
↓.......
I'm currently considering
point P (x, y) vector V (v, u)
x' = x + v
y' = y + u
set S = ([x'-1, x'+1], [y'-1, y'+1])
calculate a line L "y=ax+b" perpendicular to V crossing (x', y').
split S into two groups by L
chose the one V is facing
but I cannot find a way to achieve last two steps.
Any suggestion will be helpful.
I think what you are looking for is something called time-until-impact or time-until-collision or something like that.
But in most game engines this is not the way to go. What a standard technique for collision handling is, is space partitioning. That means, if you have n rigid bodies in your simulation then a naive algorithm requires n^2 checks (each with another) to see if a collision happened. However, performance-wise this is a killer. Instead you should partition your space via (`uniform partitioning, Oct-Trees, Z-ordering, etc.), then you only check with neighboring rigid bodies that are neighbors with the cell in question.
Note, that the velocity vector (what you call movement) vector is not really needed here because in practice it brings no benefits.

Finding the shape corresponding to a set of points

I'm working on a small problem right now.
I'm reading in from a file a set of points, and I am asked to find the area inside of them (there is more to the problem, but I'm not worried about that right now). I am given the number of points, and the problem I have is that my area function does not work if the points are not a edge traversal.
For example: If the set of points is [(0,0) , (1, 0), (1, 1), (0, 1)], it will correctly calculate the area as 1. However, if the set of points is given as [(0,0) , (1, 1), (1, 0), (0, 1)], it will return 0 as the area.
How can I take the list of points, and have it find a traversal (either clockwise or counterclockwise, it doesn't matter)? I'm not familiar with any fast algorithms to do so.
Note: This is not a convex-hull problem. The shape does not necessarily have to be convex. For example, the set of points [(0,0), (0,2), (1, 1), (2, 2), (2, 0)] is a valid shape.

Algorithm for inverting a polygon within a rectangle?

I'm trying to make a "flashlight" effect in my game where the player can only see places that are in his line of sight.
I've got most of the effect done by raycasting to each vertex in the game world and added and extra raycasts +-0.0001 rad, then connecting them in clockwise order to form this shape in red. I'm trying to get the inverse of this polygon within the bounds of the rectangular level similar to the "Inverse Selection" option in programs like Photoshop (example)
Construct a set for both rectangle and polygon and calculate the symmetric difference (vertices in either rectangle or polygon but not in both), for example:
rectangle = [(0, 0), (13, 0), (13, 10), (0, 10)]
polygon = [(0, 5), (0, 10), (2, 6), (8, 6), (11, 0), (13, 0), (13, 10)]
# "^" is the symmetric difference operator in python
set(rectangle) ^ (set(polygon))
Returns:
set([(11, 0), (2, 6), (0, 5), (0, 0), (8, 6)])
Which corresponds to the green area (vertices A, I, E, H, J) in the following picture:
Beware that it will get the complement of the red polygon which does not include the intersection with the wall in your original image:
If you want your result to be the yellow polygon of the next figure instead:
Then you will have to do a rectangle-polygon intersection for every wall/block in the stage with the complement polygon using some method like the ones described in the following question:
Method to detect intersection between a rectangle and a polygon?

Algorithm to iterate through image pixels

I'm sure something for this already exists so I figure why reinvent the wheel.
Does anyone know of an algorithm that will iterate through pixels in an image from the top left towards the bottom right.
So first it will check: (0, 0)
Then: (1, 0), (1, 1), (0, 1)
Then (2, 0), (2, 1), (2, 2), (1, 2), (0, 2)
... and so on....
Looking for the most efficient algorithm for this problem.
Thanks.
It seems you want to iterate in this manner:
1 4 9
↑ ↑
2→3 8
↑
5→6→7
...
This is just a for-loop:
for radius in range(squareImage.width):
for col in range(radius):
yield (radius, col)
for row in range(radius):
yield (radius-row, radius)
yield (0, radius)
Efficiency is not a concern here. It is impossible to do better than linear-in-number-of-pixels (like this) because you have to visit every pixel.

Resources