Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check if the <code>MotionEvent</code> coordinates are inside your shape, then do a point-in-polygon hit test. </p> <p>If your isometric shape is a diamond with equal length segments, <strong>and</strong> has angles of all 90 degrees (as corrected by Geobits in comments), a quick test can be done using <a href="http://en.wikipedia.org/wiki/Manhattan_distance" rel="nofollow">Manhatten Distance</a>. Check out the link, the pictures make it more clear why this works.</p> <pre><code>int rectSize = 10; Rectangle diamondBoundingRect = new Rectangle(0,0,rectSize,rectSize); Point point = new Point(9,9); int dx = Math.abs(point.x - diamondBoundingRect.center.x); int dy = Math.abs(point.y - diamondBoundingRect.center.y); if(dx+dy &lt; rectSize){ //point is inside diamond }else{ //point is not inside diamond } </code></pre> <p>This test is inflexible, but it may be all you need.</p> <p>If you need something better, <a href="http://www.ytechie.com/2009/08/determine-if-a-point-is-contained-within-a-polygon.html" rel="nofollow">this blog post</a> explains the algorithm for point-in-polygon hit tests.</p> <p>The idea is to cast a line (called a Ray) from your <code>MotionEvent</code> point to your polygon, and count the number of intersections between that line and line segments in that polygon. If the count is even, the point is outside the shape. If its odd, the point is inside the shape.</p> <p><a href="http://en.wikipedia.org/wiki/Point_in_polygon" rel="nofollow">Wikipedia has a nice picture</a> and more details to help you understand the logic in this approach.</p> <p><em>Concentrate on getting that to work before this next part as it may prove unnecessary depending on your needs.</em></p> <p>The points sent to you in your <code>onTouchEvent</code> are sampled. The device is just giving you points and direction at some predetermined rate. The faster the user moves his finger, the less detail you have as the sampling rate can't keep up. </p> <p>So, if you want more detail, you'll have to <a href="http://en.wikipedia.org/wiki/Interpolation" rel="nofollow">interpolate</a> . This is an advanced way of saying you need to guess where the user swiped his fingers with some fancy math. Cosine interpolation has worked well for me in the past as its a good balance between speed and results.</p> <p>Edit:</p> <p>Here's some code I use personally for point-in-polygon hit tests.</p> <pre><code>public boolean isPointInsidePolygon(double x, double y){ //pointAry holds the polygon's vertices. int polySides = this.pointAry.Length; //need at lest 3 vertices to create a polygon. if (polySides &lt; 3) { return false; } int j = polySides - 1; boolean oddNodes = false; for (int i = 0; i &lt; polySides; i++) { Point pi = this.pointAry[i]; Point pj = this.pointAry[j]; if (pi.y &lt; y &amp;&amp; pj.y &gt;= y || pj.y &lt; y &amp;&amp; pi.y &gt;= y) { if (pi.x + (y - pi.y) / (pj.y - pi.y) * (pj.x - pi.x) &lt; x) { oddNodes = !oddNodes; } } j = i; } return oddNodes; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload