The Online POV-Ray Tutorial

POV-Ray Object: Chain

This is an include file that creates a "Chain" object at the origin, stretching along the +z axis. You specify the texture, link length, link thickness, and number of links you want in your chain, and it does the rest!
// Chain Object Include File
// David J.V. Brown
// Questions/comments: DrDave@POBoxes.com
// April 25, 1997

/*
INSTRUCTIONS:
             In your .pov scene file, declare the following:
             ChainTex = [texture]; texture for the links of the chain.
             MajR = [FLOAT]; major radius of torus, corresponds to length of individual link.
             MinR = [FLOAT]; minor radius of torus, corresponds to thickness of a link.
             NLinks = [INT]; number of links you want the chain to contain.

             After those are declared, add this line:
             #include "chain.inc"

             An object called Chain will be declared starting at the origin with the first
             link in the xz plane and extending NLinks along +z.  You can then add instances
             of the object and translate/rotate it however you like.
*/

// Main Declaration Section

#declare Origin = <0,0,0>

// Objects

#declare WholeR = MajR + (2 * MinR) + 0.05
/* HalfBoxP1 = bottom near left point of box used to cut torus in half
   HalfBoxP2 = top far right point of box used to cut torius in half
   -- 0.05 is added length to ensure that cutting box extends past torus --
*/
#declare HalfBoxP1 = Origin - (x*WholeR)  - (y*MinR + 0.05)
#declare HalfBoxP2 = Origin + (x*WholeR) + (y*MinR + 0.05) + (z*WholeR)

#declare Ring = torus {
                        MajR, MinR
                        texture {ChainTex}
                          }

// Object used to cut torus in half before adding extenders to create a link.
#declare HalfBox = box { HalfBoxP1, HalfBoxP2
                         texture {ChainTex}
                       }

/* Extender: the cylinder used to make the chain links oblong.
             Created at origin and extends along the +z axis, to be translated later.
   ExtTrans: the +/- x translation factor to line the extender up with the HalfRing.
*/

#declare ExtTrans = x * MajR
#declare Extender = cylinder {Origin, MajR*z, MinR
                             texture {ChainTex}
                             }

/* HalfRing: the original torus cut in half.
   HalfTrans: the +z translation to put the flipped HalfRing on to close the Link.
   Flip: flips the 1st HalfRing over about the x axis to line it up right.
   Note:  Flip first, THEN translate, or it'll all get screwed up!
*/
#declare Flip = x*180
#declare HalfTrans = z*MajR-0.01
#declare HalfRing = difference
         {
         object { Ring }
         object { HalfBox }
         }

 // Definition of one completed chain link:
#declare Link = merge
         {
         object {HalfRing}
         object {Extender translate ExtTrans+(-0.01*z)}
         object {Extender translate -ExtTrans+(-0.01*z)}
         object {HalfRing
                         rotate Flip
                         translate HalfTrans}
         }

/* Now create a procedure to generate a chain of NLinks length.
   The chain will extend in the +z direction.
*/

#declare Chain = Link           // Initialize the Chain

#declare NLinks = NLinks - 1    // Correct for initializaton link


#declare Count = 1
#while (Count <= NLinks)
       #declare LinkTrans = z * ((3*MajR) - (MinR*2)) * Count

       #declare LinkRot = z * Count * 90

       #declare Chain = union
                {
                object{Chain}
                object {Link
                       rotate LinkRot
                       translate LinkTrans
                       }
                }

       #declare Count = Count + 1
#end

Object Library Main Page