Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Check if the cross product of <code>b-a</code> and <code>c-a</code> is<code>0</code>: that means all the points are collinear. If they are, check if <code>c</code>'s coordinates are between <code>a</code>'s and <code>b</code>'s. Use either the x or the y coordinates, as long as <code>a</code> and <code>b</code> are separate on that axis (or they're the same on both).</p> <pre><code>def is_on(a, b, c): "Return true iff point c intersects the line segment from a to b." # (or the degenerate case that all 3 points are coincident) return (collinear(a, b, c) and (within(a.x, c.x, b.x) if a.x != b.x else within(a.y, c.y, b.y))) def collinear(a, b, c): "Return true iff a, b, and c all lie on the same line." return (b.x - a.x) * (c.y - a.y) == (c.x - a.x) * (b.y - a.y) def within(p, q, r): "Return true iff q is between p and r (inclusive)." return p &lt;= q &lt;= r or r &lt;= q &lt;= p </code></pre> <p>This answer used to be a mess of three updates. The worthwhile info from them: Brian Hayes's <a href="http://bit-player.org/bph-publications/BeautifulCode-2007-Hayes.pdf" rel="nofollow noreferrer">chapter</a> in <em>Beautiful Code</em> covers the design space for a collinearity-test function -- useful background. <a href="https://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment#328337">Vincent's answer</a> helped to improve this one. And it was Hayes who suggested testing only one of the x or the y coordinates; originally the code had <code>and</code> in place of <code>if a.x != b.x else</code>.</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.
 

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