POV-Ray : Newsgroups : povray.windows : Surface Coefficients??? : Re: Surface Coefficients??? Server Time
28 Jul 2024 18:21:49 EDT (-0400)
  Re: Surface Coefficients???  
From: Ronald L  Parker
Date: 11 Feb 1998 16:07:27
Message: <34e20bd7.27585015@10.0.2.33>
On Wed, 11 Feb 1998 11:38:52 -0800, "Ken Cecka" <cec### [at] televarcom>
wrote:

>I can't claim authorship of this, but here's a perl script which will take a
>fairly standard infix notation equation (as opposed to postfix/referse
>polish or prefix), and spit out the pov code for you.  Someone posted it on
>CGRR way back when and I've kept a copy handy.
>
>An example session is as follows:
>
>bash-2.01$ echo "x^2*y^2*z^2+x^2*z^2+x*y*z=0"|poveq

Note that "x^2-y^2=5" has to be represented as "x^2+-1*y^2+-5".  The
constant term after the '=' in your example is ignored.  

Here's a version that works with the more intuitive  "x^2-y^2=5".  It
also fixes a syntax error in the original code, plus an error that
would occur if the input didn't end with a newline.

=====8<---- cut here ---------
#!/usr/bin/perl

while(<>) {
    chomp;
    $eq .= $_;
}

$coef=1;

$equation{0,0,0}=0-$1 if ( $eq =~ s/=(.*)$// );

foreach $comp (split('\s*([+-])\s*',$eq)) {
    $comp =~ s/\s+//;
    $x=0; $y=0; $z=0; 
    $coef = -1, next if ( $comp eq '-' ); 
    $coef = 1, next if ( $comp eq '+' );
    foreach $factor (split('\*',$comp)) {
        if ($factor =~ /^[xyz]/) {
            ($var,$exp) = ($factor =~ /^([xyz])\^?(\d+)?/);
            $exp = 1 if ($exp =~ /^$/);
            eval "\$$var = $exp";
        } else {
            $coef *= $factor;
        }
    }
    $equation{$x,$y,$z} = $coef;
    $power = $x+$y+$z if ($x+$y+$z > $power);
}

print "poly {\n    $power, <";

$first = 1;
for($x=$power;$x>=0;$x--) {
    for($y=$power-$x;$y>=0;$y--) {
        for($z=$power-$x-$y;$z>=0;$z--) {
            #print "\n$x,$y,$z\n";
            print ", " if (!$first);
            $first = 0;
            if ($equation{$x,$y,$z}) {
                print "$equation{$x,$y,$z}";
            } else {
                print "0";
            }
        }
    }
}
print ">\n}\n";
=====8<---- cut here ---------


Post a reply to this message

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