UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on October 06, 2012, 12:54:26 PM

Title: Polygon Ring
Post by: kevin on October 06, 2012, 12:54:26 PM
  
Polygon Ring

    This snippet draws a zooming circular polygon ring, using the TRIC command.    To plot the shape the code using polar coordinates plotting a pair of coordinates.  So we have an inner set outer set of coordinates.  These are linked back to the previous set of inner and outer  to form the quad.  


[pbcode]

   setfps 100

Do

      cls 255

      ; use a angle to control the motion of the zoom in/out
      ZoomAngle#=wrapangle(ZoomAngle#,-1)

      ; calc it's current inner radius
      InnerRadius=250+sin(ZoomAngle#)*200

      ; draw the ring         
      Ring(angle#,mousex(),mousey(),InnerRadius,$203050,InnerRadius+1000,$405080)


      ; bump the spin angle
      Angle#=wrapangle(Angle#,0.25)


   Sync
loop




Function Ring(SpinAngle#,Xpos,Ypos,InnerRadius,InnerColour,OuterRadius, OuterColour)

   lockbuffer

   AngleStep#=10
   
  For lp =0 to 360 step AngleStep#
 
        ; get the angle for this pair of vertex / cooridnates
        Angle#=wrapangle(lp+SpinAngle#)

         ; calc the inner most coorindates         
        InnerXpos#=XPos+cos(Angle#)*InnerRadius
        InnerYpos#=YPos+sin(Angle#)*InnerRadius
        
         ; calc the Outer most coorindates         
        OuterXpos#=XPos+cos(Angle#)*OuterRadius
        OuterYpos#=YPos+sin(Angle#)*OuterRadius


         ; uncomment to PLot circle to showing the inner ring of  coordinates
         ;   Circle InnerXpos#,InnerYpos#,5,true


         if lp>0  
            ; draw two triangles.  we connect the current inner and Outer coordsin with the previous
            ; inner and Outer values.  
            Tric OuterXpos#,OuterYpos#,InnerXpos#,InnerYpos#,InnerXpos2#,InnerYpos2#,InnerColour
            Tric OuterXpos#,OuterYpos#,InnerXpos2#,InnerYpos2#,OuterXpos2#,OuterYpos2#,OuterColour  
         endif
         
        ; remember this position for the next loop.
        InnerXpos2#=InnerXpos#
        InnerYpos2#=InnerYpos#
        OuterXpos2#=OuterXpos#
         OuterYpos2#=OuterYpos#

  next

   unlockbuffer


EndFunction

[/pbcode]


Gouraud Shaded Version


    Here's a version that uses GouraudTri to shade the ring.  Apart from that it's the same.

[pbcode]

   setfps 100

Do

      cls 255

      ; use a angle to control the motion of the zoom in/out
      ZoomAngle#=wrapangle(ZoomAngle#,-1)

      ; calc it's current inner radius
      InnerRadius=250+sin(ZoomAngle#)*200

      ; draw the ring         
      GouraudRing(angle#,mousex(),mousey(),InnerRadius,$203040,InnerRadius+1000,$a0f080)

      ; bump the spin angle
      Angle#=wrapangle(Angle#,0.25)


   Sync
loop




Function GouraudRing(SpinAngle#,Xpos,Ypos,InnerRadius,InnerColour,OuterRadius, OuterColour)

   lockbuffer

   AngleStep#=10
   
  For lp =0 to 360 step AngleStep#
 
        ; get the angle for this pair of vertex / cooridnates
        Angle#=wrapangle(lp+SpinAngle#)

         ; calc the inner most coorindates         
        InnerXpos#=XPos+cos(Angle#)*InnerRadius
        InnerYpos#=YPos+sin(Angle#)*InnerRadius
        
         ; calc the Outer most coorindates         
        OuterXpos#=XPos+cos(Angle#)*OuterRadius
        OuterYpos#=YPos+sin(Angle#)*OuterRadius


         ; uncomment to PLot circle to showing the inner ring of  coordinates
         ;   Circle InnerXpos#,InnerYpos#,5,true


         if lp>0  
            ; draw two triangles.  we connect the current inner and Outer coordsin with the previous
            ; inner and Outer values.  
            GouraudTri OuterXpos#,OuterYpos#,OuterColour,InnerXpos#,InnerYpos#,InnerColour,InnerXpos2#,InnerYpos2#,InnerColour
            GouraudTri OuterXpos#,OuterYpos#,OuterColour,InnerXpos2#,InnerYpos2#,InnerCOlour,OuterXpos2#,OuterYpos2#,OuterColour  
         endif
         
        ; remember this position for the next loop.
        InnerXpos2#=InnerXpos#
        InnerYpos2#=InnerYpos#
        OuterXpos2#=OuterXpos#
         OuterYpos2#=OuterYpos#

  next

   unlockbuffer


EndFunction


[/pbcode]


Related Examples

    * Draw N- Side Gouruad Polygon outlines (http://www.underwaredesign.com/forums/index.php?topic=3806.0)




Title: Re: Polygon Ring
Post by: kevin on October 31, 2012, 01:46:57 AM

  Perspective Polygon (Gouraud Shaded) Ring

   This is variant of the previous demos, but the ring sizes are perspective projected, giving the appearance of looking down or into a tunnel. 


[pbcode]


   InnerColour=$803040
   OuterColour=$a08070


   Do
      Cls

            CurrentTime=Timer()

            if CurrentTime=>ZoomStartTime and CurrentTime<ZoomEndTime

                  TimePast#=CurrentTime-ZoomStartTime
                  depth=ZoomTargetDepth*(TimePast#/ZoomTotalTime)
            else


               if mousebutton()

                  ZoomTotalTime=2000
                  ZoomStartTime=Timer()
                  ZoomEndTime=ZoomStartTime+ZoomTotalTime
                  ZoomTargetDepth=4000
               endif
            endif
            

            PerspectiveGouraudRing(mousex(),mousey(),Depth,280,300,InnerColour,OuterColour)

      Sync
   loop   
   




Function PerspectiveGouraudRing(Xpos,Ypos,Zdepth,ZInterval,TunnelSize,InnerColour,OuterColour)

   lockbuffer

   AngleStep=5
   While zDepth>ZInterval
      
      
         ; perspectively poject the inner and outter radius of the ring
         InnerRadius=(TunnelSize*450.0)/Zdepth
         Zdepth-=Zinterval
         OuterRadius=(TunnelSize*450.0)/Zdepth

   
         InnerRadius2=InnerRadius*1
         OuterRadius2=OuterRadius*1
   

   
         For lp =0 to 360 step AngleStep
   
            ; get the angle for this pair of vertex / cooridnates
            Angle#=lp

            ca#=cos(Angle#)
            sa#=sin(Angle#)
         
            ; calc the inner most coorindates         
            InnerXpos#=XPos+ca#*InnerRadius
            InnerYpos#=YPos+sa#*InnerRadius2
            ; calc the Outer most coorindates         
            OuterXpos#=XPos+ca#*OuterRadius
            OuterYpos#=YPos+sa#*OuterRadius2

            if lp>0     

               GouraudQuad OuterXpos#,OuterYpos#,OuterColour,InnerXpos#,InnerYpos#,InnerColour,InnerXpos2#,InnerYpos2#,InnerColour,OuterXpos2#,OuterYpos2#,OuterColour

            endif
            ; remember this position for the next loop.
            InnerXpos2#=InnerXpos#
              InnerYpos2#=InnerYpos#
            OuterXpos2#=OuterXpos#
            OuterYpos2#=OuterYpos#

         next lp

         swap InnerColour,OuterColour

   endwhile
   
   unlockbuffer


   if InnerRadius>0
      ; when z is negative use flat ring
      swap InnerColour,OuterColour
      GouraudRing(SpinAngle#,Xpos,Ypos,InnerRadius,InnerColour,InnerRadius+500, OuterColour)
   endif


EndFunction




Function GouraudRing(SpinAngle#,Xpos,Ypos,InnerRadius,InnerColour,OuterRadius, OuterColour)

   lockbuffer

   Dist=outerradius-innerradius

   AngleStep#=5
   for b=0 to 2

      For lp =0 to 360 step AngleStep#
   
         ; get the angle for this pair of vertex / cooridnates
         Angle#=wrapangle(lp+SpinAngle#)

         ; calc the inner most coorindates         
         InnerXpos#=XPos+cos(Angle#)*InnerRadius
         InnerYpos#=YPos+sin(Angle#)*InnerRadius
         ; calc the Outer most coorindates         
         OuterXpos#=XPos+cos(Angle#)*OuterRadius
         OuterYpos#=YPos+sin(Angle#)*OuterRadius

         if lp>0     
            GouraudQuad OuterXpos#,OuterYpos#,OuterColour,InnerXpos#,InnerYpos#,InnerColour,InnerXpos2#,InnerYpos2#,InnerColour,OuterXpos2#,OuterYpos2#,OuterColour
         endif
         ; remember this position for the next loop.
         InnerXpos2#=InnerXpos#
         InnerYpos2#=InnerYpos#
         OuterXpos2#=OuterXpos#
         OuterYpos2#=OuterYpos#

      next lp

      swap InnerColour,OuterColour
      InnerRadius+=Dist
      OuterRadius+=Dist

   next b
   unlockbuffer


EndFunction





[/pbcode]
Title: Re: Polygon Ring
Post by: ATLUS on November 28, 2012, 11:07:23 AM
Very nice codes =)