24
\$\begingroup\$

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.

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

35 Answers 35

1
2
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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)
}
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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