Last active
March 27, 2019 13:04
-
-
Save yuba/f592c01949e4e3b4d107 to your computer and use it in GitHub Desktop.
図形情報を扱うのに大事なことは、全部高校で教わった ref: https://qiita.com/yuba/items/2f35d1e5e162f44fc1b3
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
typedef struct{ | |
double x; | |
double y; | |
} vector2D; | |
vector2D nearest(vector2D A, vector2D B, vector2D P) { | |
vector2D a, b; | |
double r; | |
a.x = B.x - A.x; | |
a.y = B.y - A.y; | |
b.x = P.x - A.x; | |
b.y = P.y - A.y; | |
// 内積 ÷ |a|^2 | |
r = (a.x * b.x + a.y * b.y) / (a.x * a.x + a.y * a.y); | |
if(r <= 0){ | |
return A; | |
}else if(r >= 1){ | |
return B; | |
}else{ | |
vector2D result; | |
result.x = A.x + r * a.x; | |
result.y = A.y + r * a.y; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment