2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cross points of a circle and a line in python

2
Last updated at Posted at 2018-04-05
def project(an,p10):
  p12=an[1]-an[0]
  norm = np.linalg.norm(p12)**2
  #print(norm)
  ret = np.dot(p10,p12)/norm
  #print(ret)
  return an[0]+p12*ret

def getCrossPoints(c, r, l): 
  #print(l)
  pr = project(l, c)
  #print(pr)
  e = (l[1]-l[0]) / (np.linalg.norm(l[1]-l[0]))
  mid = (r*r - np.linalg.norm(pr-c)**2)
  base = np.sqrt(mid)
  #print("==e,base==")
  #print(mid,e,base)
  return [pr-e*base, pr+e*base]

a = []
a.append(map(int, raw_input().split()))
for aa in a:
    cx = aa[0]
    cy = aa[1]
    r = aa[2]
    c = np.array([cx,cy])

n = input()
for i in range(n):
  a = []
  a.append(map(int, raw_input().split()))
  for aa in a:
    hn = np.array(aa).reshape(2,2)
    s1 = np.array([hn[0][0],hn[0][1]])
    s2 = np.array([hn[1][0],hn[1][1]])
    #print(c,r,s1,s2)
    gcp = getCrossPoints(c, r, [s1,s2])
    #print(gcp)
    out = ""
    for x in gcp:
        for xx in x:
            if (out):
                out = out + " " 
            out = out + str(xx)
    print(out)
2 1 1
2
0 1 4 1
1.0 1.0 3.0 1.0
3 0 3 3
3.0 1.0 3.0 1.0

Refs.

2
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?