This question is similar to Do the circles overlap?, except your program should return True if the circumference of one circle intersects the other, and false if they do not. If they touch, this is considered an intersection. Note that concentric circles only intersect when both radii are equal
Input is two x, y coordinates for the circle centres and two radius lengths, as either 6 floats or ints, in any order, and output a boolean value, (you may print True or False if you choose)
test cases are in format (x1, y1 x2, y2, r1, r2):
these inputs should output true:
0 0 2 2 2 2
1 1 1 5 2 2
0.55 0.57 1.97 2.24 1.97 2.12
these should output false:
0 0 0 1 5 3
0 0 5 5 2 2
1.57 3.29 4.45 6.67 1.09 3.25
-
3\$\begingroup\$ So this is the same as the other one except for if one circle is entirely inside the other? \$\endgroup\$geokavel– geokavel2017年07月27日 03:05:42 +00:00Commented Jul 27, 2017 at 3:05
-
\$\begingroup\$ @geokavel essentially, yes \$\endgroup\$micsthepick– micsthepick2017年07月27日 03:07:32 +00:00Commented Jul 27, 2017 at 3:07
-
2\$\begingroup\$ The slight variation makes for a very different problem. Good job! \$\endgroup\$Adám– Adám2017年07月27日 08:35:50 +00:00Commented Jul 27, 2017 at 8:35
-
\$\begingroup\$ Related. \$\endgroup\$Martin Ender– Martin Ender2017年07月27日 09:33:34 +00:00Commented Jul 27, 2017 at 9:33
-
\$\begingroup\$ You are using an unusual definition of intersection. In Mathematics, one circle being entirely within the other implies there is intersection. So you should perhaps rephrase, replacing "intersection" by "partial intersection" \$\endgroup\$Luis Mendo– Luis Mendo2017年07月27日 10:19:24 +00:00Commented Jul 27, 2017 at 10:19
7 Answers 7
APL (Dyalog), 20 bytes
Uses Anders Kaseorg's formula: 0≤(x1−x2)2−(r1−r2)2+(y1−y2)2≤4r1r2
Takes (x1, r1, y1) as left argument and (x2, r2, y2) as right argument.
(-/2*⍨-)(≤∧0≤⊣)×ばつ
The overall function's structure is a fork (3-train) where the tines are -/2*⍨- and ≤∧0≤⊣ and ×ばつ*. The middle tine takes the results of the side tines as arguments. The side tines use the overall function's arguments.
Right tine:
×ばつ multiply the arguments (x1x2, r1r2, y1y2)
2⊃ pick the second element (r1r2)
×ばつ multiply by four (4r1r2)
Left tine:
- subtract the arguments (x1−x2, r1−r2, y1−y2)
2*⍨ square ((x1−x2)2, (r1−r2)2, (y1−y2)2)
-/ minus reduction i.e. alternate sum* ((x1−x2)2−((r1−r2)2−(y1−y2)2))
Now we use these results as arguments to the middle tine:
0≤⊣ is the left argument greater than or equal to zero
∧ and
≤ the left argument smaller than or equal to the right argument?
* due to APL's right associativity
Mathematica 67 bytes
This checks whether the area of the intersection of two disks is positive.
RegionMeasure@RegionIntersection[{#,#2}~Disk~#3,{#4,#5}~Disk~#6]>0&
-
1\$\begingroup\$ Mathematics. Isn't this Mathematica? \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月27日 08:49:42 +00:00Commented Jul 27, 2017 at 8:49
APL (Dyalog Classic), 17 bytes
(+/∧.≥+⍨)⎕,|0j1⊥⎕
expects two lines of input: x1 y1 x2 y2 and r1 r2
⎕ read and evaluate a line
0j1 imaginary constant i=sqrt(-1)
0j1⊥ decode from base-i, thus computing: i3x1 + i2y1 + i1x2 + i0y2 = -i x1 - y1 + ix2 + y2 = i(x2-x1) + (y2-y1)
| magnitude of that complex number, i.e. distance between the two points
⎕, read the two radii and prepend them, so we have a list of 3 reals
(+/∧.≥+⍨) flat-tolerant triangle inequality: the sum (+/) must be greater than or equal to (≥) the double of each side (+⍨), and all these results must be true (∧.)
Python 3, (削除) 56 (削除ここまで) 55 bytes
lambda X,Y,x,y,R,r:0<=(X-x)**2+(Y-y)**2-(R-r)**2<=4*r*R
-
2
GolfScript, 20 bytes
~@- 2?@@- 2?+@@+2?>!
Takes arguments in the following form r1 r2 x1 y1 x2 y2
Implements the formula below
(x2 - x1)^2 + (y2 - y1)^2 <= (r1 + r2)^2
GolfScript Does not inherently support floating point types. See this post for more details about passing floats https://codegolf.stackexchange.com/a/26553
My original intention was to use GolfScript's zip and fold operations.
GolfScript, 26 bytes
1:a;~zip{{a*+2?-1:a;}*}/+>
-
\$\begingroup\$ @JamesHolderness I forgot to mention that little quirk in GolfScript. See the edit concerning this. \$\endgroup\$Marcos– Marcos2017年12月03日 15:11:34 +00:00Commented Dec 3, 2017 at 15:11
-
\$\begingroup\$ Just updated the input to be an implementation of the truthy float test. The values aren't exact, but it still works. \$\endgroup\$Marcos– Marcos2017年12月03日 16:31:05 +00:00Commented Dec 3, 2017 at 16:31
JavaScript, 52 bytes
(X,Y,x,y,R,r)=>4*r*R/((X-x)**2+(Y-y)**2-(R-r)**2)>=1
Some how Based on Anders Kaseorg Python solution
Scala, 59 bytes
This might be golfable.
val u=(a-x)*(a-x)+(b-y)*(b-y)
(q-r)*(q-r)<=u&u<=(q+r)*(q+r)
Scala, 66 bytes
I do like this one, but sadly the previous was shorter. I hate Scala for forcing defining parameter type...
var p=math.pow(_:Double,2)
val u=p(a-x)+p(b-y)
p(q-r)<=u&u<=p(q+r)