5
\$\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
asked Jul 27, 2017 at 2:47
\$\endgroup\$
7
  • 3
    \$\begingroup\$ So this is the same as the other one except for if one circle is entirely inside the other? \$\endgroup\$ Commented Jul 27, 2017 at 3:05
  • \$\begingroup\$ @geokavel essentially, yes \$\endgroup\$ Commented Jul 27, 2017 at 3:07
  • 2
    \$\begingroup\$ The slight variation makes for a very different problem. Good job! \$\endgroup\$ Commented Jul 27, 2017 at 8:35
  • \$\begingroup\$ Related. \$\endgroup\$ Commented 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\$ Commented Jul 27, 2017 at 10:19

7 Answers 7

3
\$\begingroup\$

APL (Dyalog), 20 bytes

Uses Anders Kaseorg's formula: 0≤(x1​−x2)2​−(r1​−r2)2+(y1​−y2)2≤4​r1r2

Takes (x1, r1, y1) as left argument and (x2, r2, y2) as right argument.

(-/2*⍨-)(≤∧0≤⊣)×ばつ

Try it online!

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 (4​r1r2)

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

answered Jul 27, 2017 at 8:40
\$\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&
answered Jul 27, 2017 at 8:30
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Mathematics. Isn't this Mathematica? \$\endgroup\$ Commented Jul 27, 2017 at 8:49
3
\$\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 (∧.)

answered Feb 4, 2018 at 14:52
\$\endgroup\$
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!

answered Jul 27, 2017 at 2:48
\$\endgroup\$
1
  • 2
    \$\begingroup\$ -1 byte. \$\endgroup\$ Commented Jul 27, 2017 at 3:14
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!

answered Dec 3, 2017 at 9:35
\$\endgroup\$
2
  • \$\begingroup\$ @JamesHolderness I forgot to mention that little quirk in GolfScript. See the edit concerning this. \$\endgroup\$ Commented 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\$ Commented Dec 3, 2017 at 16:31
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

answered Jul 27, 2017 at 10:00
\$\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!

answered Jul 27, 2017 at 9:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.