How to fill the gap between two cylinders

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to fill the gap between two cylinders

Irene Tang
Hey, I drew many small cylinders. First, I used a array to store all data, e.g. {x1,y1,z1,x2,y2,z2,x3,y3,z3}.
I wrote a function that can draw a cylinder according to two inputted points (x1,y1,z1) and (x2,y2,z2), or (x2,y2,z2) and (x3,y3,z3). Here is the function.

public static void drawCylinder(GL2 gl2, double x1, double y1, double z1, double x2, double y2, double z2, double radius) {
                double dx = x2 - x1;  
                double dy = y2 - y1;  
                double dz = z2 - z1;  

                double  distance = Math.sqrt(dx*dx + dy*dy + dz*dz);  

                double  px = x1;  
                double  py = y1;  
                double  pz = z1;  

                double  bx = px;    
                double  by = py;
                double  bz = distance + pz;  

                double  sx = bx - x1;  
                double  sy = by - y1;  
                double  sz = bz - z1;  
               
                double fx = sy * dz - dy * sz;  
                double fy = sz * dx - sx * dz;  
                double fz = sx * dy - dx * sy;  
               
                double ax = x2 - bx;  
                double ay = y2 - by;  
                double az = z2 - bz;  
                double length = Math.sqrt(ax * ax + ay * ay + az * az);  

                float angle = (float)(Math.acos((distance*distance*2 - length*length)/(2*distance*distance))*180.0f / Math.PI);
               
                GLUT glut = new GLUT();
            gl2.glPushMatrix();  
            gl2.glTranslated(x1, y1, z1);  
            gl2.glRotated(angle, fx, fy, fz);    
            glut.glutSolidCylinder(radius, distance, 32, 32);    
            gl2.glPopMatrix();  
        }
There are many small gaps like this:. How can I fill these gaps?
Reply | Threaded
Open this post in threaded view
|

Re: How to fill the gap between two cylinders

gouessej
Administrator
Hi

Use gluCylinder with 2 different values at the base and at the top. It's more flexible than glutSolidCylinder.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to fill the gap between two cylinders

Irene Tang
I don't get it. How does it make the gaps disappear? I tried. I looks like this
Reply | Threaded
Open this post in threaded view
|

Re: How to fill the gap between two cylinders

gouessej
Administrator
Actually, you can keep your source code unchanged to draw the cylinders with glutSolidCylinder and you can draw some cylinders with different radius at the top and at the base to fill your gaps with gluCylinder.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to fill the gap between two cylinders

Irene Tang
Hi, I still don't get it. I wrote a demo to explain it. First, I drew c1 according to p(0,0,0) and p(0,0,1), and c2 according to p(0,0,1) and p(1,0,2),
Then I calculated the angle between two cylinders and drew many disks to fill the gap. It works well when there is only one gap. But it slows down the project since there are so many gaps in the real project.

You said I could use gluCylinder with different base and top value to fill the gaps. How to set the base and top that can perfectly fit the gaps?