# Calculate 2D tile map tiles within a "field of view" cone

I need to calculate which tiles make up a "cone", originating at my player and extending outward for a specific tile distance.

My player can only face North, South, East, West and I'm not sure yet on the angles I'll use for the cone.

I've found several posts/articles already covering this but they're trying to determine visibility and are a lot more complex than I need. I'm not lighting a room or calculating shadows/occlusions.

• It sounds like you could use Bresenham's line algorithm to enumerate the tiles along one edge of the view, and then flip the result to get the matching tile at the other end of the visible row/column. Every tile in the row/column between these two endpoints is inside your cone. Mar 28, 2017 at 22:04
• I thought about that, but I figured there might be something more efficient. Mar 28, 2017 at 22:04
• It's hard to get more efficient than Bresenham - it dates way back to the days when programmers had to be even more exacting about performance than we tend to be today, and can be done entirely with integer math. If you implement it and your profiling shows a major bottleneck, post about it and we can help you solve it - but in the absence of this evidence, fretting over it might be premature optimization. No sense blocking development progress on a performance issue that very likely may not be an issue at all. Mar 28, 2017 at 22:08

## 1 Answer

With a combination of Bresenham's algorithm and floodfill, this can be very easily solved (you could use draw a lot of lines instead of floodfill, but that tends to create empty spots).

You need to take your player's angle add ans remove half of the FOV angle from it to get the two boundaries of it.

Next, using trigonometry get the coordinates of the corners of the cone and draw a line from the player to the corners.

x = cos(angle) * r
y = sin(angle) * r


Where r is the radius of the cone and angle is the corner angles in radians.

Then interpolate between the two angles, each time using the formula above to set the tiles in a circle segment to be inside the fov. This closes the cone. If you want the cone to have a flat end instead of cicular, you could use the line algorithm.

Lastly, use flood fill to fill in the inner part of the cone with the new tile.