how to draw cylinder between points



Hello, i would like to draw a cylinder beween two coordinates. I have
found the following idea in this group and tried to program it. But it
doenst work. maybe i have done something wrong with degrees and rads or
something. if you can point out the mistake(s) i made, please do so.

/*
Let P1=(x1,y1,z1) and P2=(x2,y2,z2) be your endpoints:
1) Calculate V (directional vector of your axis);
V=(v1,v2,v3)=((x2-x1),(y2-y1),(z2-z1));
2) Calculate L = lenght of V = sqrt(v1*v1+v2*v2+v3*v3) (this will the
lenght of your cylinder);
3) Calculate the cosine angle between between V and the z-axis (wich is

the default cylinder axis in Opengl)
c=cos(theta)=v3/L;
4) Calculate the N, the normal vector to both V and the Z-axis (this
the the normal direction to the plane they span, it will be
your rotation axis);
N= V X K = (n1,n2,n3)
=(v2,-v1,0);
5) Calculate the sin between between V and the z-axis:
s=sin(theta)= sqrt(v1*v1+v2*v2 )/sqrt(v1*v1+v2*v2+v3*v3 );
6) Calculate the angle: theta =arctan(s/c);
7) Your are ready to proceed:


.... glrotatef(theta,v2,-v1,0);
gltranslatef(x1,y1,z1);
gluCylinder(cyl,radius,radius,L,lat,long);

*/
void drawPipeT(float x1, float y1, float z1, float x2, float y2, float
z2)
{
float v1, v2, v3;
float n1, n2, n3;
float vLenght;
float c;
float s;
float theta;

v1 = x2-x1;
v2 = y2-y1;
v3 = z2-z1;
vLenght = sqrt(v1*v1 + v2*v2 + v3*v3);
c = v3/vLenght;
n1 = v2; n2 = -v1; n3 = 0;
s = sqrt(v1*v1 + v2*v2)/sqrt(v1*v1 + v2*v2 + v3*v3);
theta = atan(s/c);

GLUquadricObj *cylinder1;
cylinder1 = gluNewQuadric();
gluQuadricDrawStyle(cylinder1,GLU_FILL);

glColor3d(0,0,1);
glPushMatrix();
glRotatef(theta,n1,n2,n3);
//glTranslated(x1,y1,z1); need to get rotation ok first
gluCylinder(cylinder1,0.1,0.1,vLenght,10,1);
glPopMatrix();
// from here code find the errors

// draw N
glPushMatrix();
glLineWidth(4.0); // fat line
glColor3d(1,0,0);
glBegin( GL_LINES );
glVertex3f( 0, 0, 0);
glVertex3f( n1, n2, n3);
glEnd();
glPopMatrix();

// draw endpoints
glPushMatrix();
glTranslated(x1,y1,z1);
glutSolidSphere(0.3,12,12);
glPopMatrix();

glPushMatrix();
glTranslated(x2,y2,z2);
glutSolidSphere(0.3,12,12);
glPopMatrix();
}

.



Relevant Pages

  • Re: Beginner and Vectors
    ... >> I've created my spheres and positioned them and it looks cool. ... >> I've created a cylinder which is the same length as ... >> private void DrawCylinder(float xStart, float yStart, float zStart, float ... > A cylinder created with Mesh.Cylinder will have its axis along the Z-axis. ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: Beginner and Vectors
    ... > I've created my spheres and positioned them and it looks cool. ... > I've created a cylinder which is the same length as ... > private void DrawCylinder(float xStart, float yStart, float zStart, float ... A cylinder created with Mesh.Cylinder will have its axis along the Z-axis. ...
    (microsoft.public.win32.programmer.directx.managed)
  • 3D Graphics in MFC
    ... I want to draw some simple 3D graphics in a MFC SDI Application. ... graphics consist of a cylinder, which may have multiple radius, and on that ... cylinder I want to draw Text and if possible also bitmap images. ... Please correct me so I may improve my English! ...
    (microsoft.public.vc.mfc)
  • gluCylinder in OpenGL-0.54
    ... draw a cylinder. ... The perl code below yields an empty window (no ... my $quad = OpenGL::gluNewQuadric; ...
    (comp.lang.perl.modules)
  • Re: 3D Graphics in MFC
    ... I want to draw some simple 3D graphics in a MFC SDI Application. ... graphics consist of a cylinder, which may have multiple radius, and on that ... cylinder I want to draw Text and if possible also bitmap images. ...
    (microsoft.public.vc.mfc)