Sign in to follow this  
mike74

cylinder

Recommended Posts

Does anyone know the easiest way to draw a cylinder on a line segment? The segment can be oriented any way and can be any length. Basically, it will be a segment with a width of r except the endpoints won't be rounded. Mike C. http://www.coolgroups.com/zoomer

Share this post


Link to post
Share on other sites
You need to be more clear about what you mean by drawing the cylinder "on" the line segment. Will the segment form the axis of the cylinder, or a diameter of one of the endcaps, or just what exactly?

Also, you need to tell us what graphics library you're using, unless you're just looking for an algorithmic view of the problem.

Share this post


Link to post
Share on other sites
Constructing your own cylinder is pretty easy. However, I think OpenGL (or the gl toolkit or something) has built-in functionality for drawing a cylinder. Perhaps that would be sufficient for your purposes.

Share this post


Link to post
Share on other sites
If I use the built-in functionality, I still need to figure out how to translate it, rotate it, and scale it to match the line segment. I'm not exactly sure how to do that.

Mike C.
http://www.coolgroups.com/zoomer

Share this post


Link to post
Share on other sites
Quote:
If I use the built-in functionality, I still need to figure out how to translate it, rotate it, and scale it to match the line segment. I'm not exactly sure how to do that.
Right. So you need a translation and a rotation. Let's see...*goes to look at red book*...OpenGL constructs the cylinder along the z axis, extending from z = 0 to z = height. To me it makes more sense for the cylinder to be centered at the origin, so the first thing we'll do is translate it there.

Then, let's say your line segment is p0->p1. The center is (p0+p1)/2, so this is where we'll translate it to after we've rotated it. For our rotation, we need an axis-angle pair that will rotate the z axis into alignment with the vector p1-p0. So let's try to put it all together:

Vector3 center = (p0+p1)*.5; // Cylinder center
Vector3 v = p1-p0;
float height = v.Length(); // Cylinder height
float z = v.z / height;
if (z < -1) z = -1;
else if (z > 1) z = 1;
float angle = RadToDeg(acos(z)); // Angle by which to rotate
Vector3 axis = Vector3(0,0,1).Cross(v); // Or maybe the other way around
float length = axis.Length();
if (length < epsilon) {angle = 0; axis.Set(0,0,1);}
else {axis /= length;}
glTranslate(center.x, center.y, center.z); // Translate to cylinder center
glRotate(angle, axis.x, axis.y, axis.z); // Rotate into alignment
glTranslate(0, 0, -height * .5); // Translate so centered at origin
DrawGLCylinder(height);

Wow, what a mess - I probably didn't get that right. You might try it though (reverse the cross-product terms if it comes out backwards).

There are other ways to go about this, such as creating an orthonormal basis for the cylinder and/or creating it yourself with some trig. But who knows - maybe the above solution will work...

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