Author Topic: Point-in-cylinder  (Read 772 times)

Is there any way to find out if a point or rotated cuboid (Z axis only) is inside/intersecting a cylinder with a defined radius and height rotated in any direction?

(Given: XYZ centre of bottom part of cylinder, Normal facing 'up' the cylinder from the bottom to the top, radius, height, [XYZ point] or [XYZ centre of bottom part of cuboid, Z rotation])
« Last Edit: June 15, 2008, 05:50:52 AM by Space Guy »

Sure, theres a way to code it. But I don't have the time/patience to do it. Plus, you're more skilled then me.

This should work, I've tested it with a few simple examples.
Code: [Select]
function isInCylinder(%pos, %radius, %height, %normal, %point)
{
%normal = vectorNormalize(%normal); // just in case
%dist = mAbs(vectorDot(%point, %normal) - vectorDot(%pos, %normal)); // perp. dist. from plane of bottom circle to point
%point2 = vectorAdd(%point, vectorScale(%normal, -%dist));
if(%dist > %height || vectorDist(%pos, %point2) > %radius)
return false;
return true;
}
« Last Edit: June 15, 2008, 01:26:16 PM by Randy »

This was tricky
Code: [Select]
function is_in_cylinder(%pt, %vec, %rad, %ht, %norm)
// "pt" is the point you're checking
// "vec" is actually the bottom of the cylinder
// "rad" and "ht" are the radius and height of the cylinder
// "norm" is the normalized vector relative to the bottom of the cylinder

{
%top = VectorAdd(%vec, VectorScale(%norm, %ht SPC %ht SPC %ht));
%center = VectorAdd(%vec, VectorScale(%norm, (%ht/2) SPC (%ht/2) SPC (%ht/2)));
%vec1 = VectorSub(%pt, %top);
%vec2 = VectorSub(%top, %vec);
%dist = mAbs( VectorCross(%vec1, %vec2) ) / VectorLen(%vec2);

if( %dist > %rad ) return false;
else
{
%start = 0;
%hit = VectorScale(%norm, %start);
while( true )
{
if( VectorDist(%pt, %hit) - %dist < 0.005 )
break;
else if( %start > %ht )
return false;

%start += 0.005;
%hit = VectorScale(%norm, %start);
}

if( VectorDist(%hit, %center) > VectorDist(%vec, %center) )
return false;
else
return true;
}
}
« Last Edit: June 15, 2008, 06:09:12 PM by exidyne »

Thank you! I'll try these out.