Point in polygon: ray casting: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Test every edge with an exact orientation predicate and explicit boundary classification.

Cast a horizontal right ray and count half-open edge crossings with exact orientation signs. Classify an explicit boundary point before applying odd-even parity.

Open the 3D visualization

Point in polygon: ray casting example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Point in polygon: ray casting

Point containment under the even-odd fill rule, including self-intersecting polygon paths.

Choosing an alternative

Nonzero winding can classify a twice-traced loop as inside while even-odd parity classifies it outside. Both rules are meaningful and agree on ordinary simple polygons.

Worked example: input and state changes

Example input

0,0;4,0;4,4;0,4|2,2

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Exact integer predicates; equal scale on both coordinate axes.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [],
      "kind": "graph",
      "points": [
        [
          0,
          0
        ],
        [
          4,
          0
        ],
        [
          4,
          4
        ],
        [
          0,
          4
        ],
        [
          2,
          2
        ]
      ],
      "nodeLabels": [
        "0 (0, 0)",
        "1 (4, 0)",
        "2 (4, 4)",
        "3 (0, 4)",
        "Q (2, 2)"
      ],
      "directed": false,
      "chosen": [],
      "visited": [],
      "result": 0,
      "output": []
    }
  2. Step 1: Edge 0: cross product 8; right-ray crossings 0.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [
        0,
        1,
        4
      ],
      "kind": "graph",
      "points": [
        [
          0,
          0
        ],
        [
          4,
          0
        ],
        [
          4,
          4
        ],
        [
          0,
          4
        ],
        [
          2,
          2
        ]
      ],
      "nodeLabels": [
        "0 (0, 0)",
        "1 (4, 0)",
        "2 (4, 4)",
        "3 (0, 4)",
        "Q (2, 2)"
      ],
      "directed": false,
      "chosen": [
        0
      ],
      "visited": [],
      "result": 0,
      "output": []
    }
  3. Step 3: Edge 2: cross product 8; right-ray crossings 1.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [
        2,
        3,
        4
      ],
      "kind": "graph",
      "points": [
        [
          0,
          0
        ],
        [
          4,
          0
        ],
        [
          4,
          4
        ],
        [
          0,
          4
        ],
        [
          2,
          2
        ]
      ],
      "nodeLabels": [
        "0 (0, 0)",
        "1 (4, 0)",
        "2 (4, 4)",
        "3 (0, 4)",
        "Q (2, 2)"
      ],
      "directed": false,
      "chosen": [
        2
      ],
      "visited": [],
      "result": 0,
      "output": []
    }
  4. Step 4: Edge 3: cross product 8; right-ray crossings 1.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [
        3,
        0,
        4
      ],
      "kind": "graph",
      "points": [
        [
          0,
          0
        ],
        [
          4,
          0
        ],
        [
          4,
          4
        ],
        [
          0,
          4
        ],
        [
          2,
          2
        ]
      ],
      "nodeLabels": [
        "0 (0, 0)",
        "1 (4, 0)",
        "2 (4, 4)",
        "3 (0, 4)",
        "Q (2, 2)"
      ],
      "directed": false,
      "chosen": [
        3
      ],
      "visited": [],
      "result": 0,
      "output": []
    }
  5. Step 5: Inside under even-odd fill rule.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [
        4
      ],
      "kind": "graph",
      "points": [
        [
          0,
          0
        ],
        [
          4,
          0
        ],
        [
          4,
          4
        ],
        [
          0,
          4
        ],
        [
          2,
          2
        ]
      ],
      "nodeLabels": [
        "0 (0, 0)",
        "1 (4, 0)",
        "2 (4, 4)",
        "3 (0, 4)",
        "Q (2, 2)"
      ],
      "directed": false,
      "chosen": [],
      "visited": [],
      "result": 1,
      "output": []
    }

Expected final state

Inside under even-odd fill rule.

Complexity and input bounds

Family: Geometry. Time complexity: O(n).

Implementation detail: Up to twelve polygon vertices and one query, integers -20..20. Return 2 boundary, 1 inside, 0 outside. Native n=0 returns outside. Repeated vertices are supported. Both views preserve the coordinate aspect ratio. Independent fixtures cover degeneracies and deterministic outcomes in all nine languages.

Space complexity: O(n) index storage for closest-pair sweep; O(1) predicate state otherwise, excluding inputs and saved frames.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Polygon vertices x,y separated by ;, then |queryX,queryY. 3..12 vertices; integers -20..20. Boundary=2, inside=1, outside=0.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Do not double-count a vertex at the query height. Horizontal edges require boundary testing but contribute no crossing. State the fill rule for complex paths.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
7
8
// Closed polygon path n 0..12; x/y/query integer -20..20. Return 2 on boundary, otherwise 1 inside under even-odd ray parity rule, or 0 outside. Empty path returns 0. Repeated vertices and self-intersections are supported under the stated fill rule, which may differ between algorithms. Half-open vertical crossings avoid vertex double counting; exact integer orientation avoids division. O(n) time, O(1) auxiliary space. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(x, y, n, qx, qy) {
    let count = 0; let next = 0; let turn = 0;
    for (let i = 0; i < n; i++) { next = (i+1)%n; turn = (x[next]-x[i])*(qy-y[i])-(y[next]-y[i])*(qx-x[i]);
    if (turn == 0 && (qx-x[i])*(qx-x[next]) <= 0 && (qy-y[i])*(qy-y[next]) <= 0) { return 2; }
    if ((y[i] > qy && y[next] <= qy && turn < 0) || (y[i] <= qy && y[next] > qy && turn > 0)) { count++; } }
    return count%2;
}

Source

Original-author technical reference comparing polygon containment strategies, not an asserted first-invention paper.