title: Is there a simple "point in rect" algorithm for a wraparound map?
body: <p>I'm trying to build a rectangular grid that can wrap around at the edges.  Anyone who plays video games will probably be familiar with the concept: go far enough in one direction on the world map and you'll end up back where you started.  This causes some difficulty in setting up the viewport, though, since the edges can scroll into negative coordinate territory.</p>
  
  <p>It's easy enough to take a negative coordinate and determine its real value:</p>
  
  <pre><code>function GetRealCoords(value: TPoint): TPoint;
  begin
     result := ModPoints(AddPoints(value, MAP_SIZE), MAP_SIZE);
  end;
  </code></pre>
  
  <p>where AddPoints and ModPoints simply apply the <code>+</code> and <code>mod</code> operators, respectively, to each coordinate of the two inputs to produce an output value.</p>
  
  <p>The trouble is reversing this operation.  Given a point in which both coordinates are positive and a TRect in which the Top and Left values could be positive or negative, (and the Bottom or Right could be beyond the edges of the map,) and with MAP_SIZE declared at global scope, is there any way to determine whether the point falls within the territory covered by the viewing rectangle without having to run the same calculation up to four different times?</p>