|
|
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
|
|