2
\$\begingroup\$

I'm attempting to optimize a game map by only drawing circular & annular sectors within the current viewable rectangle bounds.

The solution should cover both cases in the example graphic (Figs 1 & 2). I have a version of Figure 1 working which converts the rectangle vertices to polar coordinates and calculates start/end angles and inner & outer radii to draw within, however this does not work when the rect overlaps the centre of the circle (Fig 2).

Drawing is currently being done with JavaScript & SVG, so an example using those would be ideal (or just the maths portion in JS). Also, each of the 'wedges' in the grid should be drawn separately, as they each need to be referenced as discrete objects by the engine.

Polar grid rectangle intersection

\$\endgroup\$
  • 3
    \$\begingroup\$ Please, post the relevant code and say what specifically doesn´t work with your current implementation. \$\endgroup\$ – Ignatiamus Jan 10 '18 at 19:54
  • \$\begingroup\$ I'm too mathematically dumb to suggest a solution right off the bat, but one fuzzy idea popping in my head is to think of the circle as consisting of 4 symmetrical quadrants to be tested/drawn separately, and if your rectangle contains/overlaps the center, split it into multiple rectangles based on the quadrant. \$\endgroup\$ – user77245 Jan 11 '18 at 1:45
  • \$\begingroup\$ This way if your existing solution using polar coordinates can work for a quarter of the circle, then it should work for the rest since you'll be applying that solution separately for each quadrant the rectangle overlaps. \$\endgroup\$ – user77245 Jan 11 '18 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.