dc, 22 bytes
9k?+_5R-d*_3R-d*+v-vzp
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).
+ Add r1 and r2
_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.