Last active
December 17, 2015 09:38
-
-
Save ikekou/5588434 to your computer and use it in GitHub Desktop.
[Math][JavaScript][CoffeeScript] 2点を通る直線と円の交点を求める
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#直線の座標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