Intersection in a circle of a vector inside a circle

I have a circle. There is a dot inside the circle. At the moment I have a vector. I would like to know which point on the circle intersects this vector. Here is the drawing:

http://n4te.com/temp/circle.png http://n4te.com/temp/circle.png

The red dot is the dot that I am trying to identify.

I know these things: the center of the circle, the beginning of the vector, and the direction of the vector.

I know this is the main material, but I still have problems. Most Googlings bring me a linear circle collision, which is connected, but not quite so. Thanks for any help you can provide!

+3
source share
2 answers

Elementary vector algebra .

O — center of circle (vector)
r — its radius       (scalar)
A — origin of ray    (vector)
k — direction of ray (vector)

(A + kt - O)² = r² t, , A + kt - .

:

. dot product, ² . LHS

(A + kt - O)² = (A - O)² + 2(k.(A - O))t + k²t².

k²t² + 2(k.(A - O))t + (A - O)² - r² = 0. (rayVX² + rayVY²)t² + 2(rayVX(rayX - circleX) + rayVY(rayY - circleY))t + (rayX - circleX)² + (rayY - circleY)² - r² = 0.

+11

. Java-:

float xDiff = rayX - circleX;
float yDiff = rayY - circleY;
float a = rayVX * rayVX + rayVY * rayVY;
float b = 2 * (rayVX * (rayX - circleX) + rayVY * (rayY - circleY));
float c = xDiff * xDiff + yDiff * yDiff - r * r;
float disc = b * b - 4 * a * c;
if (disc >= 0) {
    float t = (-b + (float)Math.sqrt(disc)) / (2 * a);
    float x = rayX + rayVX * t;
    float y = rayY + rayVY * t;
    // Do something with point.
}
+7

Source: https://habr.com/ru/post/1719802/


All Articles