Sign in to follow this  
gauntalus

Manually calculating vertices for a 3D cylinder

Recommended Posts

Hey all. I'm trying to manually generate the vertices for a 3D cylinder for a research project that I'm working on for school. Basically, I have been given two points which will be the center of each end of the cylinder, as well as the radius. I'll also be provided with the "order" of the cylinder, like the number of sides it will need. Note that none of this will use openGL, I'm generating STL files for a stereolithography machine. I've already implemented simple point and vector classes so that I can read in the two end points, and generate a vector from P1 to P2. Next, I need some way of finding a point of basically arbitrary orientation, the only stipulations being that it be of distance "radius" from one of the end points, and assuming we call the new point P3, the angle P3-P1-P2 (or P3-P2-P1 as it may be) must be a right angle. I figured there might be a way to calculate a perpendicular vector to P1-P2, then normalize, and finally do a scalar multiplication against the radius to find and vector that would translate from P1 to P3. However, I don't know how to find a, as I said before, somewhat arbitrarily oriented perpendicular vector to P1-P2. I'm sorry if that question was rediculously unclear. If you have trouble understanding what I'm trying ask, let me know and I'll rephrase the question with pretty gimp'd pictures and arrows etc. Thanks everyone.

Share this post


Link to post
Share on other sites
This forum has a pretty strict no-homework policy, but I assume research project != homework?

Generating the vertices for an arbitrarily oriented cylinder is a common problem that is fairly straightforward to solve. It sounds like you've got the general idea except for generating the orthonormal basis vectors you'll use to find the endcap vertices. Let W = normalize(P2-P1). As you've noted you need a unit-length vector 'U' that is perpendicular to W. It will also be convenient to have a unit-length vector V perpendicular to both W and U. U, V, and W form the orthonormal basis for the cylinder.

To find a vector perpendicular to W, simply cross W with the world x, y, or z axis. The only caveat is that two vectors which are nearly aligned will produce a cross product with a very small magnitude, which you may not be able to normalize. To avoid this problem, simply cross W with the world axis corresponding to the component of W whose absolute value is least. Then, normalize the result to get U. Finally, V = Cross(W, U).

You now have two coordinate systems, each with basis vectors U, V, W, and with origins P1 and P2 respectively. You can then use simple trig to find the vertices of the endcaps.

Let me know if I left anything out or if you have further questions.

Share this post


Link to post
Share on other sites
Thanks a lot. I can't believe that I overlooked that solution, I have like two pages of sketches scribbled up and somehow never came to that conclusion.

FYI the project isn't for homework, the research proj is for constructing ceramic lenses for focusing radiation ;-)

I'm sure this forum will be seeing a lot more of me however, as I and a few of my friends just started up a new project on sourceforge. Take a look if you're into chess and/or death and destruction. http://www.sourceforge.net/projects/brutalchess/

also, our blog is at http://brutalchess.sourceforge.net/

Thanks again! I really appreciate it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this