7
\$\begingroup\$

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
\$\endgroup\$
  • 3
    \$\begingroup\$ So this is the same as the other one except for if one circle is entirely inside the other? \$\endgroup\$ – geokavel Jul 27 '17 at 3:05
  • \$\begingroup\$ @geokavel essentially, yes \$\endgroup\$ – micsthepick Jul 27 '17 at 3:07
  • 1
    \$\begingroup\$ The slight variation makes for a very different problem. Good job! \$\endgroup\$ – Adám Jul 27 '17 at 8:35
  • \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Jul 27 '17 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 Jul 27 '17 at 10:19
3
\$\begingroup\$

APL (Dyalog), 20 bytes

Uses Anders Kaseorg's formula: 0≤(x₁​−x₂)²​−(r₁​−r₂)²+(y₁​−y₂)²≤4​rr

Takes (x₁, r₁, y₁) as left argument and (x₂, r₂, y₂) as right argument.

(-/2*⍨-)(≤∧0≤⊣)4×2⊃×

Try it online!

The overall function's structure is a fork (3-train) where the tines are -/2*⍨- and ≤∧0≤⊣ and 4×2⊃×*. 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 (xx₂, rr₂, yy₂)
2⊃ pick the second element (rr₂)
 multiply by four (4​rr₂)

Left tine:
- subtract the arguments (x₁​−x₂, r₁​−r₂, y₁​−y₂)
2*⍨ square ((x₁​−x₂)², (r₁​−r₂)², (y₁​−y₂)²)
-/ minus reduction i.e. alternate sum* ((x₁​−x₂)²​−((r₁​−r₂)²​−(y₁​−y₂)²))

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

\$\endgroup\$
3
\$\begingroup\$

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&
\$\endgroup\$
  • 1
    \$\begingroup\$ Mathematics. Isn't this Mathematica? \$\endgroup\$ – Mr. Xcoder Jul 27 '17 at 8:49
2
\$\begingroup\$

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

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ -1 byte. \$\endgroup\$ – notjagan Jul 27 '17 at 3:14
  • \$\begingroup\$ @notjagan Sure, thanks! \$\endgroup\$ – Anders Kaseorg Jul 27 '17 at 3:28
2
\$\begingroup\$

GolfScript, 20 bytes

~@- 2?@@- 2?+@@+2?>!

Try it online!

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;}*}/+>

Try it online!

\$\endgroup\$
  • \$\begingroup\$ @JamesHolderness I forgot to mention that little quirk in GolfScript. See the edit concerning this. \$\endgroup\$ – Marcos Dec 3 '17 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 Dec 3 '17 at 16:31
2
\$\begingroup\$

APL (Dyalog Classic), 17 bytes

(+/∧.≥+⍨)⎕,|0j1⊥⎕

Try it online!

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 (∧.)

\$\endgroup\$
1
\$\begingroup\$

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

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

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)

Try it online!

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)

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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