pub struct Ray { /* private fields */ }
Expand description
This structure conceptually represents a half-line (which also known as “Ray”).
A ray has a “start vertex” r0, that is, r0 is a part of the ray itself, but we cannot make a disk of radius ε which contained in the ray around r0 for arbitrary ε > 0.
If we consider the vectors from r0 to each point on the ray, then they are all pairwise parallel. Therefore, there exists a “direction vector” v and we can represent each point on ray by the parameterized equation:
r0 + tv (t ≥ 0),
where t is the parameter which is greater than or equal to zero.
We can also think of a ray as the locus of a moving point at a constant velocity from the starting point r0 as time passes. In this case, the location of the point after time t (t ≥ 0) is equal to r0 + tv.
Implementations§
Source§impl Ray
impl Ray
Sourcepub fn new(src: Coordinate, dst: Coordinate) -> Self
pub fn new(src: Coordinate, dst: Coordinate) -> Self
Creates and returns a Ray w.r.t. the given arguments.
§Arguments
src
: The starting point of the ray.dst
: The point for whichsrc
is heading.
§Return
A Ray
that starts from src
towards dst
with arrival time 1.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (1., 2.).into();
let c2 = (2., 3.).into();
let r1 = Ray::new(c1, c2);
Sourcepub fn point(&self) -> Coordinate
pub fn point(&self) -> Coordinate
Returns the “starting point” of the given ray.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (1., 2.).into();
let c2 = (2., 3.).into();
let r1 = Ray::new(c1, c2);
assert!(c1.eq(&r1.point()));
Sourcepub fn point_by_ratio(&self, ratio: f64) -> Coordinate
pub fn point_by_ratio(&self, ratio: f64) -> Coordinate
Returns the value of parameterized equation r0 + tv by the given ratio t.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (1., 2.).into();
let c2 = (2., 3.).into();
let r1 = Ray::new(c1, c2);
assert!(r1.point_by_ratio(2.).eq(&(3., 4.).into()));
Sourcepub fn is_contain(&self, rhs: &Coordinate) -> bool
pub fn is_contain(&self, rhs: &Coordinate) -> bool
Checks whether self
contains the given Cartesian coordinate.
Note that this function considers self
as a open-ended line.
That is, if the given point lies on the extended line of self
, this function returns true
.
§Return
true
if the given point lies onself
,false
otherwise.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (1., 2.).into();
let c2 = (2., 3.).into();
let r1 = Ray::new(c1, c2);
assert!(r1.is_contain(&(3., 4.).into()));
Sourcepub fn is_intersect(&self, rhs: &Ray) -> bool
pub fn is_intersect(&self, rhs: &Ray) -> bool
Checks whether the given two rays are intersecting with each other. More precisely, it checks whether they have one or more common points.
§Return
true
if the given rays have one or more common points,false
otherwise.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (1., 2.).into();
let c2 = (2., 3.).into();
let r1 = Ray::new(c1, c2);
assert!(r1.is_contain(&(3., 4.).into()));
Sourcepub fn intersect(&self, rhs: &Ray) -> Coordinate
pub fn intersect(&self, rhs: &Ray) -> Coordinate
Returns a common point of the given rays. If they have more than 2 common points, then returns a middle point of the starting points of the given rays.
Note that this function considers the rays as a open-ended line. That is, if the common point lies on the extended line(s) of them, this function returns the point.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (0., 0.).into();
let c2 = (1., 1.).into();
let c3 = (4., 0.).into();
let c4 = (0., 4.).into();
let r1 = Ray::new(c1, c2);
let r2 = Ray::new(c3, c4);
assert!(r1.intersect(&r2).eq(&(2., 2.).into()));
Sourcepub fn is_parallel(&self, rhs: &Ray) -> bool
pub fn is_parallel(&self, rhs: &Ray) -> bool
Checks whether the given two rays are parallel. If they have more than 2 common points, they are not considered as parallel.
§Return
true
if the given rays are parallel,false
otherwise.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (0., 0.).into();
let c2 = (1., 1.).into();
let c3 = (0., 1.).into();
let c4 = (1., 2.).into();
let r1 = Ray::new(c1, c2);
let r2 = Ray::new(c3, c4);
assert!(r1.is_parallel(&r2));
Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Normalizes the given Ray
. The magnitude of the ‘velocity’ becomes 1. Does nothing if it is 0.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (0., 0.).into();
let c2 = (3., 4.).into();
let mut r1 = Ray::new(c1, c2);
r1.normalize();
assert!(r1.point_by_ratio(1.).eq(&(0.6, 0.8).into()));
Sourcepub fn reverse(&self) -> Self
pub fn reverse(&self) -> Self
Returns the reversed ray of the given ray. The returned ray has the same starting point and the opposite direction to the given ray.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (0., 0.).into();
let c2 = (3., 4.).into();
let r1 = Ray::new(c1, c2);
let r2 = r1.reverse();
assert!(r2.point_by_ratio(1.).eq(&(-3., -4.).into()));
Sourcepub fn rotate_by(&self, angle: f64) -> Self
pub fn rotate_by(&self, angle: f64) -> Self
Returns a ray which performs a rotation of the given vector through the given angle (in radian). The direction of rotation is counter-clockwise direction and the center of rotation is the starting point of the given vector.
§Example
use geo_buffer::{Coordinate, Ray};
let c1 = (0., 0.).into();
let c2 = (3., 4.).into();
let r1 = Ray::new(c1, c2);
let r2 = r1.rotate_by(std::f64::consts::PI/2.);
assert!(r2.point_by_ratio(1.).eq(&(-4., 3.).into()));