POV-Ray : Newsgroups : povray.general : Roots of a function : Re: Roots of a function Server Time
25 Apr 2024 23:35:15 EDT (-0400)
  Re: Roots of a function  
From: William F Pokorny
Date: 29 Sep 2019 08:48:53
Message: <5d90a835$1@news.povray.org>
On 9/29/19 3:07 AM, IGM wrote:
> This is my poor-man solution:
> 
> // root(s) of function F = Y(X)-thr
> // Example:
> // #declare X = array [10] {0,10,20,30,40,50,60,70,80,90};
> // #declare C = array [10]
> {0.000,21.000,44.000,69.000,97.000,124.000,158.000,181.000,206.000,232.000};
> // root(X,C,35)
> // #debug concat("root: ", str(macro_value,5,3),"\n")
> #macro root(X,Y,thr)
> 
>      #include "math.inc"
>      GetStats(Y);
> 
>      #local f_prism = prism {
>          linear_spline
>          -1, 1, dimension_size(Y,1)+3,
> 
>          #for (k,0,dimension_size(Y,1)-1)
>              <X[k],Y[k]>,
>          #end
>          <X[dimension_size(Y,1)-1],StatisticsArray[2]-1>, // add lower point 1
>          <X[0],StatisticsArray[2]-1>, // add lower point 2
>          <X[0],Y[0]> // close prism
>          // rotate x*-90 // For plot in x,z plane. In this case P0 = <0,thr,0>
>          }
> 
>      #object {f_prism pigment {rgb <0,2,0>} } // no_image?
> 
>      #local P0 = <0,0,thr>; // Starting point
>      #local V0 = <1,0,0>; // Incident vector
>      // Trace to object
>      #local N = <99,99,99>; // init
>      #local P1 = trace(f_prism, P0, V0, N); // Loop here to find multiple roots
>      // Plot traced ray:
>      // #cylinder {P0, P1, 1 pigment {rgb <0,1,0>} no_shadow no_reflection}
> 
>      // For multiple roots create array!
>      #declare macro_value = P1.x;
> #end
> 
> 

Looks workable, though a few things caught my eye.

1)
If thr is 0 the Starting point will be on the prism edge and I'm not 
sure what the trace result might be in that case though expect one would 
want 0.0 as the returned root.

2)
IIRC you have to look at the normal returned by the trace command to 
really know whether it found a valid intersection. It's set on valid 
intersections only I believe.

3) And a lesson for me!
Saw #object (which you shouldn't need for the trace) and #cylinder; 
Thought, "Does that really work?" It does! You can also stick '#' on the 
end on some lines or code #declare CylinderZ = #cylinder {} and nary a 
peep or problem from the parser - new or old versions. The usual and 
recommended syntax is object {} and cylinder {}.

A declare without the leading # works too but we get:

File 'tmp.pov' line 40: Parse Warning: Should have '#' before 'declare'.
File 'tmp.pov' line 40: Possible Parse Error: 'declare' should be changed to
  '#declare'. Future versions may not support 'declare' and may require
  '#declare'.

Given this warning, I'd think we should be getting one on #object {} and 
the like too as the other side of the syntax change push - now that I 
understand there is another side.

Suppose this behavior related to Christoph's recommendation to me back 
in February to use default {} rather than #default {} though it's the 
latter which is still in the documentation. The idea, as I understand 
it, is to get to where the #<token> is used only with SDL language 
control elements.

Twenty years playing with POV-Ray and still learning.

I'm also not the brightest light bulb. I code in bash and tcl which use 
'#' style comments. I've long been aware

# camera { Camera00 }

doesn't comment the camera and isolated #s are ignored because sometimes 
I comment with '#' by muscle memory. I'd personally like a parse error 
for floating '#'s. It would save me time and I can see no reason to have 
them though '# declare Widget12 = ...' works fine today (it's treated as 
#declare).

Bill P.


Post a reply to this message

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