Do the circles overlap?

21

0

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.

Tim

Posted 2017-07-26T20:42:07.293

Reputation: 2 789

we usually are a bit more relaxed on the I/O...could I take (x1,y1),(x2,y2),(r1,r2)? – Giuseppe – 2017-07-26T20:48:52.753

@Giuseppe sorry that was just how I presented them for the test cases. You can take them however you like. (You can take them as a single variable (list, string etc) or as multiple inputs / arguments.) – Tim – 2017-07-26T20:50:31.633

4What do we need to return if two circles are touching externally? – JungHwan Min – 2017-07-26T20:55:53.353

@JungHwanMin I've been thinking about that. The idea of circles touching but not overlapping gives me a headache - I'm not sure it's a real thing. I think that touching circles are technically overlapping, but that might invalidate some answers, so you can return either True or False, as long as it is consistent for your program. – Tim – 2017-07-26T20:59:38.433

6The technical term for "touching but not overlapping" is "tangent" and it is a thing in geometry if nowhere else. – dmckee --- ex-moderator kitten – 2017-07-27T04:32:20.617

2Taking 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 Rock Garf Hunter – 2017-07-27T04:34:08.890

4I 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 Rock Garf Hunter – 2017-07-27T04:39:52.003

Related. – Martin Ender – 2017-07-27T09:34:01.340

Is it really necessary to have float inputs? If the inputs were restricted to integers, a lot more languages could participate, and there wouldn't be issues w.r.t. floating-point imprecision, which would greatly improve this challenge. – Mego – 2017-07-27T10:57:49.110

With hindsight the floats requirement was stringent (there was a reason for it - I had to solve this problem myself with floats and wasn’t thinking carefully enough). I’m not changing it now because I’ve got 19 answers, and some could be affected (maybe they could be shorter if they didn’t handle floats). I’m not sure how it’s confusing - they handle numbers which have any number of digits and optionally a decimal point followed by up to 3 digits. – Tim – 2017-07-27T11:03:20.667

2I 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 – 2017-07-27T13:58:25.577

@Mego you can assume the number is small enough that it is accurate to 3dp – Tim – 2017-07-27T15:29:14.543

1Specifying 'up to four decimal digits precision' would (a) encompass all the test cases shown and (b) ensure that IEEE 32-bit floats could convincingly represent such numbers over a wide range of magnitudes. – dmckee --- ex-moderator kitten – 2017-07-27T19:46:33.210

Is [x1, x2, y1, y2, r1, r2] acceptable input? – Alexander - Reinstate Monica – 2017-07-28T07:09:38.930

@Alexander yes it is – Tim – 2017-07-28T10:57:14.820

Answers

18

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.

Dennis

Posted 2017-07-26T20:42:07.293

Reputation: 196 637

Damn, I forgot about using complex numbers for coordinates. Good one! :D – HyperNeutrino – 2017-07-26T21:19:04.717

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 – 2017-07-27T01:52:15.740

1@JonathanAllan Yes, A computes the distances of the centers as the norm of their difference vector. – Dennis – 2017-07-27T01:53:40.147

11

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))

Arnauld

Posted 2017-07-26T20:42:07.293

Reputation: 111 334

For anyone who hasn't seem Math.hypot before.

– Pureferret – 2017-07-27T07:38:14.097

scala polyglot :) but it does not work for some reason

– V. Courtois – 2017-07-27T08:54:34.297

@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 – 2017-07-27T11:13:49.647

1@Arnauld ooh~ thanks! Should I post it separately? – V. Courtois – 2017-07-27T12:05:28.363

@V.Courtois Sure. Go for it! – Arnauld – 2017-07-27T12:42:55.303

@Pureferret this whole time I've been typing this out explicitly in code, that's great! – Jack – 2017-07-28T12:43:39.497

7

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

Anders Kaseorg

Posted 2017-07-26T20:42:07.293

Reputation: 29 242

7

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?

Luis Mendo

Posted 2017-07-26T20:42:07.293

Reputation: 87 464

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 – 2017-07-27T15:31:12.400

@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 – 2017-07-27T15:33:01.833

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 – 2017-07-27T15:43:34.200

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 – 2017-07-27T15:48:44.760

6

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!

Giuseppe

Posted 2017-07-26T20:42:07.293

Reputation: 21 077

-2 bytes function(k,r)dist(matrix(k,2))<sum(r) – djhurio – 2017-07-27T05:46:26.030

What about dist(matrix(scan(),2))<sum(scan())? – djhurio – 2017-07-27T05:49:27.370

6

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.

xnor

Posted 2017-07-26T20:42:07.293

Reputation: 115 687

5

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

HyperNeutrino

Posted 2017-07-26T20:42:07.293

Reputation: 26 575

This code works in python 2 also. – micsthepick – 2017-07-26T23:50:27.390

@micsthepick Cool, thanks. It's just the way TIO does formatting. – HyperNeutrino – 2017-07-26T23:53:02.800

5

Python 3, 45 bytes

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

Try it online!

Anders Kaseorg

Posted 2017-07-26T20:42:07.293

Reputation: 29 242

5

05AB1E, 6 bytes

αs--0›

Try it online!

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

HyperNeutrino

Posted 2017-07-26T20:42:07.293

Reputation: 26 575

Oh 05AB1E does imaginary now? – Magic Octopus Urn – 2017-08-09T17:40:02.877

1@MagicOctopusUrn Apparently /shrug – HyperNeutrino – 2017-08-09T17:40:38.353

4

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

Adám

Posted 2017-07-26T20:42:07.293

Reputation: 37 779

3

Jelly, 12 bytes

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

Try it online!

-2 bytes thanks to Dennis

HyperNeutrino

Posted 2017-07-26T20:42:07.293

Reputation: 26 575

Wouldn't creating a new link with ạ/² on it same bytes? – caird coinheringaahing – 2017-07-26T21:03:51.040

@cairdcoinheringaahing ? – HyperNeutrino – 2017-07-26T21:04:52.933

Never mind, I got 14 bytes by doing this

– caird coinheringaahing – 2017-07-26T21:06:18.787

You can use I instead of reducing by absolute difference. – Dennis – 2017-07-26T21:24:56.587

@Dennis Ooh thanks – HyperNeutrino – 2017-07-26T21:26:55.867

3

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!

ბიმო

Posted 2017-07-26T20:42:07.293

Reputation: 15 345

3Shorter as an operator: (u!v)r x y s. – Anders Kaseorg – 2017-07-26T21:03:30.267

3

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

JungHwan Min

Posted 2017-07-26T20:42:07.293

Reputation: 13 290

3

Java (OpenJDK 8), 38 bytes

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

Try it online!

Olivier Grégoire

Posted 2017-07-26T20:42:07.293

Reputation: 10 647

3

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.

Kevin Cruijssen

Posted 2017-07-26T20:42:07.293

Reputation: 67 575

1Oh! 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 – 2017-08-09T18:03:27.560

2

Pyth, 15 bytes

<sm^-EE2 2^+EE2

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

Test suite!

KarlKastor

Posted 2017-07-26T20:42:07.293

Reputation: 2 352

2

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.

Sean

Posted 2017-07-26T20:42:07.293

Reputation: 4 136

2

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.

TheLethalCoder

Posted 2017-07-26T20:42:07.293

Reputation: 6 930

Can't you save a few bytes there by writing (r+R)*2 instead of (r+R)+(r+R)? – Ian H. – 2017-07-27T11:21:16.963

@IanH. Yeah don't know how I missed that. – TheLethalCoder – 2017-07-27T11:27:12.813

Am I missing something or does this not work?

– Ian H. – 2017-07-27T11:33:17.127

@IanH. I'd made a typo, the + on the RHS should have been a *. – TheLethalCoder – 2017-07-27T11:35:58.287

And my feedback even made that worse. Good job on the solution though! – Ian H. – 2017-07-27T11:38:15.430

You can golf it to 41 bytes like this: (x,y,r,X,Y,R)=>(x-=X)*x+(y-=Y)*y<(r+=R)*r. I just posted the Java 8 equivalent myself.

– Kevin Cruijssen – 2017-08-09T07:24:19.267

@KevinCruijssen Nice idea! – TheLethalCoder – 2017-08-09T07:55:52.287

2

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.

Engineer Toast

Posted 2017-07-26T20:42:07.293

Reputation: 5 769

1

Java, 50 38 bytes

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

Roman Gräf

Posted 2017-07-26T20:42:07.293

Reputation: 2 915

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 – 2017-07-28T06:45:23.580

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 – 2017-07-28T06:51:18.060

I'm afraid your answer is now exactly the same as the already posted answer by @OlivierGrégoire..

– Kevin Cruijssen – 2017-08-09T07:29:24.697

1

Scala, 23 bytes

Thanks @Arnauld for his almost polyglot answer.

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

Try it online!

V. Courtois

Posted 2017-07-26T20:42:07.293

Reputation: 868

1

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

manatwork

Posted 2017-07-26T20:42:07.293

Reputation: 17 865

1

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.

Cody Gray

Posted 2017-07-26T20:42:07.293

Reputation: 2 639

1

05AB1E, 10 bytes

-¨nOt²¹+θ‹

Try it online!

Magic Octopus Urn

Posted 2017-07-26T20:42:07.293

Reputation: 19 422

1

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.

WebSmithery

Posted 2017-07-26T20:42:07.293

Reputation: 221

0

Julia 0.6.0 (46 bytes)

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

Goysa

Posted 2017-07-26T20:42:07.293

Reputation: 91

0

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.

Joshua

Posted 2017-07-26T20:42:07.293

Reputation: 231

0

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?

Mego

Posted 2017-07-26T20:42:07.293

Reputation: 32 998

0

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.

JAD

Posted 2017-07-26T20:42:07.293

Reputation: 2 898

0

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.

Riking

Posted 2017-07-26T20:42:07.293

Reputation: 249

0

Javascript, 38 chars

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

Test:

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

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

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

Qwertiy

Posted 2017-07-26T20:42:07.293

Reputation: 2 697

0

QBIC, 30 bytes

?abs(:-:)^2+abs(:-:)^2<(:+:)^2

Basically does a²+b²=c². Takes parameters in the order of x1, x2, y1, y2, r1, r2.

                 The code builds a triangle y doing a²+b²=c²
?abs(:-:)^2      Takes the size of our triangle along the x-axis
+abs(:-:)^2      Takes the size of our triangle along the y-axis
 (:+:)^2         Takes the length of both radii
<                And compares them. The circles overlap (-1) if their combined 
                 radii is bigger than the c-side of the triangle

steenbergh

Posted 2017-07-26T20:42:07.293

Reputation: 7 772

0

Swift, 47 Bytes

($0-$3)*($0-$3)+($1-$4)*($1-$4)<($2+$5)*($2+$5)

$0, $1, ... are implicit closure parameters. To use, assign to a variable with a type annotation:

let f: (
    _ x1: Double, // $0 ----+
    _ y1: Double, // $1 ----|----+
    _ r1: Double, // $2 ----|----|----+
    _ x2: Double, // $3 ----+    |    |
    _ y2: Double, // $4 ---------+    |
    _ r2: Double  // $5 --------------+
    ) -> Bool = {
     ($0-$3)*($0-$3)+($1-$4)*($1-$4)<($2+$5)*($2+$5)
}

Alexander - Reinstate Monica

Posted 2017-07-26T20:42:07.293

Reputation: 481

0

Perl 5, 54 bytes

$_=$F[2]+$F[5]>sqrt(($F[0]-$F[3])**2+($F[1]-$F[4])**2)

Try it online!

Input format:

x1 y1 r1 x2 y2 r2

Xcali

Posted 2017-07-26T20:42:07.293

Reputation: 7 671