Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-323

Point in Convex Polygon

Problem

Return True if the point lies inside or on the boundary of the convex polygon given by its vertices in order, and False otherwise.

Check the sign of the cross product between each edge and the vector to the point: every non-zero sign must agree for the point to be inside or on the hull.

Hint

Boundary points have a cross product of zero for one edge and are still inside.

Test cases

case 1
in: [[[0,0],[2,0],[2,2],[0,2]],[1,1]]
expected: true
case 2
in: [[[0,0],[2,0],[2,2],[0,2]],[2,2]]
expected: true
case 3
in: [[[0,0],[2,0],[2,2],[0,2]],[3,3]]
expected: false
case 4
in: [[[0,0],[4,0],[2,4]],[2,2]]
expected: true
case 5
in: [[[0,0],[4,0],[2,4]],[1,3]]
expected: false
Starter codePython
def point_in_convex_polygon(points, point):
    # Your code here
    pass

5 test cases · run your code against them in the editor