POV-Ray : Newsgroups : povray.newusers : Problem with macros and translate function :/ Server Time10 Aug 2024 20:01:56 EDT (-0400)
 Problem with macros and translate function :/ (Message 1 to 6 of 6)
 From: aysays Subject: Problem with macros and translate function :/ Date: 23 Aug 2023 08:00:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.64e5f432d9dedc30941194678c4c5f32%40news.povray.org",
"headline": "Problem with macros and translate function :\/",
"dateCreated": "2023-08-23T12:00:00+00:00",
"datePublished": "2023-08-23T12:00:00+00:00",
"author": {
"@type": "Person",
"name": "aysays"
}
}
Hello I'm new to Pov-Ray and am trying to write a broccoli fractal code however
I'm having problems with the translate function. If I understood it correctly,
translate only accepts numerical values. I tried writing a macro but that didn't
work either. How am I supposed to write a recursive function that utilizes
translate? :/ Here is my code:

#include "colors.inc"

global_settings {
ambient_light rgb <1, 1, 1>
assumed_gamma 1.0
}

camera {
location <0, 0, -25>
look_at <0, 0, 0>
}

light_source {
<-100, -100, -100>
color rgb <1, 1, 1>
}

#declare phi = (1 + sqrt(5))/2;
#declare ang = 360 * (1 - 1/phi);

#macro trans()
translate< sqrt(i)* cos(i * ang),
sqrt(i) * sin(i * ang),
10-sqrt(pow(sqrt(i)* cos(i * ang),2) + pow(sqrt(i) * sin(i *
ang),2))>
#end

#macro frac(n)
#if(n > 0)
cone {
<0, 10, 0>, 10
<0, 0, 0>, 0
texture {
pigment { Green }
finish { ambient 0.2 }
}

#declare i = 1;
#while (i <= 100)
//#declare theta = i * ang;
//#declare r = sqrt(i);
//#declare a = r * cos(theta);
//#declare b = r * sin(theta);

trans()

scale 0.15
}
frac(n - 1)

#declare i = i + 1;
#end
#end
#end

frac(3);

```
 From: jr Subject: Re: Problem with macros and translate function :/ Date: 23 Aug 2023 09:10:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.64e604c2d6a8965b80c03e9d6cde94f1%40news.povray.org",
"headline": "Re: Problem with macros and translate function :\/",
"dateCreated": "2023-08-23T13:10:00+00:00",
"datePublished": "2023-08-23T13:10:00+00:00",
"author": {
"@type": "Person",
"name": "jr"
}
}
hi,

"aysays" <nomail@nomail> wrote:
> Hello I'm new to Pov-Ray and am trying to write a broccoli fractal code however

great.  enjoy!

> I'm having problems with the translate function. If I understood it correctly,
> translate only accepts numerical values. I tried writing a macro but that didn't
> work either. How am I supposed to write a recursive function that utilizes
> translate? :/ Here is my code:

have not the time to delve deeply, right now, just a couple of things (as you're
"new").

scene files ought to have a '#version' directive as (one of) their first lines.
re variable names, I too am all for lowercase but you will have to be very
careful, using capitalised or pre/post-fixed names is recommended and "safe".
use '#local' inside macros, rather than '#declare's.

re the recursive macro.  I'd be uncomfortable trying to create, um, a cone of
cones, inline, suggest a macro which expands to one cone, and call/place that
from within the loop  in 'frac()'.  hth.

regards, jr.
```
 From: kurtz le pirate Subject: Re: Problem with macros and translate function :/ Date: 23 Aug 2023 11:22:47 Message: <64e62447@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#64e62447%40news.povray.org",
"headline": "Re: Problem with macros and translate function :\/",
"dateCreated": "2023-08-23T15:22:47+00:00",
"datePublished": "2023-08-23T15:22:47+00:00",
"author": {
"@type": "Person",
"name": "kurtz le pirate"
}
}
On 23/08/2023 13:57, aysays wrote:
> Hello I'm new to Pov-Ray and am trying to write a broccoli fractal code however
> I'm having problems with the translate function. If I understood it correctly,
> translate only accepts numerical values. I tried writing a macro but that didn't
> work either. How am I supposed to write a recursive function that utilizes
> translate? :/ Here is my code:
>
> ..
>
>
>

Quickly: the trans() macro doesn't know the values of 'i' and 'ang'.
Either declare these variables globally, or pass them as parameters.

--
Kurtz le pirate
Compagnie de la Banquise
```
 From: jr Subject: Re: Problem with macros and translate function :/ Date: 23 Aug 2023 12:55:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.64e6394bd6a8965b80c03e9d6cde94f1%40news.povray.org",
"headline": "Re: Problem with macros and translate function :\/",
"dateCreated": "2023-08-23T16:55:00+00:00",
"datePublished": "2023-08-23T16:55:00+00:00",
"author": {
"@type": "Person",
"name": "jr"
}
}
hi,

kurtz le pirate <kur### [at] gmailcom> wrote:
> ...
> Quickly:

should have stopped for coffee.  </grin>

> the trans() macro doesn't know the values of 'i' and 'ang'.
> Either declare these variables globally, or pass them as parameters.

'ang' is global, '#declare'd.  but (sorry) you're wrong re the 'i', see below.
without passing it as an argument cannot modify it though.

and to clarify:
> use '#local' inside macros, rather than '#declare's.

unless you do want to modify global variables.

regards, jr.

-----<snip>-----
#version 3.7;

global_settings {assumed_gamma 1}
box {0,1}

#declare foo_ = 12345;

#macro m_b()
#debug concat("foo ",str(foo_,0,0)," bar ",str(bar_,0,0),".\n")
#end

#macro m_a()
#local bar_ = 67890;
m_b()
#end

m_a()
```
 From: Bald Eagle Subject: Re: Problem with macros and translate function :/ Date: 23 Aug 2023 14:10:00 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"headline": "Re: Problem with macros and translate function :\/",
"dateCreated": "2023-08-23T18:10:00+00:00",
"datePublished": "2023-08-23T18:10:00+00:00",
"author": {
"@type": "Person",
"name": "Bald Eagle"
}
}
"aysays" <nomail@nomail> wrote:
> Hello I'm new to Pov-Ray and am trying to write a broccoli fractal code however
> I'm having problems with the translate function. If I understood it correctly,
> translate only accepts numerical values. I tried writing a macro but that didn't
> work either. How am I supposed to write a recursive function that utilizes
> translate?

The first thing I noticed when trying to format/indent your code (and
simultaneously make jr twitch by using TAB characters to denote whitespace) was
that you cone definition and while loop are .... disjoint.

Also, sin and cos expect angle expressed in radians.

I don't see much of a difference when changing the camera location, so something
strange is going on.  I also don't get much of a difference when I multiply your
x and y values in the translate function by as much as 10.   So you may not be
translating as much as you think you ought to be.
Scaling that cone after translating causes an(other), probably unintentional
translation, due to the offset from the origin.
Scaling by 0.15 100 times is going to make those other cones SMALL, probably to
the point of invisibility, especially since they are currently (?) hidden by the
first cone, since all the others don't get translated far enough away from the
first one. (speculating)

What you want to do is plan out a little more clearly what ought to be
happening.
Include some comments describing exactly what you want to happen at each stage,
and then comment each line to describe what each line actually does.

I'd probably make the macro determine the location and scale of the cone, and
instantiate it, an put the macro call(s) in the while loop.  Just to make things
clearer.  Maybe calculate the translate and scaling separately, and output those
values to the debug stream, so you can see what's _actually_ happening
(numerically)

I'd also calculate your n value, check to make sure it's non-zero, and THEN call
the macro recursively if that's the case.  seems clearer.

- BW

I think you're close, but the logic of the scene needs to be cleaned up a bit.
```
 From: kurtz le pirate Subject: Re: Problem with macros and translate function :/ Date: 24 Aug 2023 05:32:08 Message: <64e72398@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#64e72398%40news.povray.org",
"headline": "Re: Problem with macros and translate function :\/",
"dateCreated": "2023-08-24T09:32:08+00:00",
"datePublished": "2023-08-24T09:32:08+00:00",
"author": {
"@type": "Person",
"name": "kurtz le pirate"
}
}
On 23/08/2023 18:52, jr wrote:
> hi,
>
> kurtz le pirate <kur### [at] gmailcom> wrote:
>> ...
>> Quickly:
>
> should have stopped for coffee.  </grin>
>

You're a thousand times right.
I'm going back for another coffee.

Sorry :(

--
Kurtz le pirate
Compagnie de la Banquise
```