Calculating the Bounding Rectangle of a Circular Sector

https://news.ycombinator.com/rss Hits: 3
Summary

Sun 19 Oct 2025 This article will be short and straight to the point. While working with geometry in 2D, I was recently looking for an algorithm to calculate the bounding box of a specific shape that I initially called a "cone". Actually, as I'm talking about 2D, I should rather say I needed the bounding rectangle of a circular sector - a part of a circle with a limited angle around an axis pointing in a specific direction. When developing a 2D game, this shape can represent, for example, the area of effect of an attack, such as punching nearby enemies, firing a shotgun, spraying some substance ahead, or casting a magical spell. Calculating its bounding rectangle can be useful for querying a space-partitioning data structure (like a grid, a quadtree, etc.) for potentially affected objects. I prototyped my solution in ShaderToy, which you can see here: shadertoy.com/view/w3jcRw. A circular sector is described by: vec2 apex - the starting point and the center of the circle that this shape is part of vec2 direction - a vector pointing in the direction of the axis (must be normalized) float halfAngle - the angle between the axis and the edges, or half of the angle between the opposing edges (in radians, in range 0...π) For 0, the shape becomes just a line segment. For π, it becomes a full circle. float radius - the radius of the circle that this shape is part of The output bounding rectangle is described by just vec2 MinPos, MaxPox - two points defining the minimum and maximum coordinates it contains. To calculate the bounding rectangle of our cone, we need to consider all possible points that extend the furthest along the X and Y axes, and take their min/max. The first such point is the apex. The next two are what I call "edge points." However, there are cases where this is not enough. We also need to check four "extra points" located at a distance of radius from the apex along -X, +X, -Y, +Y, as long as each of these points belongs to the cone. My final algorithm in G...

First seen: 2025-10-25 18:31

Last seen: 2025-10-25 20:33