3
\$\begingroup\$

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!

\$\endgroup\$
12
  • 5
    \$\begingroup\$ 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? \$\endgroup\$
    – hyper-neutrino
    May 31 at 4:12
  • 17
    \$\begingroup\$ Can you add some test cases? \$\endgroup\$
    – Jonah
    May 31 at 4:32
  • 4
    \$\begingroup\$ For future reference, we strongly recommend using the Sandbox to get feedback on your challenges before you post them to the main site. \$\endgroup\$
    – pxeger
    May 31 at 6:34
  • 12
    \$\begingroup\$ Please avoid cumbersome I/O formats. \$\endgroup\$
    – Adám
    May 31 at 8:18
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Noodle9
    May 31 at 9:37
14
\$\begingroup\$

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<bc+af+de$$

\$\endgroup\$
9
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 9 bytes \$\endgroup\$
    – Bubbler
    May 31 at 4:40
  • \$\begingroup\$ @Bubbler Oh, nice. And very nice observation about the transpose being unnecessary; thanks! \$\endgroup\$
    – hyper-neutrino
    May 31 at 4:43
6
\$\begingroup\$

R, 62 44 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.

\$\endgroup\$
3
\$\begingroup\$

Factor + math.matrices.laplace, 44 bytes

[ 2 group [ 1 suffix ] map determinant sgn ]

Try it online!

Port of @hyper-neutrino's Jelly answer.

\$\endgroup\$
3
\$\begingroup\$

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
    
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Bubbler
    May 31 at 5:08
3
\$\begingroup\$

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}$$

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 28 bytes

≔⪪A²θ›⁰ΣEθקι⁰ΣE³×⊖맧θ⁺κ⊖λ¹

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

≔⪪A²θ

Split the input into three pairs of coordinates.

›⁰ΣEθקι⁰ΣE³×⊖맧θ⁺κ⊖λ¹

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.