File: dist_to_line.pl

package info (click to toggle)
libmath-vector-real-perl 0.18-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: perl: 826; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 577 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl

use strict;
use warnings;

# see http://perlmonks.org/?node_id=814899

# Given a line defined by two points $l0 and $l1 calculate the
# distance to another point $p:

use Math::Vector::Real;

my $l0 = V(2, 3, 4);
my $l1 = V(1, 0, 1);

my $p = V(2, 2, 2);

# calculate the vector $n perpendicular to the line that goes to $p:

my $u = $l1 - $l0; # line direction

my $n = $p - $l0;
$n -= ($u * $n)/($u * $u) * $u;

# the distance is the length of the vector:

printf "The distance between the point %s and the line [%s - %s] is %g\n",
    $p, $l0, $l1, abs($n)