POV-Ray : Newsgroups : povray.newusers : problem using rotate in a while loop Server Time
4 Nov 2024 13:03:41 EST (-0500)
  problem using rotate in a while loop (Message 1 to 7 of 7)  
From: Suso Banderas
Subject: problem using rotate in a while loop
Date: 10 Feb 2003 17:12:09
Message: <pan.2003.02.10.22.12.09.759527@suso.org>
I'm trying to make a scene where several boxes form a circle and
are tangent to the circle.  in other words, each box would be
rotated a certain amount as they are placed around the circle.
I tried doing this with a while loop and a couple of variables. 
I can get the translation to work, but the rotate doesn't work.
What could I be doing wrong?  By the way, I already tried making
the box a declared object that is defined outside the while loop.
Thanks for your help.

#include "colors.inc"

light_source { <-10, 20, 0> color White }
light_source { <0, 50, 0> color White }


plane {
    y, -1
    pigment { checker color Black color White scale 5 rotate y*45 }
}

camera {
    location <0, 20, 0>
    look_at <0, 0, 0>
}

#declare rad = 10;     // radius of circle
#declare objects = 9;  // number of objects in circle
#declare position = 0;  // starting position
#declare increment = 2*pi / objects;  // increment between objects
#declare degree = 180/pi;  // Radian to degree conversion

#declare thisobject = box {
    <-1,-1,-1>
    <1,1,1>
    pigment { color Green }
    rotate <0,increment*degree,0>
}

#while (position < 2*pi)
    box {
        <-1,-1,-1>
        <1,1,1>
        pigment { color Green }
        rotate <0,increment*degree,0>  // rotate the box so that it is tangent.
        translate <cos(position)*rad, 0, sin(position)*rad>
    }
    #declare position = position + increment;
#end


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: problem using rotate in a while loop
Date: 10 Feb 2003 17:30:11
Message: <web.3e4827d1367de2c2b417814a0@news.povray.org>
Suso Banderas wrote:
>I'm trying to make a scene where several boxes form a circle and
>are tangent to the circle.  in other words, each box would be
>rotated a certain amount as they are placed around the circle.
>I tried doing this with a while loop and a couple of variables.
>I can get the translation to work, but the rotate doesn't work.
>What could I be doing wrong?  By the way, I already tried making
>the box a declared object that is defined outside the while loop.
>Thanks for your help.
>
>#include "colors.inc"
>
>light_source { <-10, 20, 0> color White }
>light_source { <0, 50, 0> color White }
>
>
>plane {
>    y, -1
>    pigment { checker color Black color White scale 5 rotate y*45 }
>}
>
>camera {
>    location <0, 20, 0>
>    look_at <0, 0, 0>
>}
>
>#declare rad = 10;     // radius of circle
>#declare objects = 9;  // number of objects in circle
>#declare position = 0;  // starting position
>#declare increment = 2*pi / objects;  // increment between objects
>#declare degree = 180/pi;  // Radian to degree conversion
>
>#declare thisobject = box {
>    <-1,-1,-1>
>    <1,1,1>
>    pigment { color Green }
>    rotate <0,increment*degree,0>
>}
>
>#while (position < 2*pi)
>    box {
>        <-1,-1,-1>
>        <1,1,1>
>        pigment { color Green }
>        rotate <0,increment*degree,0>  // rotate the box so that it is tangent.
>        translate <cos(position)*rad, 0, sin(position)*rad>
>    }
>    #declare position = position + increment;
>#end
>

You can just change this line:
rotate <0,increment*degree,0>

- to this:
rotate <0,-position*degree,0>


Tor Olav


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: problem using rotate in a while loop
Date: 10 Feb 2003 17:50:03
Message: <web.3e482bfc367de2c2b417814a0@news.povray.org>
Tor Olav Kristensen wrote:
>Suso Banderas wrote:
>>I'm trying to make a scene where several boxes form a circle and
>>are tangent to the circle.  in other words, each box would be
>>rotated a certain amount as they are placed around the circle.
>>I tried doing this with a while loop and a couple of variables.
>>I can get the translation to work, but the rotate doesn't work.
>>What could I be doing wrong?  By the way, I already tried making
>>the box a declared object that is defined outside the while loop.
>>Thanks for your help.
>>
>>#include "colors.inc"
>>
>>light_source { <-10, 20, 0> color White }
>>light_source { <0, 50, 0> color White }
>>
>>
>>plane {
>>    y, -1
>>    pigment { checker color Black color White scale 5 rotate y*45 }
>>}
>>
>>camera {
>>    location <0, 20, 0>
>>    look_at <0, 0, 0>
>>}
>>
>>#declare rad = 10;     // radius of circle
>>#declare objects = 9;  // number of objects in circle
>>#declare position = 0;  // starting position
>>#declare increment = 2*pi / objects;  // increment between objects
>>#declare degree = 180/pi;  // Radian to degree conversion
>>
>>#declare thisobject = box {
>>    <-1,-1,-1>
>>    <1,1,1>
>>    pigment { color Green }
>>    rotate <0,increment*degree,0>
>>}
>>
>>#while (position < 2*pi)
>>    box {
>>        <-1,-1,-1>
>>        <1,1,1>
>>        pigment { color Green }
>>        rotate <0,increment*degree,0>  // rotate the box so that it is tangent.
>>        translate <cos(position)*rad, 0, sin(position)*rad>
>>    }
>>    #declare position = position + increment;
>>#end
>>
>
>You can just change this line:
>rotate <0,increment*degree,0>
>
>- to this:
>rotate <0,-position*degree,0>


But I would recommend writing the code like this:


#include "colors.inc"

light_source { <-10, 20, 0> color White }
light_source { <0, 50, 0> color White }

plane {
  y, -1
  pigment { checker color Black color White scale 5 rotate y*45 }
}

camera {
  location <0, 20, 0>
  look_at <0, 0, 0>
}

#declare rad = 10;     // radius of circle
#declare objects = 9;  // number of objects in circle
#declare increment = 2*pi/objects;  // increment between objects

#declare thisobject =
  box {
    -<1, 1, 1>, <1, 1, 1>
    pigment { color Green }
  }

#declare thisobject = object { thisobject translate rad*x }

#declare position = 0;  // starting position
#while (position < 2*pi)
  object { thisobject rotate degrees(position)*y }
  #declare position = position + increment;
#end


Or even like this:


#version 3.5;
#include "colors.inc"

light_source { <-10, 20, 0> color White }
light_source { <0, 50, 0> color White }

plane {
  y, -1
  pigment { checker color Black color White scale 5 }
  rotate y*45
}

camera {
  location 20*y
  look_at 0*y
}

#declare Radius = 10;
#declare NrOfObjects = 9;
#declare dAngle = 360/NrOfObjects;

#declare BoxObject =
  box {
    -<1, 1, 1>, <1, 1, 1>
    pigment { color Green }
  }

#declare BoxObject = object { BoxObject translate Radius*x }

#declare Cnt = 0;
#while (Cnt < NrOfObjects)
  object { BoxObject rotate Cnt*dAngle*y }
  #declare Cnt = Cnt + 1;
#end // while


Tor Olav


Post a reply to this message

From: Frank 'Sputnik' Rothfuß
Subject: Re: problem using rotate in a while loop
Date: 10 Feb 2003 19:12:20
Message: <3E483FE5.3AFC8F53@computermuseum.fh-kiel.de>
Hi Sosu,

your main error is rotating by 'increment' which doesn't change in the
loop. Use '-position' instead. (POV-angles are counted in the opposite
direction as math-angles -- that's the reason for the '-'.)

The declared 'thisobject' is not used, so it will not appear. To put
it in the scene, use 

   object { thisobject /*texture, rotate etc. may come here*/ }

POV-Ray has functions to convert from degrees to radians and reverse:
   degrees ( Angle_in_radians )
   radians ( Angle_in_degrees )

Names of variables should have at least one uppercase letter in them
to avoid confusion with (current or future) keywords of POV-Ray. You
have a near miss with 'degree' which is almost the keyword 'degrees'!

Instead of
   rotate <0, -degrees(Position), 0>
   translate <cos(Position)*Rad, 0, sin(Position)*Rad>
let POV-Ray do the trigonometry:
   translate Rad*x // put onto the circle
   rotate -degrees(Position)*y // swing around the origin to destination
You can even omit the conversion to degrees, if you express the
Position in degrees instead of radians.

Due to floating point inaccuracies, your loop might not reach 2*pi
after nine additions of 'Increment' and create an additional box
rotated by slightly less than 2*pi. Integers will be treated
correctly (when all intermediate results also are integers!), so
it is better to use an integer number in loops and calculate the
non-integer Position from it -- or increment the position, but count
with integers.

Putting all this together, leaving out the superfluous 'thisobject'
and fiddling around here and there gives this:


// BEGIN OF POV-SCENE //////////////////////////////////////////////////

#include "colors.inc"

light_source { <-10, 20, 0> color White }
light_source { <0, 50, 0> color White }


plane {
    y, -1
    pigment { checker color Black color White scale 5 rotate y*45 }
}

camera {
    location 20*y
    look_at 0
}

#declare Rad     = 10;  // radius of circle
#declare Objects =  9;  // number of objects in circle
#declare Increment = 360/Objects;  // degrees increment between objects


#declare Number  =  0;  // starting number
#while (Number < Objects)
    box { -1, 1
        pigment { color Green }
        translate Rad*x
        rotate -Number*Increment*y // let POV do the sin and cos
    }
    #declare Number = Number + 1;
#end//while Number

// END OF POV-SCENE ////////////////////////////////////////////////////


   Sputnik

-- 

-------------------------------------

e-mail: fr### [at] computermuseumfh-kielde
-------------------------------------


Post a reply to this message

From: Suso Banderas
Subject: Re: problem using rotate in a while loop
Date: 10 Feb 2003 22:53:42
Message: <3e4873c6$1@news.povray.org>
Oh I get it now.  POV-Ray really functions reverse of what I expected.
Being a programmer, I was expecting it to keep track of what the box's
previous rotational angle was on the last iteration of the while loop.  I
should have realized that I wasn't doing that with the position.  Thanks
for your help.  Both of you.

On Mon, 10 Feb 2003 17:29:37 -0500, Tor Olav Kristensen wrote:

>>
>>
> You can just change this line:
> rotate <0,increment*degree,0>
> 
> - to this:
> rotate <0,-position*degree,0>
> 
> 
> Tor Olav


Post a reply to this message

From: Johannes Dahlstrom
Subject: Re: problem using rotate in a while loop
Date: 11 Feb 2003 05:26:35
Message: <3e48cfda@news.povray.org>
Suso Banderas wrote:

> Oh I get it now.  POV-Ray really functions reverse of what I expected.
> Being a programmer, I was expecting it to keep track of what the box's
> previous rotational angle was on the last iteration of the while loop.  I
> should have realized that I wasn't doing that with the position.  Thanks
> for your help.  Both of you.

Well, at each iteration it creates a new box from scratch, so... 
BTW, no need to resort to trigonometry here, just do the translation before 
rotation:

#declare Rad = 3;
#declare Rot = 0;
#while(Rot < 360)
  box {
   -1, 1
   pigment { rgb y }
   translate Rad*z;  
   rotate Rot*y
  }
  #declare Rot = Rot + 30;
#end


Post a reply to this message

From: Warp
Subject: Re: problem using rotate in a while loop
Date: 11 Feb 2003 05:52:06
Message: <3e48d5d6@news.povray.org>
Suso Banderas <sus### [at] susoorg> wrote:
> Oh I get it now.  POV-Ray really functions reverse of what I expected.
> Being a programmer, I was expecting it to keep track of what the box's
> previous rotational angle was on the last iteration of the while loop.

  It works pretty much like a programming language would.

  When you make a "#declare Object = box { ... }", that's a bit like
saying "struct Object { ... }" or "class Object { ... }" in C/C++.
When you make an instantiation of the box with "object { Object }", that's
like making an instantiation of the struct/class in C/C++, like
"Object obj;". If you now modify *this* instantiation it naturally doesn't
modify any other instantiation you may have of the same class.

-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.