Math(3pm) | User Contributed Perl Documentation | Math(3pm) |
XRacer::Math - Library of miscellaneous 3D math functions used by XRacer tools.
use XRacer::Math; Basic mathematics: $midpoint = midpoint (@vertices); $normalize_vector = normalize ($vector); $magnitude = magnitude ($vector); $magnitude = magnitude_in_direction ($v1, $v2); $angle = angle_between ($v1, $v2); $vector = multiply_scalar_vector ($scalar, $vector); $matrix = matrix_multiply ($a, $b); $vector = matrix_vector_multiply ($m, $v); $sum = sum_vectors (@vectors); $vector = subtract_vectors ($vector1, $vector2); ($min_x, $max_x, $min_y, $max_y, $min_z, $max_z) = bbox (@vertices); Dot products and cross products: $product = dot_product ($vector1, $vector2); $vector = cross_product ($vector1, $vector2); Distances: $distance = distance ($point1, $point2); $distance = distance_point_to_line ($line, $point); $distance = distance_point_to_plane ($plane, $point); $distance = minimum_distance_between_two_lines ($line1, $line2); Planes: $plane = plane_coefficients ($point1, $point2, $point3); $boolean_result = is_coplanar ($point1, $point2, $point3, $point4); $normal = normal ($plane); $unit_normal = unit_normal ($plane); $line = intersection_of_two_planes ($plane1, $plane2); $point = intersection_line_and_plane ($plane, $line); ($inside_face, $outside_face) = split_convex_face_in_place ($face, $plane); Line segments: $lineseg = line_segment ($point1, $point2); Cylinders: $boolean_result = line_intersects_cylinder ($line, $cylinder);
This library contains miscellaneous functions which are used by the XRacer track building tools. The functions perform various 3D mathematical operations on points (vertices), vectors, lines and planes.
Points or vertices are represented by a reference to an array containing three elements. That is to say, to initialize a point, do this:
$point = [ $x, $y, $z ];
Vectors are represented by a reference to an array containing three elements:
$vector = [ $dx, $dy, $dz ];
Lines are represented by the formula:
p + t.v
where p is a point on the line and v is a vector parallel to the line. Therefore a line is simply a reference to a hash containing the point and vector:
$line = {'p' => $point, 'v' => $vector};
A line segment is represented by the formula:
p + t.v where a <= t <= b
implying the following representation:
$lineseg = { 'p' => $point, 'v' => $vector, 'a' => $a, 'b' => $b };
Notice that you can use a line segment wherever you would use a line.
Cylinders are represented by the hash reference:
$cylinder = { 'radius' => $radius, 'p' => $point, 'v' => $vector, 'a' => $a, 'b' => $b };
where $radius is the radius of the cylinder, and the other fields are a line segment running from the midpoint of one end of the cylinder to the midpoint of the other end of the cylinder. In other words, a cylinder is simply a line segment with added radius field.
Planes are represented by the formula:
ax + by + cz + d = 0
where a, b, c and d are coefficients which uniquely define the plane. A plane is a reference to an array containing these four constants:
$plane = [ $a, $b, $c, $d ];
Use the "plane_coefficients" function to compute the four coefficients of a plane, given any three points (not colinear) on the plane.
The following page was enormously useful in working out these formulae: "http://www.magic-software.com/ComputerGraphics.html"
$face = [ $vertex0, $vertex1, ... ];
we split the face into two faces. The first face returned is inside the plane. The second face returned is outside the plane.
Richard W.M. Jones, <rich@annexia.org>
XRacer is copyright (C) 1999-2000 Richard W.M. Jones (rich@annexia.org) and other contributors listed in the AUTHORS file.
2020-12-29 | perl v5.32.0 |