圆圈重叠吗?


21

给定中心的坐标和2个圆的半径,请输出它们是否重叠的真实值。

输入项

  • 输入可以通过STDIN或等效的函数参数获取,但不能作为变量。您可以将它们作为单个变量(列表,字符串等)或作为多个输入/参数,以任意顺序使用。

  • 输入将是六个浮点数。这些浮点数最多可以保留3个小数位。坐标可以是正或负。半径将为正。

输出量

  • 输出可以通过STDOUT或函数返回。

  • 程序必须有2个不同的输出-一个用于True值(圆圈确实重叠),另一个用于False输出(它们不重叠)。

测试用例

(输入[(x1, y1, r1), (x2, y2, r2)]以测试用例的元组列表形式提供;您可以采用任何格式的输入)

真正

[(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)]

[(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)]

这是Code Golf,最短答案以字节为单位。


4
如果两个圆圈在外部接触,我们需要返回什么?
JungHwan Min

6
“触摸但不重叠”的技术术语是“切线”,如果没有其他地方,这是几何形状的东西。
dmckee

2
花车似乎是一个非常严格的要求。您能放松一下,使其更具一般性吗?我想在Brain-Flak中解决此问题,但是我不太可能花时间来实现IEEE浮点数,如果我这样做了,无论如何它将是90%的字节数,所以我只是在研究一个浮点数实现。
小麦巫师

4
我还要指出,在很多情况下,浮点数不能精确到“小数点后三位”。我不确定您要答案如何处理,但是现在有点混乱。
小麦巫师

2
我认为您可能会对浮动工具的工作方式有一个基本的误解。因为它们是固定大小的,所以值越大,精度就越低。有一个点,一个浮点数不能精确表示小数点后3位内的所有值。同样,不建议编辑挑战以消除不必要的限制。
Mego

Answers:


18

果冻,5个字节

IA<S}

将两个复数(中心)作为第一个参数,并将两个实数(半径)作为第二个参数。

在线尝试!

怎么运行的

IA<S}  Main link.
       Left argument:  [x1 + iy1, x2 + iy2]
       Right argument: [r1, r2]

I      Increments; yield (x2 - x1) + i(y2 - y1).
 A     Absolute value; yield √((x2 - x1)² + (y2 - y1)²).
   S}  Take the sum of the right argument, yielding r1 + r2.
  <    Compare the results.

该死,我忘了使用复数作为坐标。好一个!:D
HyperNeutrino

出于兴趣,A这里的结果将被视为行向量“中心”的范数吗?(ÆḊ本身就是内容复杂的错误。)
乔纳森·艾伦

1
@JonathanAllan是的,计算中心A的距离作为其差矢量的范数。
丹尼斯

11

JavaScript(ES6),38个字节

将输入作为6个不同的变量x1y1r1x2y2r2

(x,y,r,X,Y,R)=>Math.hypot(x-X,y-Y)<r+R

测试用例


对于以前似乎没有Math.hypot的人
Pureferret


@ V.Courtois传递参数的方式与方法声明不匹配。应该是a:Double,x:Double,b:Double,y:Double,r:Double,q:Double
Arnauld

1
@Arnauld ooh〜谢谢!我应该分开张贴吗?
V. Courtois

@ V.Courtois当然。去吧!
Arnauld


7

MATL,5个字节

ZPis<

输入格式为:

[x1, y1]
[x2, y2]
[r1, r2]

在线尝试!验证所有测试用例

怎么运行的

ZP   % Take two vectors as input. Push their Euclidean distance
i    % Input the vector of radii
s    % Sum of vector
<    % Less than?

不确定是否是我,但是当我使用您的试用链接并按运行时,我收到“错误无法解码服务器的响应”-也不确定是否有帮助,但是您是否考虑过(ab)使用复数,例如在果冻的答案?
丹尼斯·贾赫鲁丁

@DennisJaheruddin嘿,很高兴再次在这里见到你!(1)可能是缓存缓存。您是否尝试过硬刷新?(2)我确实做到了,但我认为它也是5字节数(-|而不是ZP
Luis Mendo

我想这是防火墙。现在,我想知道一种类似-r2而不是的输入格式是否r2会有所帮助,因为那样的话您将需要三个差异,而不是2个差异和一个附加...我最好在画得太深之前运行!
丹尼斯·贾赫鲁丁

我认为否定一个输入是可接受的输入格式。如果您在“在线试用”服务中发现任何问题,请在此处报告?
路易斯·门多

6

R,39个字节

function(k,r)dist(matrix(k,2,2))<sum(r)

接受输入k=c(x1,x2,y1,y2)r=c(r1,r2); 返回FALSE正切圆。

在线尝试!

27个字节:

function(m,r)dist(m)<sum(r)

将输入作为矩阵,将圆心指定为行,并指定半径向量。

在线尝试!


-2个字节function(k,r)dist(matrix(k,2))<sum(r)
djhurio'7

dist(matrix(scan(),2))<sum(scan())
djhurio

6

Python,40个字节

lambda x,y,r,X,Y,R:abs(x-X+(y-Y)*1j)<r+R

在线尝试!

使用Python的复杂算法计算两个中心之间的距离。我假设我们不能将输入点直接当作复数,所以代码将它们表示为x+y*1j





4

APL(Dyalog),10字节

提示输入圆心作为两个复数的列表,然后提示半径作为两个数的列表

(+/⎕)>|-/

在线尝试!

(+/⎕) [是]半径的总和

> 比...更棒

| 的大小

-/⎕ 中心的差异


3

Mathematica,16个字节

Norm[#-#2]<+##3&

输入: [{x1, y1}, {x2, y2}, r1, r2]


Mathematica有一个 RegionIntersection内置功能,但仅此一项就长18个字节...

内置版本:

RegionIntersection@##==EmptyRegion@2&

带2个Disk对象。[Disk[{x1, y1}, r1], Disk[{x2, y2}, r2]]





3

Java 8, 41 38 bytes

(x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R

Try it here.

Apparently, Java also has Math.hypot, which is 3 bytes shorter.

EDIT: Just realized this answer is now exactly the same as @OlivierGrégoire's Java 8 answer, so please upvote him instead of me if you like the 38-byte answer.

Old answer (41 bytes):

(x,y,r,X,Y,R)->(x-=X)*x+(y-=Y)*y<(r+=R)*r

Try it here.


1
Oh! So that's why I got 3 upvotes today, but 0 when the challenge was posted? ^^ I was wondering what triggered this weird behavior ;) Since I like my answer, and you posted the same, you get a +1 as well! :p
Olivier Grégoire


2

Perl 6, 13 bytes

*+*>(*-*).abs

Try it online!

The first two arguments are the radii, in either order. The third and fourth arguments are the coordinates of the centers, as complex numbers, in either order.


2

Taxi, 1582 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Tom's Trims.Go to Tom's Trims:n.[a]Go to Post Office:s.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to What's The Difference.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 5 l.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:s 1 l 2 r 4 l.Pickup a passenger going to Addition Alley.Go to Tom's Trims:s 1 r 3 r.Pickup a passenger going to The Babelfishery.Switch to plan "b" if no one is waiting.Switch to plan "a".[b]Go to Addition Alley:n 1 r 1 l 3 l 1 l.Pickup a passenger going to Magic Eight.Go to Post Office:n 1 r 1 r 3 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 5 l 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:s 1 l 2 r 4 l.Pickup a passenger going to Magic Eight.Go to Magic Eight:s 1 r.Switch to plan "c" if no one is waiting.'1' is waiting at Writer's Depot.[c]'0' is waiting at Writer's Depot.Go to Writer's Depot:w 1 l 2 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.

Try it online!

Outputs 1 for overlapping circles.
Outputs 0 for non-overlapping circles (including tangential circles).

Ungolfed / formatted:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Tom's Trims.
Go to Tom's Trims: north.
[a]
Go to Post Office: south.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: north 5th left.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: south 1st left 2nd right 4th left.
Pickup a passenger going to Addition Alley.
Go to Tom's Trims: south 1st right 3rd right.
Pickup a passenger going to The Babelfishery.
Switch to plan "b" if no one is waiting.
Switch to plan "a".
[b]
Go to Addition Alley: north 1st right 1st left 3rd left 1st left.
Pickup a passenger going to Magic Eight.
Go to Post Office: north 1st right 1st right 3rd right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 5th left 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: south 1st left 2nd right 4th left.
Pickup a passenger going to Magic Eight.
Go to Magic Eight: south 1st right.
Switch to plan "c" if no one is waiting.
'1' is waiting at Writer's Depot.
[c]
'0' is waiting at Writer's Depot.
Go to Writer's Depot: west 1st left 2nd left.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.

2

C#, 50 41 bytes

(x,y,r,X,Y,R)=>(x-=X)*x+(y-=Y)*y<(r+=R)*r

Saved 9 bytes thanks to @KevinCruijssen.


Can't you save a few bytes there by writing (r+R)*2 instead of (r+R)+(r+R)?
Ian H.

@IanH. Yeah don't know how I missed that.
TheLethalCoder

Am I missing something or does this not work?
Ian H.

@IanH. I'd made a typo, the + on the RHS should have been a *.
TheLethalCoder

And my feedback even made that worse. Good job on the solution though!
Ian H.


1

PostgreSQL, 41 characters

prepare f(circle,circle)as select $1&&$2;

Prepared statement, takes input as 2 parameters in any circle notation.

Sample run:

Tuples only is on.
Output format is unaligned.
psql (9.6.3, server 9.4.8)
Type "help" for help.

psql=# prepare f(circle,circle)as select $1&&$2;
PREPARE

psql=# execute f('5.86, 3.92, 1.670', '11.8, 2.98, 4.571');
t

psql=# execute f('8.26, -2.72, 2.488', '4.59, -2.97, 1.345');
t

psql=# execute f('9.32, -7.77, 2.8', '6.21, -8.51, 0.4');
t

psql=# execute f('4.59, -2.97, 1.345', '11.8, 2.98, 4.571');
f

psql=# execute f('9.32, -7.77, 2.8', '4.59, -2.97, 1.345');
f

psql=# execute f('5.86, 3.92, 1.670', '6.21, -8.51, 0.4');
f

1

Java, 50 38 bytes

(x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R

Using ideas in other answers, this can be shortened to 38 like so: (x,y,r,X,Y,R)->Math.hypot(x-X,y-Y)<r+R. In fact, just realised this is the exact same as Arnauld's JavaScript answer.
laszlok

Thanks... This answer was nevee intended to be golfed... i thought it was such a simple challenge there wouldn't be anything that can be golfed...
Roman Gräf

I'm afraid your answer is now exactly the same as the already posted answer by @OlivierGrégoire..
Kevin Cruijssen

1

x86 Machine Code (with SSE2), 36 bytes

; bool CirclesOverlap(double x1, double y1, double r1,
;                     double x2, double y2, double r2);
F2 0F 5C C3        subsd   xmm0, xmm3      ; x1 - x2
F2 0F 5C CC        subsd   xmm1, xmm4      ; y1 - y2
F2 0F 58 D5        addsd   xmm2, xmm5      ; r1 + r2
F2 0F 59 C0        mulsd   xmm0, xmm0      ; (x1 - x2)^2
F2 0F 59 C9        mulsd   xmm1, xmm1      ; (y1 - y2)^2
F2 0F 59 D2        mulsd   xmm2, xmm2      ; (r1 + r2)^2
F2 0F 58 C1        addsd   xmm0, xmm1      ; (x1 - x2)^2 + (y1 - y2)^2
66 0F 2F D0        comisd  xmm2, xmm0
0F 97 C0           seta    al              ; ((r1 + r2)^2) > ((x1 - x2)^2 + (y1 - y2)^2)
C3                 ret

The above function accepts descriptions of two circles (x- and y-coordinates of center point and a radius), and returns a Boolean value indicating whether or not they intersect.

It uses a vector calling convention, where the parameters are passed in SIMD registers. On x86-32 and 64-bit Windows, this is the __vectorcall calling convention. On 64-bit Unix/Linux/Gnu, this is the standard System V AMD64 calling convention.

The return value is left in the low byte of EAX, as is standard with all x86 calling conventions.

This code works equally well on 32-bit and 64-bit x86 processors, as long as they support the SSE2 instruction set (which would be Intel Pentium 4 and later, or AMD Athlon 64 and later).

AVX version, still 36 bytes

If you were targeting AVX, you would probably want to add a VEX prefix to the instructions. This does not change the byte count; just the actual bytes used to encode the instructions:

; bool CirclesOverlap(double x1, double y1, double r1,
;                     double x2, double y2, double r2);
C5 FB 5C C3      vsubsd   xmm0, xmm0, xmm3   ; x1 - x2
C5 F3 5C CC      vsubsd   xmm1, xmm1, xmm4   ; y1 - y2
C5 EB 58 D5      vaddsd   xmm2, xmm2, xmm5   ; r1 + r2
C5 FB 59 C0      vmulsd   xmm0, xmm0, xmm0   ; (x1 - x2)^2
C5 F3 59 C9      vmulsd   xmm1, xmm1, xmm1   ; (y1 - y2)^2
C5 EB 59 D2      vmulsd   xmm2, xmm2, xmm2   ; (r1 + r2)^2
C5 FB 58 C1      vaddsd   xmm0, xmm0, xmm1   ; (x1 - x2)^2 + (y1 - y2)^2
C5 F9 2F D0      vcomisd  xmm2, xmm0
0F 97 C0         seta     al                 ; ((r1 + r2)^2) > ((x1 - x2)^2 + (y1 - y2)^2)
C3               ret

AVX instructions have the advantage of taking three operands, allowing you to do non-destructive operations, but that doesn't really help us to compact the code any here. However, mixing instructions with and without VEX prefixes can result in sub-optimal code, so you generally want to stick with all AVX instructions if you're targeting AVX, and in this case, it doesn't even hurt your byte count.



1

PHP, 66 bytes

<?php $i=$argv;echo hypot($i[1]-$i[4],$i[2]-$i[5])<$i[3]+$i[6]?:0;

Try it online!

Runs from the command line, taking input as 6 command-line parameter arguments, and prints 1 if the circles overlap, else 0.


0

Julia 0.6.0 (46 bytes)

a->((a[1]-a[2])^2+(a[3]-a[4])^2<(a[5]+a[6])^2)

0

Clojure, 68 bytes

#(<(+(*(- %4 %)(- %4 %))(*(- %5 %2)(- %5 %2)))(*(+ %6 %3)(+ %6 %3)))

Takes six arguments: x1, y1, r1, x2, y2, r2. Returns true or false.

Sadly, Clojure does not have a pow function of some sorts. Costs a lot of bytes.


0

Actually, 8 bytes

-)-(h@+>

Try it online!

Explanation:

-)-(h@+>  (implicit input: [y1, y2, x1, x2, r1, r2])
-         y2-y1 ([y2-y1, x1, x2, r1, r2])
 )-       move to bottom, x1-x2 ([x1-x2, r1, r2, y2-y1])
   (h     move from bottom, Euclidean norm ([sqrt((y2-y1)**2+(x2-x1)**2), r1, r2])
     @+   r1+r2 ([r1+r2, norm])
       >  is r1+r2 greater than norm?

0

R (+pryr), 31 bytes

pryr::f(sum((x-y)^2)^.5<sum(r))

Which evaluates to the function

function (x, y, z) 
sum((x - y)^2)^0.5 < sum(z)

Where x are the coordinates of circle 1, y are the coordinates of circle 2 and z the radii.

Calculates the distance between the two centers using Pythagoras and tests if that distance is smaller than the sum of the radii.

Makes use of R's vectorisation to simultaneously calculate (x1-x2)^2 and (y1-y2)^2. These are then summed and squarely rooted.


0

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.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.