Skip to the code
cf.
+10 XP

#666 Is the Shape Convex

easy

A vector editor offers a "smooth fill" that only works on convex shapes. A shape is given by its corners vertices, in order around the outline (clockwise or counterclockwise). The outline never crosses itself.

Write isConvex(vertices) that returns true if the shape is convex, meaning every turn along the outline goes the same way, and false if it has a dent. Three corners in a straight line don't count as a turn either way.

Example 1

Input: vertices = [[0,0],[4,0],[4,4],[0,4]]
Output: true

A square turns left at every corner.

Example 2

Input: vertices = [[0,0],[4,0],[2,1],[4,4],[0,4]]
Output: false

At [2, 1] the outline turns right while every other corner turns left: that's a dent.

Constraints

  • 3 <= vertices.length <= 10^4
  • -10^4 <= x, y <= 10^4
  • The outline doesn't cross itself and has non-zero area.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).