Skip to content

Instantly share code, notes, and snippets.

@ikekou
Last active December 17, 2015 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikekou/5588434 to your computer and use it in GitHub Desktop.
Save ikekou/5588434 to your computer and use it in GitHub Desktop.
[Math][JavaScript][CoffeeScript] 2点を通る直線と円の交点を求める
#直線の座標A、直線の座標、円の中心点の座標、円の半径
getPointsOfIntersectionWithLineAndCircle=(aX,aY,bX,bY,cX,cY,r)->
a = bY - aY
b = aX - bX
c = -( a*aX + b*aY )
l = Math.sqrt((bX-aX)*(bX-aX)+(bY-aY)*(bY-aY))
eX = (bX - aX) / l
eY = (bY - aY) / l
vX = -eY
vY = eX
k = - (a*cX + b*cY + c)/(a*vX+b*vY)
pX = cX + k*vX
pY = cY + k*vY
if r<k
return false
else
S = Math.sqrt( r*r - k*k )
x1 = pX + S*eX
y1 = pY + S*eY
x2 = pX - S*eX
y2 = pY - S*eY
return [x1,y1,x2,y2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment