Я пытаюсь определить, пересекаются ли бесконечный цилиндр и круг в трехмерном пространстве. Это было задано здесь: Нахождение пересечения Круга и Бесконечного Цилиндра в трехмерном пространстве
Однако, только математик может понять ответ Ив Дауста. Есть еще один ответ от MBo, который я кодировал (ниже). К сожалению, тестирование показывает, что оно не работает должным образом. Я ищу помощь в этом, что нематематик может понять. Заранее спасибо!
// cylinderLoc = infinite cylinder location (any location on the cylinder axis)
// cylinderDir = infinite cylinder direction (normalized)
// cylinderRadius = infinite cylinder radius
// circleLoc = circle location (circle center)
// circleDir = circle direction (normalized direction of the circle plane)
// circleRadius = circle radius
bool cylinderIntersectCircle(
Vector3 cylinderLoc, Vector3 cylinderDir, double cylinderRadius,
Vector3 circleLoc, Vector3 circleDir, double circleRadius)
{
// get the perpendicular distance from the circle center to the cylinder axis
Vector3 diff = Vector3.Subtract(circleLoc, cylinderLoc);
diff = Vector3.Cross(cylinderDir, diff);
double distance = diff.Length(); // the length is also called the magnitude
// get the dot product (cosine) between the cylinder and circle directions
double dot = Vector3.Dot(cylinderDir, circleDir);
// determine if the cylinder and circle intersect
return (distance <= cylinderRadius + circleRadius * Abs(dot));
}
ОБНОВЛЕНИЕ: Вот изображение, показывающее, что может сделать проще. Мне нужно это «сладкое пятно», где ободок круга находится глубоко в месте, которое имеет цилиндр на плоскости круга. Направление от центра круга, которое принимает его ближе всего к контуру цилиндра. ![Cylinder / circle intersection](https://i.stack.imgur.com/bJHRc.png)
ОБНОВЛЕНИЕ 2: Вот несколько примеров номеров для MBo, чтобы увидеть, которые демонстрируют его алгоритм, возвращающий false, когда он должен возвращать true. Ниже это изображение результата. Я сделал каждый объект разным цветом, чтобы помочь. Камера поворачивается на 180 градусов для лучшего обзора (если смотреть сзади). Зеленая рамка - это «расстояние». Синяя рамка - "цилиндр-радиус + круг-радиус * абс (точка)".
cylinderLoc = ( 0.0, 0.0, 0.0 )
cylinderDir = ( 0.0, 1.0, 0.0 )
cylinderRadius = 0.3
circleLoc = ( -0.25, 0.0, -0.5 )
circleDir = ( -0.6, -0.5, 0.6245 )
circleRadius = 0.45
// get the perpendicular distance from the circle center to the cylinder axis
Vector3 diff = Vector3.Subtract(circleLoc, cylinderLoc);
// ---> diff = ( -0.25, 0.0, -0.5 ) - ( 0.0, 0.0, 0.0 )
// ---> diff = ( -0.25, 0.0, -0.5 )
diff = Vector3.Cross(cylinderDir, diff);
// ---> diff = cross(( 0.0, 1.0, 0.0 ), ( -0.25, 0.0, -0.5 ))
// ---> cross.x = 1.0 * -0.5 - 0.0 * 0.0 = -0.5
// ---> cross.y = 0.0 * -0.25 - -0.5 * 0.0 = 0.0
// ---> cross.z = 0.0 * 0.0 - -0.25 * 1.0 = 0.25
// ---> diff = ( -0.5, 0.0, 0.25 ));
double distance = diff.Length(); // the length is also called the magnitude
// ---> distance = Sqrt(-0.5 * -0.5 + 0.0 * 0.0 + 0.25 * 0.25)
// ---> distance = Sqrt(0.25 + 0.0 + 0.0625)
// ---> distance = Sqrt(0.3125)
// ---> distance = 0.55901699437494742410229341718282 (0.559 is close enough)
// get the dot product (cosine) between the cylinder and circle directions
double dot = Vector3.Dot(cylinderDir, circleDir);
// ---> dot = dot((0.0, 1.0, 0.0), (-0.6, -0.5, 0.6245))
// ---> dot = 0.0 * -0.6 + 1.0 * -0.5 + 0.0 * 0.6245
// ---> dot = -0.5
// determine if the cylinder and circle intersect
return (distance <= cylinderRadius + circleRadius * Abs(dot));
// ---> return (0.559 <= 0.3 + 0.45 * Abs(-0.5));
// ---> return (0.559 <= 0.525);
// ---> This returns false, but the circle does in fact intersect the cylinder.
![enter image description here](https://i.stack.imgur.com/bxiHH.png)