# Do the circles overlap?

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.

• What do we need to return if two circles are touching externally? Jul 26, 2017 at 20:55
• The technical term for "touching but not overlapping" is "tangent" and it is a thing in geometry if nowhere else. Jul 27, 2017 at 4:32
• 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. Jul 27, 2017 at 4:34
• 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. Jul 27, 2017 at 4:39
• 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.
– user45941
Jul 27, 2017 at 13:58

# 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 Jul 26, 2017 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.) Jul 27, 2017 at 1:52
• @JonathanAllan Yes, A computes the distances of the centers as the norm of their difference vector. Jul 27, 2017 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. Jul 27, 2017 at 7:38
• scala polyglot :) but it does not work for some reason Jul 27, 2017 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. Jul 27, 2017 at 11:13
• @Arnauld ooh~ thanks! Should I post it separately? Jul 27, 2017 at 12:05
• @V.Courtois Sure. Go for it! Jul 27, 2017 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 and Q
gsE      sum(eval(input()) >= that


# MATL, 5 bytes

ZPis<


Input format is:

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


### 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? Jul 27, 2017 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) Jul 27, 2017 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! Jul 27, 2017 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? Jul 27, 2017 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) Jul 27, 2017 at 5:46
• What about dist(matrix(scan(),2))<sum(scan())? Jul 27, 2017 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

• Oh 05AB1E does imaginary now? Aug 9, 2017 at 17:40
• @MagicOctopusUrn Apparently /shrug Aug 9, 2017 at 17:40

# 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. Jul 26, 2017 at 23:50
• @micsthepick Cool, thanks. It's just the way TIO does formatting. Jul 26, 2017 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]].

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


Try it online!

Thanks @AndersKaseorg for -1 byte!

• Shorter as an operator: (u!v)r x y s. Jul 26, 2017 at 21:03

# Jelly, 12 bytes

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


Try it online!

-2 bytes thanks to Dennis

• Wouldn't creating a new link with ạ/² on it same bytes? Jul 26, 2017 at 21:03
• @cairdcoinheringaahing ? Jul 26, 2017 at 21:04
• Never mind, I got 14 bytes by doing this Jul 26, 2017 at 21:06
• You can use I instead of reducing by absolute difference. Jul 26, 2017 at 21:24
• @Dennis Ooh thanks Jul 26, 2017 at 21:26

# 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.

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


Try it here.

• 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 Aug 9, 2017 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)? Jul 27, 2017 at 11:21
• @IanH. Yeah don't know how I missed that. Jul 27, 2017 at 11:27
• Am I missing something or does this not work? Jul 27, 2017 at 11:33
• @IanH. I'd made a typo, the + on the RHS should have been a *. Jul 27, 2017 at 11:35
• And my feedback even made that worse. Good job on the solution though! Jul 27, 2017 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. Jul 28, 2017 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... Jul 28, 2017 at 6:51

# 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-$i,$i-$i)<$i+$i?: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.

# dc, 22 bytes

9k?+_5R-d*_3R-d*+v-vzp


Try it online!

Or try the test suite!

Input is read from stdin in the format x1 x2 y1 y2 r1 r2. Note that negative numbers are written in dc with an underscore _ instead of a minus sign -.

Output is on stdout: 1 for truthy, 0 for falsey. If the two circles are tangent to one another or if they're identical, they're considered to be overlapping.

This requires a recent version of GNU dc which supports the R operation (rotate).

On versions of dc which don't support R, you can instead use

9k?-d*sY-d*lY+vsD+lD-vzp

(24 bytes long), with input presented on stdin in the order r1 r2 x1 x2 y1 y2. Here's a test suite for the 24-byte program, for older versions of dc

Explanation

9k   Set precision to 9 decimal places.
?    Read the input and push all 6 numbers on the stack (r2 is at the top of the stack since it's entered last).
_5R  Rotate the stack so that r1+r2 is now at the bottom.
-d*  Compute (y1-y2)^2.
_3R  Rotate the stack so that it's now: r1+r2 (y1-y2)^2 x1 x2 (top on the right)
-d*  Compute (x1-x2)^2.
+    Compute (x1-x2)^2 + (y1-y2)^2.
v    Take the square root to compute the distance between the centers.
-    Subtract the distance between the centers from r1-r2.
v    Pop one number from the stack. If it's non-negative, then compute its square root and push that back on the stack.
z    Push the size of the stack onto the stack. (This is 0 if the difference was negative, and it's 1 if the difference was positive.)
p    Print the item at the top of the stack.


## Julia 0.6.0 (46 bytes)

a->((a-a)^2+(a-a)^2<(a+a)^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.