View Full Version : How can we draw a cylinder using two points in 3D space

Hi,

I was wonderring how we can draw a cylinder using any two points in 3D space?

I tried using glu cylinder but I can only translate to 1 point but what about the other point, is there a way I can draw a cylinder between two points in 3D space?

A_Newbie

You need to translate the cylinder to the first point and rotate it towards the other point. The height of the cylinder is equal to the distance between the two points.

The problem is to find the proper rotation matrix, but I think you can use gluLookAt in some way to build the required transformation matrix.

Fastian

03-29-2001, 01:45 AM

You can first translate to the first point then

let the second vector be

v2 = (30,30,30)

calculate the angle on the x and y plane using

v2 = (30, 30, 0) and unit along x axis = 1,0,0

i.e. theta1 = angle bw v2 and unitX;

similarly find angle on x and z plane as theta2 with v2 = (30,0,30) then

glRotatef(theta1,1,0,0);

glrotatef(90-theta2,0,1,0);

drawcylinder().

[This message has been edited by Fastian (edited 03-29-2001).]

Hello,

I tried this but it did not work. Here is the code that I used. I have pos1 and pos2 which are structures with x, y, and z members. dx, dy, and dz are the deltas between pos1.y and pos2.x, and .z. dxSquared is dx*dx, etc.

glPushMatrix ();

glTranslatef (pos1->x, pos1->y, pos1->z);

// rotate about x axis

opp = pos2->y - pos1->y;

adj = pos2->z - pos1->z;

theta = atan (opp/adj);

theta = 360 * (theta / (2*3.1415926));

glRotatef (-theta, 1.0, 0.0, 0.0);

// rotate about y axis

opp = pos2->x - pos1->x;

adj = pos2->z - pos1->z;

theta = atan (opp/adj);

theta = 360 * (theta / (2*3.1415926));

glRotatef (90-(-theta), 0.0, 1.0, 0.0);

gluCylinder (q, radius, radius, height, 20, 20);

glPopMatrix ();

Can someone tell me what I am doing wrong? I think it has something to do with the non-communicative nature of rotational transformations (i.e., the order of the rotation matters -- the first rotation changes the angle required by the second).

Somewhere, someone must have actual working code that draws a cylindar (including the end disks!) when given two points in cartesian coordinate space. Help!

Originally posted by Fastian:

You can first translate to the first point then

let the second vector be

v2 = (30,30,30)

calculate the angle on the x and y plane using

v2 = (30, 30, 0) and unit along x axis = 1,0,0

i.e. theta1 = angle bw v2 and unitX;

similarly find angle on x and z plane as theta2 with v2 = (30,0,30) then

glRotatef(theta1,1,0,0);

glrotatef(90-theta2,0,1,0);

drawcylinder().

[This message has been edited by Fastian (edited 03-29-2001).]

Fastian

10-14-2001, 08:34 PM

void CGraphics: http://www.opengl.org/discussion_boards/ubb/biggrin.gifrawCylinder(const RGFW3f *startPoint, float angleXY, float angleXZ, float length, float baseRadius, float topRadius, RGFW4f *StartColor, RGFW4f *EndColor)

{

glPushMatrix();

// move to the starting position of the pipe

glTranslatef(startPoint->x, startPoint->y, startPoint->z);

// orient the pipe to be perpendicular to the screen

glRotatef(90, 0, 1, 0);

// rotate on the xy plane i.e. around the z axis. since pipe is already rotated

// by 90 degree therefore our z has become the x axis in this case.

glRotatef(angleXY, 1, 0, 0);

// rotate on the xz plane i.e. around the Y axis.

glRotatef(angleXZ, 0, 1, 0);

// draw the pipe

gluCylinder(cylinder, baseRadius, topRadius, length, 18, 1);

glPopMatrix();

}

Hope this helps

Fastian

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.