Given the coordinates of the centres and the radii of 2 circles, output a truthy value of whether they do or do not overlap.

Input

  • Input may be taken via STDIN or equivalent, function arguments, but not as a variable. You can take them as a single variable (list, string etc) or as multiple inputs / arguments, in whatever order you want.

  • The input will be six floats. These floats will be up to 3 decimal places. The coordinates can be positive or negative. The radii will be positive.

Output

  • Output can be via STDOUT or function return.

  • The program must have exactly 2 distinct outputs - one for a True value (the circles do overlap) and one for a False output (they don't overlap).

Test cases

(Input is given as list of tuples [(x1, y1, r1), (x2, y2, r2)] for the test cases; you can take input in any format)

True

[(5.86, 3.92, 1.670), (11.8, 2.98, 4.571)]
[(8.26, -2.72, 2.488), (4.59, -2.97, 1.345)]
[(9.32, -7.77, 2.8), (6.21, -8.51, 0.4)]

False

[(4.59, -2.97, 1.345), (11.8, 2.98, 4.571)]
[(9.32, -7.77, 2.8), (4.59, -2.97, 1.345)]
[(5.86, 3.92, 1.670), (6.21, -8.51, 0.4)]

This is Code Golf, shortest answer in bytes wins.

  • 4
    What do we need to return if two circles are touching externally? – JungHwan Min Jul 26 '17 at 20:55
  • 6
    The technical term for "touching but not overlapping" is "tangent" and it is a thing in geometry if nowhere else. – dmckee Jul 27 '17 at 4:32
  • 2
    Taking floats seems like a pretty stringent requirement. Could you relax it to a more general representation? I would like to solve this in Brain-Flak, but I am unlikely to take the time to implement IEEE floats, and if I did it would be 90% of the byte count anyway so I would just be golfing a float implementation. – Post Left Garf Hunter Jul 27 '17 at 4:34
  • 4
    I would also like to point out that floats are not accurate up to "three decimal places" in a lot of cases. I'm not sure exactly what you want answers to handle, but its a little confusing right now. – Post Left Garf Hunter Jul 27 '17 at 4:39
  • 2
    I think you might have a fundamental misunderstanding of how floats work. Because they are fixed-size, as the values get larger, the precision gets lower. There is a point beyond which a float cannot accurately represent all values within 3 decimal places. Also, editing a challenge to remove an unnecessary restriction is not discouraged. – Mego Jul 27 '17 at 13:58

34 Answers 34

Jelly, 5 bytes

IA<S}

Takes two complex numbers (centers) as first argument, and two real numbers (radii) as second argument.

Try it online!

How it works

IA<S}  Main link.
       Left argument:  [x1 + iy1, x2 + iy2]
       Right argument: [r1, r2]

I      Increments; yield (x2 - x1) + i(y2 - y1).
 A     Absolute value; yield √((x2 - x1)² + (y2 - y1)²).
   S}  Take the sum of the right argument, yielding r1 + r2.
  <    Compare the results.
  • Damn, I forgot about using complex numbers for coordinates. Good one! :D – HyperNeutrino Jul 26 '17 at 21:19
  • Out of interest would the result of A here be considered the norm of the row vector "centers"? (ÆḊ itself errors with complex content.) – Jonathan Allan Jul 27 '17 at 1:52
  • 1
    @JonathanAllan Yes, A computes the distances of the centers as the norm of their difference vector. – Dennis Jul 27 '17 at 1:53

JavaScript (ES6), 38 bytes

Takes input as 6 distinct variables x1, y1, r1, x2, y2, r2.

(x,y,r,X,Y,R)=>Math.hypot(x-X,y-Y)<r+R

Test cases

let f =

(x,y,r,X,Y,R)=>Math.hypot(x-X,y-Y)<r+R

// True
console.log(f(5.86, 3.92, 1.670, 11.8, 2.98, 4.571))
console.log(f(8.26, -2.72, 2.488, 4.59, -2.97, 1.345))
console.log(f(9.32, -7.77, 2.8, 6.21, -8.51, 0.4))

// False
console.log(f(4.59, -2.97, 1.345, 11.8, 2.98, 4.571))
console.log(f(9.32, -7.77, 2.8, 4.59, -2.97, 1.345))
console.log(f(5.86, 3.92, 1.670, 6.21, -8.51, 0.4))

  • For anyone who hasn't seem Math.hypot before. – Pureferret Jul 27 '17 at 7:38
  • scala polyglot :) but it does not work for some reason – V. Courtois Jul 27 '17 at 8:54
  • @V.Courtois The way you pass the parameters doesn't match the method declaration. It should be a:Double,x:Double,b:Double,y:Double,r:Double,q:Double. – Arnauld Jul 27 '17 at 11:13
  • 1
    @Arnauld ooh~ thanks! Should I post it separately? – V. Courtois Jul 27 '17 at 12:05
  • @V.Courtois Sure. Go for it! – Arnauld Jul 27 '17 at 12:42

Pyth, 5 bytes

gsE.a

Input format:

[x1, y1], [x2, y2]
r1, r2

Try it online

How it works

     Q   autoinitialized to eval(input())
   .a    L2 norm of vector difference of Q[0] and Q[1]
gsE      sum(eval(input()) >= that

MATL, 5 bytes

ZPis<

Input format is:

[x1, y1]
[x2, y2]
[r1, r2]

Try it online! Or verify all test cases.

How it works

ZP   % Take two vectors as input. Push their Euclidean distance
i    % Input the vector of radii
s    % Sum of vector
<    % Less than?
  • Not sure if it is me, but when I use your trial link and press run I get 'Error The server's response could not be decoded' -- Also Not sure if it helps, but did you think about (ab) using complex numbers like in the Jelly answer? – Dennis Jaheruddin Jul 27 '17 at 15:31
  • @DennisJaheruddin Hey, nice to see you again here! (1) Blame caching, probably. Did you try a hard refresh? (2) I did, but I think it's also 5 bytes (-| instead of ZP) – Luis Mendo Jul 27 '17 at 15:33
  • I suppose it is the firewall. Now i'm wondering whether an input format with something like -r2 instead of r2 would help because then you would need three differences, instead of 2 differences and an addition... I'd better run before I get drawn in too deep! – Dennis Jaheruddin Jul 27 '17 at 15:43
  • I don't think that negating one input is acceptable as input format. If you find any problems with the Try It Online service, would you please report here? – Luis Mendo Jul 27 '17 at 15:48

R, 39 bytes

function(k,r)dist(matrix(k,2,2))<sum(r)

takes input k=c(x1,x2,y1,y2) and r=c(r1,r2); returns FALSE for tangent circles.

Try it online!

27 bytes:

function(m,r)dist(m)<sum(r)

Takes input as a matrix with the circle centers given as rows and a vector of radii.

Try it online!

  • -2 bytes function(k,r)dist(matrix(k,2))<sum(r) – djhurio Jul 27 '17 at 5:46
  • What about dist(matrix(scan(),2))<sum(scan())? – djhurio Jul 27 '17 at 5:49

Python, 40 bytes

lambda x,y,r,X,Y,R:abs(x-X+(y-Y)*1j)<r+R

Try it online!

Uses Python's complex arithmetic to compute the distance between the two centers. I'm assuming we can't take the input points directly as complex numbers, so the code expresses them like x+y*1j.

Python 3, 45 bytes

lambda X,Y,R,x,y,r:(X-x)**2+(Y-y)**2<(R+r)**2

Try it online!

05AB1E, 6 bytes

αs--0›

Try it online!

-1 byte by using a - b > 0 rather than (reverse) b - a < 0

Python 3, 45 bytes

lambda a,b,c,d,e,f:(a-d)**2+(b-e)**2<(c+f)**2

Try it online!

-8 bytes thanks to Neil/Step Hen

  • This code works in python 2 also. – micsthepick Jul 26 '17 at 23:50
  • @micsthepick Cool, thanks. It's just the way TIO does formatting. – HyperNeutrino Jul 26 '17 at 23:53

APL (Dyalog), 10 bytes

Prompts for circle centers as list of two complex numbers, then for radii as list of two numbers

(+/⎕)>|-/⎕

Try it online!

(+/⎕) [is] the sum of the radii

> greater than

| the magnitude of

-/⎕ the difference in centers

Mathematica, 16 bytes

Norm[#-#2]<+##3&

Input: [{x1, y1}, {x2, y2}, r1, r2]


Mathematica has a RegionIntersection builtin, but that alone is 18 bytes long...

Built-in version:

RegionIntersection@##==EmptyRegion@2&

Takes 2 Disk objects. [Disk[{x1, y1}, r1], Disk[{x2, y2}, r2]].

Haskell, 37 36 bytes

(u#v)r x y s=(u-x)^2+(v-y)^2<(r+s)^2

Try it online!

Thanks @AndersKaseorg for -1 byte!

  • 3
    Shorter as an operator: (u!v)r x y s. – Anders Kaseorg Jul 26 '17 at 21:03

Jelly, 12 bytes

I²+⁴I²¤<⁵S²¤

Try it online!

-2 bytes thanks to Dennis

Java (OpenJDK 8), 38 bytes

(a,b,c,x,y,z)->Math.hypot(a-x,b-y)<c+z

Try it online!

Java 8, 41 38 bytes

(x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R

Try it here.

Apparently, Java also has Math.hypot, which is 3 bytes shorter.

EDIT: Just realized this answer is now exactly the same as @OlivierGrégoire's Java 8 answer, so please upvote him instead of me if you like the 38-byte answer.

Old answer (41 bytes):

(x,y,r,X,Y,R)->(x-=X)*x+(y-=Y)*y<(r+=R)*r

Try it here.

  • 1
    Oh! So that's why I got 3 upvotes today, but 0 when the challenge was posted? ^^ I was wondering what triggered this weird behavior ;) Since I like my answer, and you posted the same, you get a +1 as well! :p – Olivier Grégoire Aug 9 '17 at 18:03

Pyth, 15 bytes

<sm^-EE2 2^+EE2

Takes input in the order x1,x2,y1,y2,r1,r2

Test suite!

Perl 6, 13 bytes

*+*>(*-*).abs

Try it online!

The first two arguments are the radii, in either order. The third and fourth arguments are the coordinates of the centers, as complex numbers, in either order.

Taxi, 1582 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Tom's Trims.Go to Tom's Trims:n.[a]Go to Post Office:s.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to What's The Difference.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 5 l.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:s 1 l 2 r 4 l.Pickup a passenger going to Addition Alley.Go to Tom's Trims:s 1 r 3 r.Pickup a passenger going to The Babelfishery.Switch to plan "b" if no one is waiting.Switch to plan "a".[b]Go to Addition Alley:n 1 r 1 l 3 l 1 l.Pickup a passenger going to Magic Eight.Go to Post Office:n 1 r 1 r 3 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 5 l 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:s 1 l 2 r 4 l.Pickup a passenger going to Magic Eight.Go to Magic Eight:s 1 r.Switch to plan "c" if no one is waiting.'1' is waiting at Writer's Depot.[c]'0' is waiting at Writer's Depot.Go to Writer's Depot:w 1 l 2 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.

Try it online!

Outputs 1 for overlapping circles.
Outputs 0 for non-overlapping circles (including tangential circles).

Ungolfed / formatted:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Tom's Trims.
Go to Tom's Trims: north.
[a]
Go to Post Office: south.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: north 5th left.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: south 1st left 2nd right 4th left.
Pickup a passenger going to Addition Alley.
Go to Tom's Trims: south 1st right 3rd right.
Pickup a passenger going to The Babelfishery.
Switch to plan "b" if no one is waiting.
Switch to plan "a".
[b]
Go to Addition Alley: north 1st right 1st left 3rd left 1st left.
Pickup a passenger going to Magic Eight.
Go to Post Office: north 1st right 1st right 3rd right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 5th left 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: south 1st left 2nd right 4th left.
Pickup a passenger going to Magic Eight.
Go to Magic Eight: south 1st right.
Switch to plan "c" if no one is waiting.
'1' is waiting at Writer's Depot.
[c]
'0' is waiting at Writer's Depot.
Go to Writer's Depot: west 1st left 2nd left.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.

C#, 50 41 bytes

(x,y,r,X,Y,R)=>(x-=X)*x+(y-=Y)*y<(r+=R)*r

Saved 9 bytes thanks to @KevinCruijssen.

  • Can't you save a few bytes there by writing (r+R)*2 instead of (r+R)+(r+R)? – Ian H. Jul 27 '17 at 11:21
  • @IanH. Yeah don't know how I missed that. – TheLethalCoder Jul 27 '17 at 11:27
  • Am I missing something or does this not work? – Ian H. Jul 27 '17 at 11:33
  • @IanH. I'd made a typo, the + on the RHS should have been a *. – TheLethalCoder Jul 27 '17 at 11:35
  • And my feedback even made that worse. Good job on the solution though! – Ian H. Jul 27 '17 at 11:38

Scala, 23 bytes

Thanks @Arnauld for his almost polyglot answer.

math.hypot(a-x,b-y)<r+q

Try it online!

PostgreSQL, 41 characters

prepare f(circle,circle)as select $1&&$2;

Prepared statement, takes input as 2 parameters in any circle notation.

Sample run:

Tuples only is on.
Output format is unaligned.
psql (9.6.3, server 9.4.8)
Type "help" for help.

psql=# prepare f(circle,circle)as select $1&&$2;
PREPARE

psql=# execute f('5.86, 3.92, 1.670', '11.8, 2.98, 4.571');
t

psql=# execute f('8.26, -2.72, 2.488', '4.59, -2.97, 1.345');
t

psql=# execute f('9.32, -7.77, 2.8', '6.21, -8.51, 0.4');
t

psql=# execute f('4.59, -2.97, 1.345', '11.8, 2.98, 4.571');
f

psql=# execute f('9.32, -7.77, 2.8', '4.59, -2.97, 1.345');
f

psql=# execute f('5.86, 3.92, 1.670', '6.21, -8.51, 0.4');
f

Java, 50 38 bytes

(x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R
  • Using ideas in other answers, this can be shortened to 38 like so: (x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R. In fact, just realised this is the exact same as Arnauld's JavaScript answer. – laszlok Jul 28 '17 at 6:45
  • Thanks... This answer was nevee intended to be golfed... i thought it was such a simple challenge there wouldn't be anything that can be golfed... – Roman Gräf Jul 28 '17 at 6:51
  • I'm afraid your answer is now exactly the same as the already posted answer by @OlivierGrégoire.. – Kevin Cruijssen Aug 9 '17 at 7:29

x86 Machine Code (with SSE2), 36 bytes

; bool CirclesOverlap(double x1, double y1, double r1,
;                     double x2, double y2, double r2);
F2 0F 5C C3        subsd   xmm0, xmm3      ; x1 - x2
F2 0F 5C CC        subsd   xmm1, xmm4      ; y1 - y2
F2 0F 58 D5        addsd   xmm2, xmm5      ; r1 + r2
F2 0F 59 C0        mulsd   xmm0, xmm0      ; (x1 - x2)^2
F2 0F 59 C9        mulsd   xmm1, xmm1      ; (y1 - y2)^2
F2 0F 59 D2        mulsd   xmm2, xmm2      ; (r1 + r2)^2
F2 0F 58 C1        addsd   xmm0, xmm1      ; (x1 - x2)^2 + (y1 - y2)^2
66 0F 2F D0        comisd  xmm2, xmm0
0F 97 C0           seta    al              ; ((r1 + r2)^2) > ((x1 - x2)^2 + (y1 - y2)^2)
C3                 ret

The above function accepts descriptions of two circles (x- and y-coordinates of center point and a radius), and returns a Boolean value indicating whether or not they intersect.

It uses a vector calling convention, where the parameters are passed in SIMD registers. On x86-32 and 64-bit Windows, this is the __vectorcall calling convention. On 64-bit Unix/Linux/Gnu, this is the standard System V AMD64 calling convention.

The return value is left in the low byte of EAX, as is standard with all x86 calling conventions.

This code works equally well on 32-bit and 64-bit x86 processors, as long as they support the SSE2 instruction set (which would be Intel Pentium 4 and later, or AMD Athlon 64 and later).

AVX version, still 36 bytes

If you were targeting AVX, you would probably want to add a VEX prefix to the instructions. This does not change the byte count; just the actual bytes used to encode the instructions:

; bool CirclesOverlap(double x1, double y1, double r1,
;                     double x2, double y2, double r2);
C5 FB 5C C3      vsubsd   xmm0, xmm0, xmm3   ; x1 - x2
C5 F3 5C CC      vsubsd   xmm1, xmm1, xmm4   ; y1 - y2
C5 EB 58 D5      vaddsd   xmm2, xmm2, xmm5   ; r1 + r2
C5 FB 59 C0      vmulsd   xmm0, xmm0, xmm0   ; (x1 - x2)^2
C5 F3 59 C9      vmulsd   xmm1, xmm1, xmm1   ; (y1 - y2)^2
C5 EB 59 D2      vmulsd   xmm2, xmm2, xmm2   ; (r1 + r2)^2
C5 FB 58 C1      vaddsd   xmm0, xmm0, xmm1   ; (x1 - x2)^2 + (y1 - y2)^2
C5 F9 2F D0      vcomisd  xmm2, xmm0
0F 97 C0         seta     al                 ; ((r1 + r2)^2) > ((x1 - x2)^2 + (y1 - y2)^2)
C3               ret

AVX instructions have the advantage of taking three operands, allowing you to do non-destructive operations, but that doesn't really help us to compact the code any here. However, mixing instructions with and without VEX prefixes can result in sub-optimal code, so you generally want to stick with all AVX instructions if you're targeting AVX, and in this case, it doesn't even hurt your byte count.

05AB1E, 10 bytes

-¨nOt²¹+θ‹

Try it online!

PHP, 66 bytes

<?php $i=$argv;echo hypot($i[1]-$i[4],$i[2]-$i[5])<$i[3]+$i[6]?:0;

Try it online!

Runs from the command line, taking input as 6 command-line parameter arguments, and prints 1 if the circles overlap, else 0.

Julia 0.6.0 (46 bytes)

a->((a[1]-a[2])^2+(a[3]-a[4])^2<(a[5]+a[6])^2)

Clojure, 68 bytes

#(<(+(*(- %4 %)(- %4 %))(*(- %5 %2)(- %5 %2)))(*(+ %6 %3)(+ %6 %3)))

Takes six arguments: x1, y1, r1, x2, y2, r2. Returns true or false.

Sadly, Clojure does not have a pow function of some sorts. Costs a lot of bytes.

Actually, 8 bytes

-)-(h@+>

Try it online!

Explanation:

-)-(h@+>  (implicit input: [y1, y2, x1, x2, r1, r2])
-         y2-y1 ([y2-y1, x1, x2, r1, r2])
 )-       move to bottom, x1-x2 ([x1-x2, r1, r2, y2-y1])
   (h     move from bottom, Euclidean norm ([sqrt((y2-y1)**2+(x2-x1)**2), r1, r2])
     @+   r1+r2 ([r1+r2, norm])
       >  is r1+r2 greater than norm?

R (+pryr), 31 bytes

pryr::f(sum((x-y)^2)^.5<sum(r))

Which evaluates to the function

function (x, y, z) 
sum((x - y)^2)^0.5 < sum(z)

Where x are the coordinates of circle 1, y are the coordinates of circle 2 and z the radii.

Calculates the distance between the two centers using Pythagoras and tests if that distance is smaller than the sum of the radii.

Makes use of R's vectorisation to simultaneously calculate (x1-x2)^2 and (y1-y2)^2. These are then summed and squarely rooted.

Go, 93 bytes

package q
import c "math/cmplx"
func o(a,b complex128,r,R float64)bool{return c.Abs(b-a)<r+R}

Fairly simple algorithm, same as several other answers, except it uses the built-in complex type and calls math/cmplx.Abs().

Taking the radii as complex numbers doesn't help, because the cast to float64 adds more bytes than the variable declaration saves (can't do float64 < complex128).

Try it online! Includes the test cases, and uses package main instead of a library.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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