On 10/23/07, camotito <[EMAIL PROTECTED]> wrote:

> I was just implementing this small program for calculating the line-
> equation, given two points.
> Just in case you don't remind this equation.... y = slope * x + b.
> First I calculate the slope and then b:
>
> $slope = ($ARGV[1] - $ARGV[3]) / ($ARGV[0] - $ARGV[2]);
> $b = $ARGV[1] - ($ARGV[0] * $slope);
> print "\n$ARGV[1] = $slope * $ARGV[0] + $b\n";

It's easier to read, write, and maintain code that uses names instead
of indices. So I'd re-write your code so far like this:

  use strict;
  use warnings;
  # @ARGV holds two points (two xy pairs)
  my($x1, $y1, $x2, $y2) = @ARGV;
  my $slope = ($y1 - $y2) / ($x1 - $x2);
  my $b = $y1 - ($x1 * $slope);
  print "\n";
  print "$y1 = $slope * $x1 + $b\n";

> For this input :
>
>  perl my_program 16.81 16.57 0 0
>
> It gives me this result :
>
>  16.57 = 0.985722784057109 * 16.81 + 0
>
> Clearly b can be 0 only if the slope is 1.

One must pay special attention to those points a mathematician
describes as "clear". :-)   You must have meant to say, clearly, b can
be 0 only if the line passes through the origin. But check that sort
of detail with your algebra teacher or in a mathematics forum; perl is
doing what you're asking of it.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to