# Are vertices in a clockwise order?

Your program must accept as input six numbers, which describe a triangle - for example, the inputs 80, 23, 45, 1, 76, -2 describe a triangle with vertices (80, 23), (45, 1), and (76, -2). The input must be given as six plain real numbers or as a list of six real numbers, in the order given in the example. Your task is to figure out whether the triangle's vertices are given in clockwise order. Assume that the input vertices are not all in one line. In this case, the vertices are not in clockwise order because the second vertex is to the bottom-left of the first.

This is code-golf, meaning the submission with the shortest number of bytes wins. Good luck!

• Can we take the points as a list of three pairs? Or, can we take the points as two lists of length three, being the X and Y coordinate lists? May 31 at 4:12
• Can you add some test cases? May 31 at 4:32
• For future reference, we strongly recommend using the Sandbox to get feedback on your challenges before you post them to the main site. May 31 at 6:34
• Please avoid cumbersome I/O formats.
May 31 at 8:18
• @Adám Would upvote your comment but accidently hit upvote twice. Don't know why ppl get so caught-up on minor parts of their posts. This is code-golf, let people answer creatively. May 31 at 9:37

# JavaScript (ES6), 38 bytes

Returns a Boolean value (true for clockwise).

(a,b,c,d,e,f)=>a*d+b*e+c*f<b*c+a*f+d*e


Try it online!

$$D=\begin{vmatrix} a&b&1\\ c&d&1\\ e&f&1 \end{vmatrix}=(ad-bc)-(af-be)+(cf-de)$$

$$D<0\iff ad+be+cf

# Jelly, 8 bytes

s2;€1ÆḊṠ


Try it online!

-2 bytes thanks to Bubbler by transposing the matrix from ngn's formula which saves bytes on padding with 1s, since determinant is invariant over transposition.

Outputs -1 for clockwise and 1 for counterclockwise. Stole ngn's formula from chat.

s2;€1ÆḊṠ   Main Link; [x0, y0, x1, y1, x2, y2]
s2         Slice into chunks of size 2; [[x0, y0], [x1, y1], [x2, y2]]
;        Append...
€       to each chunk...
1      1; [[x0, y0, 1], [x1, y1, 1], [x2, y2, 1]]
ÆḊ    Determinant
Ṡ   Sign

• 9 bytes May 31 at 4:40
• @Bubbler Oh, nice. And very nice observation about the transpose being unnecessary; thanks! May 31 at 4:43

# R, 6244 36 bytes

Edit: Realized that I'd ridiculously over-engineered my first version after looking at Arnauld's answer which uses the same approach. Upvote that!

Edit 2: And then I looked hyper-neutrino's answer (upvote that one, too!) realized that that strategy would be shorter anyway.

function(p)det(rbind(matrix(p,2),1))


Try it online!

Outputs a positive value for anticlockwise, and a negative value for clockwise. Add 2 bytes (>0) (like this) if you prefer to have a TRUE/FALSE output.

# Factor + math.matrices.laplace, 44 bytes

[ 2 group [ 1 suffix ] map determinant sgn ]


Try it online!

# J, 24 16 bytes

0>1-/ .*@,.3 2$]  Try it online! -8 thanks to Bubbler for pointing out I could take a single determinant of a 3x3 to implement the shoelace formula Takes input as a single list. Looks like hyper-neutrino beat me to the idea, which is just to use the shoelace formula and then check if the answer is negative. Since the shoelace formula expects the coordinates in counter-clockwise order, it will be negative only when they're in clockwise order. ## how Consider the input: 80 23 45 1 76 _2  • 3 2$] Shape into matrix:

80 23
45  1
76 _2

• 1...,. Zip with 1:

1 80 23
1 45  1
1 76 _2

• -/ .* Determinant:

787

• 0> Is it negative?

0

• OP answered "no" to "Can we take the points as a list of three pairs?". And if you were to use determinant built-in, directly using it on 3x3 is shorter anyway. May 31 at 5:08

# MATLAB/Octave, 33 bytes

@(A)det([reshape(A,2,3);1,1,1])<0


Try it online!
Anonymous function. Takes as input list of points (so a vector in MATLAB's nomenclature). It reshapes it into 2x3 array, adds ones and calculates the determinant.
The reshaped array with ones looks like so: $$\begin{bmatrix} A(1) & A(3) & A(5) \\ A(2) & A(4) & A(6) \\ 1 & 1 & 1 \end{bmatrix}$$

# Charcoal, 28 bytes

≔⪪Ａ²θ›⁰ΣＥθ×§ι⁰ΣＥ³×⊖λ§§θ⁺κ⊖λ¹


Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for clockwise, nothing if not. Explanation:

≔⪪Ａ²θ


Split the input into three pairs of coordinates.

›⁰ΣＥθ×§ι⁰ΣＥ³×⊖λ§§θ⁺κ⊖λ¹


Multiply each coordinate by each cyclically adjacent coordinate and its adjacency, i.e. $$x_0 (y_{0+1} + 0y_{0+0} - y_{0-1}) + x_1 (y_{1+1} + 0y_{1+0} - y_{1-1}) + x_2(y_{2+1} + 0y_{2+0} - y_{2-1})$$ and compare the sum to zero to see whether the vertices are clockwise.

# Python 2, 42 bytes

lambda a,b,c,d,e,f:a*d+b*e+c*f<b*c+a*f+d*e


Try it online!

Port of Arnuald's JS answer. But unfortunately using dot product is longer, because @ operator requires numpy.