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.

asked Jan 5, 2020 at 23:23
\$\endgroup\$
2
  • \$\begingroup\$ Is complex number input allowed? \$\endgroup\$ Commented Jan 6, 2020 at 0:12
  • \$\begingroup\$ @xnor Yes, it's allowed. \$\endgroup\$ Commented Jan 6, 2020 at 0:13

8 Answers 8

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
answered Jan 5, 2020 at 23:42
\$\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!

answered Jan 6, 2020 at 6:32
\$\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.

answered Jan 5, 2020 at 23:58
\$\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!

answered Jan 6, 2020 at 0:32
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 5ị€ can be ṁ5. \$\endgroup\$ Commented Jan 6, 2020 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!

answered Jan 6, 2020 at 1:32
\$\endgroup\$
2
  • \$\begingroup\$ I added a test case where all four sides have different lengths, and your code seems to fail on it. \$\endgroup\$ Commented Jan 6, 2020 at 2:16
  • \$\begingroup\$ @Bubbler Now fixed. My optimization was silly. \$\endgroup\$ Commented Jan 6, 2020 at 2:18
2
\$\begingroup\$

APL (Dyalog Unicode), (削除) 17 (削除ここまで) 11 bytes SBCS

-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?
answered Jan 14, 2020 at 15:20
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 11 bytes using alternating sum -/. \$\endgroup\$ Commented Jan 15, 2020 at 0:12
  • \$\begingroup\$ Thanks, @Bubbler I didn't knkow about that \$\endgroup\$ Commented Jan 15, 2020 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!

answered Jan 6, 2020 at 23:45
\$\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.

answered Jan 8, 2020 at 12:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.