16
\$\begingroup\$

Related: Is this quadrilateral cyclic?

Background

A tangential quadrilateral is a quadrilateral which has an incircle:

Examples include any square, rhombus, or a kite-like shape. Rectangles or parallelograms in general are not tangential.

Task

Given the four vertices of a quadrilateral (as Cartesian coordinates), determine if it is tangential.

Input & output

For input, it is allowed to use any format that unambiguously specifies the four vertices' coordinates (eight real or floating-point numbers). You can assume the following on the input:

  • The points specify a simple convex quadrilateral, i.e. all internal angles are strictly less than 180 degrees, and the edges meet only at the vertices.
  • The points are specified in counter-clockwise order (or the other way around if you want).

For output, you can use one of the following:

  • Truthy/falsy values as defined by your language of choice (swapping the two is allowed), or
  • Two consistent values for true/false respectively.

It is acceptable if your code produces wrong output due to floating-point inaccuracies.

Test cases

Tangential

(0, 0), (0, 1), (1, 1), (1, 0)  # unit square
(-2, 0), (0, 1), (2, 0), (0, -1)  # rhombus
(1, -2), (-2, -1), (-1, 2), (4, 2)  # kite
(0, 0), (50, 120), (50, 0), (32, -24)  # all four sides different

Not tangential

(0, 0), (0, 1), (2, 1), (2, 0)  # rectangle
(0, 0), (1, 1), (3, 1), (2, 0)  # parallelogram

Scoring & winning criterion

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Is complex number input allowed? \$\endgroup\$ – xnor Jan 6 at 0:12
  • \$\begingroup\$ @xnor Yes, it's allowed. \$\endgroup\$ – Bubbler Jan 6 at 0:13
11
\$\begingroup\$

MATL, 11 10 bytes

5:)d|2e!sd

Input is a vector of four complex numbers. Output is 0 (which is falsy) if tangential, or nonzero (which is truthy) if not tangential.

Try it online! Or verify all test cases.

Explanation

The code computes the difference between sums of lengths of opposite sides. This difference is zero if and only if the quatrilateral is tangential.

5:   % Range [1 2 3 4 5]
)    % Implicit input: complex vector of length 4. Index into it modularly.
     % This repeats the first vertex after the last
d    % Consecutive differences
|    % Absolute value, element-wise
2e   % Reshape as a 2-column matrix, in column-major order
!    % Transpose
s    % Sum of each column. Gives a vector of length 2
d    % Consecutive difference
| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Python 3, 47 bytes

f=lambda l,i=3:i+1and abs(l[i]-l[i-1])-f(l,i-1)

Try it online!

Take complex number input. Outputs as Truthy/Falsey swapped. Test cases from Noodle9.


48 bytes

lambda a,b,c,d:A(a-b)+A(c-d)-A(b-c)-A(d-a)
A=abs

Try it online!

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Python 3, 89 \$\cdots\$ 59 55 bytes

lambda l:sum((-1)**i*abs(l[i-1]-l[i])for i in range(4))

Try it online!

A list of vertices as complex numbers is passed in. The lengths of the sides \$(a, b, c, d)\$ are calculated and uses \$a+c=b+d\$ for a tangential quadrilateral. Returns's a falsy value (0) for a tangential or a truthy value (nonzero) otherwise.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Jelly, 9 8 bytes

ṁ5ạƝŒœ§E

Try it online!

Explanation

5ị€       | Modular index 1,2,3,4,5 into list
   ạƝ     | Absolute difference of neighbouring pairs
     Œœ   | Split into odd and even indices
       §  | Sum of inner lists
        E | Equal

A monadic link taking a list of complex coordinates and returning 1 for tangential and 0 for not.

Based on @LuisMendo’s MATL answer so be sure to upvote that one!

Thanks to @JonathanAllan for saving a byte!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 5ị€ can be ṁ5. \$\endgroup\$ – Jonathan Allan Jan 6 at 21:59
2
\$\begingroup\$

JavaScript (ES6), 74 bytes

Takes input as a list of coordinate pairs. Returns \$0\$ (falsy) for tangential or a non-zero value (truthy) for non-tangential.

a=>(g=_=>Math.hypot(([x,y]=a[i],[X,Y]=a[++i&3],x-X),y-Y))(i=0)-g()+g()-g()

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I added a test case where all four sides have different lengths, and your code seems to fail on it. \$\endgroup\$ – Bubbler Jan 6 at 2:16
  • \$\begingroup\$ @Bubbler Now fixed. My optimization was silly. \$\endgroup\$ – Arnauld Jan 6 at 2:18
2
\$\begingroup\$

APL (Dyalog Unicode), 17 11 bytesSBCS

-6 bytes thanks to Bubbler

outputs 1 if tangential, 0 if not

0=-/|2-/5⍴⎕

Try it online!

Explanation:

0=-/|2-/5⍴⎕

          ⎕ take 4 complex numbers as evaluated input
        5⍴   reshape to 5
     2-/     difference between each pair of numbers
    |        absolute value
  -/         alternating sum
0=           the quadrilateral is tangential if the final result is 0

Previous answer

=/+/⍉2 2⍴|2-/5⍴⎕

Try it online!

Explanation:

=/+/⍉2 2⍴|2-/5⍴⎕

                ⎕  take 4 complex numbers as evaluated input
              5⍴    reshape to 5
           2-/      find the difference between each pair of numbers
          |         absolute value
      2 2⍴          reshape to 2x2 matrix
    ⍉              transpose
  +/                sum the rows
=/                  are they both equal?
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 11 bytes using alternating sum -/. \$\endgroup\$ – Bubbler Jan 15 at 0:12
  • \$\begingroup\$ Thanks, @Bubbler I didn't knkow about that \$\endgroup\$ – mabel Jan 15 at 9:31
1
\$\begingroup\$

Wolfram Language (Mathematica), 24 bytes

Returns Sphere if the quadrilateral is tangential, Insphere if it is not.

Head@Insphere@Polygon@#&

Try it online!


Wolfram Language (Mathematica), 38 bytes

Returns True if the quadrilateral is tangential, False if it is not.

0=={1,-1,1,-1}.Norm/@(#-RotateLeft@#)&

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 9 bytes

ĆüαnOtιOË

Try it online!

Port of Nick Kennedy's Jelly answer. It turned out pretty short, despite 05AB1E's lack of complex numbers.

| improve this answer | |
\$\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.