Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of testing against a box you can of course test against any shape that you wish.</p> <p>I initially posted you can use an NSBezierPath, but appearantly that ins't available on the iPhone kit, only on Macs. Instead, on the iPhone you can use CGPath.</p> <p>Create a new path by using <code>CGPathCreateMutable()</code> which returns a <code>const CGPath *</code> (also known as <code>CHPathRef</code><br> Then use <code>CGPathAddRect</code> or <code>CGPathAddLines</code> to create our path.<br> <code>CGPathContainsPoint</code> will test if your point was in the shape.</p> <p>Alternatively, you can create a customer function that (since you use triangles) just does a simple calculation to check whether your point is inside the triangle shape. Bit of math should do the trick (though when you rotate the shape it'll be slightly more complicated. I write slightly, as you can then just rotate the touch point relative to the shape's origin and do the hit detection)</p> <p>For a triangle:</p> <pre><code> C /\ /__\ A B point of touch is P </code></pre> <p>With the following algorithm you should be able to find the touch:</p> <pre><code>/* first test against a box, for performance */ if( P.y &gt; C.y || P.y &lt; A.y || P.x &lt; A.x || P.X &gt; B.x ) return false; // false if P falls outside "the box" /* then check if its within the shape */ /* split the triangle into 2 parts, around the axle of point C */ if( P.x &lt; C.x ) // if the x value of point P is on the left of point C if( P.y &gt; ((C.y -A.y) / (C.x - A.x)) * P.x ) return false; // the point is above the triangle's side AC else // if the x value of point P is greater than or equal to point C if( P.y &gt; C.y - ((C.y - B.y) / ( B.x - C.x )) * ( P.x - C.x ) ) return false; // the point is above the triangle's side BC return true; // the point must be in the triangle, so return true. </code></pre> <p>The above is dry coded, but should be correct.</p> <p>The above only works against a triangle in the shape as I drew it (where C.x is between A.x and B.x, and A and B are on the same height, but below C). Of course you can modify this to test against any shape, however, you have to weigh this against just using the available <code>CGPath</code>.</p> <p>If you don't get it, or if it's faulty, let me know!</p>
    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. 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.
    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