3
\$\begingroup\$

Given 2 random circle sections, return how many times they intersect. Rules/clarifications are as below.

  1. You are provided 10 total values,
    • The center (x,y) of the circle section
    • Angles of the circle (i.e. (0,90) would be a quarter circle "facing" the top-right corner.
    • The radius of the circle section
  2. The circle sections are "drawn" on a 128 by 128 screen, with (0,0) being the center. They have a max radius of 64.
  3. You don't need to draw the circles - the program should only print out the number of intersections.
  4. You can choose to generate the values randomly, in-program, or have the user write it in at program start. (The latter is preferred, but not necessary.)

Below is an example case. As you can see, it has one intersect.

example

The output should look something like this:

$ run ./circle
input (circle 1 center)
input (circle 1 radius)
input (circle 1 angles)
input (more inputs...)
... 
intersections

Heres the output for the example case (the prompts are optional):

$ run ./circle
c1center: (20,20)
c1radius: 40
c1angles: (210,150)
c2center: (-30,-30)
c2radius: 90
c2angles: (270,180
1

Anyway, best of luck! This is code golf, so the minimum number of bytes in any programming language wins!

Oh, and it'd be nice if you could provide a description/link to your program in the answer. Standard Loopholes are not allowed. If you find any errors in my explanation or want me to clarify, just ask. Thanks!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ May we assume that the circles are distinct so that there are not infinite intersections? \$\endgroup\$
    – notjagan
    Commented Dec 13, 2017 at 2:15
  • \$\begingroup\$ @notjagan yes, circles are completely different in that respect. \$\endgroup\$
    – i..
    Commented Dec 13, 2017 at 2:16
  • \$\begingroup\$ What if the intersection is outside the monitor/screen? \$\endgroup\$
    – user202729
    Commented Dec 13, 2017 at 2:41
  • \$\begingroup\$ @user202729 It should be counted - only the center of the circles themselves need to be on-screen. I hope this helps. \$\endgroup\$
    – i..
    Commented Dec 13, 2017 at 3:41
  • 1
    \$\begingroup\$ Note that challenges should be completely clear since it was posted, because edits to the question may validate existing answers. That's a common mistake with challenges of new users. To achieve that, using the Sandbox is often helpful. \$\endgroup\$
    – user202729
    Commented Dec 13, 2017 at 3:59

1 Answer 1

3
\$\begingroup\$

Python 3 + numpy, 310 291 bytes

-19 bytes thanks to ovs!

v=lambda c,x,y:sin(arctan2(*c[::-1])-x)>sin(y-x)
def f(a,r,w,x,b,s,y,z):
 d=linalg.norm(b-a)
 if abs(r-s)<d<r+s:e=(r*r-s*s+d*d)/2/d;g=a+e*(b-a)/d;t=(sqrt(r*r-e*e)*(b-a)/d*[1,-1])[::-1];return int(v(g+t-a,w,x)and v(g+t-b,y,z))+int(v(g-t-a,w,x)and v(g-t-b,y,z))-(t==0).all()
from numpy import*

Try it online!

This is a horrible answer.

Takes angles in radians.

\$\endgroup\$
2
  • \$\begingroup\$ Pretty good answer. I was wondering if anyone could do it in less than 500 bytes. +1 \$\endgroup\$
    – i..
    Commented Dec 13, 2017 at 3:39
  • 1
    \$\begingroup\$ 291 bytes, no need to convert bool to int, a%(2*pi)<b%(2*pi) is equivalent to sin(a)>sin(b) ... \$\endgroup\$
    – ovs
    Commented Dec 13, 2017 at 13:32

Your Answer

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

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