Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Point within cylinder function  (Read 839 times)
tagget
Customers
Community Member
*****
Posts: 243


« on: January 24, 2009, 09:39:46 PM »

Was looking through old projects and found this function and thought it might be of use to someone. It's based on a C++ algorithm that I've lost the link for.

Code:
Public Function IsPointInCylinder(ByVal vPoint As TV_3DVECTOR, ByVal vStartPoint As TV_3DVECTOR, ByVal vEndPoint As TV_3DVECTOR, ByVal fRadius As Single) As Boolean

    ' Normalize start & end points.
    Dim vStartNormal As TV_3DVECTOR = TVMaths.VNormalize(TVMaths.VSubtract(vStartPoint, vEndPoint))
    Dim vEndNormal As TV_3DVECTOR = TVMaths.VNormalize(New TV_3DVECTOR(-vStartNormal.x, -vStartNormal.y, -vStartNormal.z))

    ' Check if the given point is within the cylinder.
    Dim fDistanceToPlane As Single = TVMaths.VDotProduct(TVMaths.VSubtract(vPoint, vStartPoint), vStartNormal)
    If (fDistanceToPlane > 0) Then
        If TVMaths.VDotProduct(TVMaths.VSubtract(vPoint, vEndPoint), vEndNormal) > 0 Then
            vPoint = TVMaths.VSubtract(vPoint, TVMaths.VMultiply(vStartNormal, fDistanceToPlane))
            If TVMaths.GetDistance3D(vPoint.x, vPoint.y, vPoint.z, vStartPoint.x, vStartPoint.y, vStartPoint.z) < fRadius Then
                ' Point IS inside the cylinder.
                Return True
            Else
                ' Point ISN'T inside the cylinder.
                Return False
            End If
        End If
    End If

 End Function
Logged
Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« Reply #1 on: January 26, 2009, 12:13:20 PM »

That sounds kinda complicated for what it does. I'd do something like... (in XNA)

Code:
public static bool IsInCylinder(this Vector3 p, float radius, float height, Vector3 center)
{
var xzDiff = new Vector2(center.x - p.x, center.z - p.z);
return (xzDiff.Length() < radius && Math.Abs(center.y - p.y) < height / 2);
}

(off the top of my head, untested)
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks