POV-Ray : Newsgroups : povray.general : Macro return value on failure : Re: Macro return value on failure Server Time
26 Apr 2024 03:38:46 EDT (-0400)
  Re: Macro return value on failure  
From: Mike Horvath
Date: 23 Jul 2018 18:49:40
Message: <5b565b84$1@news.povray.org>
On 7/23/2018 10:37 AM, JimT wrote:
>>>
>>> (As an unrelated side note, it is highly recommended to /not/ use
>>> all-lowercase variable names; such names might be used as new keywords
>>> in future versions without special notice.)
>>>
>>
>> Thanks!
>>
>> Now I am afraid because I use lower case for variable names almost
>> exclusively.
>>
>> :(
> 
> I too am afraid. Many of my local variables are all lower case, though my #macro
> names do start with e.g. AA_......
> 
> Something that doesn't seem to be well known (or maybe clipka has warned against
> it in the past) is if you #declare a variable before you pass it into a #macro
> and change it using #local in the #macro, when the #macro finishes it retains
> the changed value. Obviously (since clipka says so) you can pass back the 3rd
> component of a 3 vector, but then you would have to unpack the <x,y>
> intersection point from the <x,y,z> vector. I would just add a 3rd argument to
> line_intersection() and use that as the switch.
> 
> #version 3.7;
> #include "colors.inc"
> global_settings { assumed_gamma 1.0 }
> //
> #macro line(p1, p2)
>   #local A = (p1.y - p2.y);
>   #local B = (p2.x - p1.x);
>   #local C = (p1.x * p2.y - p2.x*p1.y);
>   <A,B,-C>
> #end
> 
> #macro line_intersection(l1, l2, unique_int_point)
>   #local D  = l1.x * l2.y - l1.y * l2.x;
>   #local Dx = l1.z * l2.y - l1.y * l2.z;
>   #local Dy = l1.x * l2.z - l1.z * l2.x;
>   #if (D != 0)
>    #local out_x = Dx / D;
>    #local out_y = Dy / D;
>    #local out_value = <out_x,out_y>;
>       #local unique_int_point = 1;
>   #else
>    #local out_value = <0,0>;
>    #local unique_int_point = 0;
>   #end
>   out_value
> #end
> 
> #declare p1 = <0,1>;
> #declare p2 = <2,3>;
> #declare p3 = <2,6>;
> //#declare p3 = <2,3>;
> #declare p4 = <0,4>;
> #declare L1 = line(p1, p2);
> #declare L2 = line(p3, p4);
> #declare unique_int_point = 1;
> //
> #declare R  = line_intersection(L1, L2, unique_int_point);
> //
> #if(unique_int_point = 1)
>    #debug concat("\n\n\n Intersection detected: <", vstr(2,R,",",0,-1),">\n\n\n")
>    sphere{R 0.1 pigment{Blue}}
> #else
>    #debug concat("\n\n\n No single intersection point detected.\n\n\n")
> #end
> //
> cylinder{p1 p2 0.05 pigment{Red}}
> cylinder{p3 p4 0.05 pigment{Green}}
> //
> camera{orthographic location <0,0,-100> look_at <0,-0.00001,0> angle 12}
> light_source{<0,0,-100> color rgb <1,1,1>  shadowless}
> background{White}
> 
> 
> 


Wow, I thought local variables only worked in the local scope. E.g. the 
globally declared variable in the code you supplied would retain its 
original value.


Mike


Post a reply to this message

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