Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up against this same problem a little while ago, and after some searching, I found this to be the best solution.</p> <p>Java has a <code>Polygon</code> class with a <code>contains()</code> method that would make things really simple. Unfortunately, the <code>java.awt.Polygon</code>class is not supported in Android. However, I was able to find someone who wrote an <a href="http://www.anddev.org/other-coding-problems-f5/using-java-awt-polygon-in-android-t6521.html#wrap" rel="noreferrer">equivalent class</a>.</p> <p>I don't think you can get the individual points that make up the path from the Android <code>Path</code> class, so you will have to store the data in a different way.</p> <p>The class uses a Crossing Number algorithm to determine whether or not the point is inside of the given list of points.</p> <pre><code>/** * Minimum Polygon class for Android. */ public class Polygon { // Polygon coodinates. private int[] polyY, polyX; // Number of sides in the polygon. private int polySides; /** * Default constructor. * @param px Polygon y coods. * @param py Polygon x coods. * @param ps Polygon sides count. */ public Polygon( int[] px, int[] py, int ps ) { polyX = px; polyY = py; polySides = ps; } /** * Checks if the Polygon contains a point. * @see "http://alienryderflex.com/polygon/" * @param x Point horizontal pos. * @param y Point vertical pos. * @return Point is in Poly flag. */ public boolean contains( int x, int y ) { boolean oddTransitions = false; for( int i = 0, j = polySides -1; i &lt; polySides; j = i++ ) { if( ( polyY[ i ] &lt; y &amp;&amp; polyY[ j ] &gt;= y ) || ( polyY[ j ] &lt; y &amp;&amp; polyY[ i ] &gt;= y ) ) { if( polyX[ i ] + ( y - polyY[ i ] ) / ( polyY[ j ] - polyY[ i ] ) * ( polyX[ j ] - polyX[ i ] ) &lt; x ) { oddTransitions = !oddTransitions; } } } return oddTransitions; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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