Struct geo_buffer::util::Ray

source ·
pub struct Ray { /* private fields */ }
Expand description

This structure conceptullay 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 every ε > 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

source

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 which src 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);
 
source

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()));
 
source

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()));
source

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 on self,
  • 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()));
source

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()));
source

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()));
 
source

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));
source

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()));
source

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.

Trait Implementations§

source§

impl Clone for Ray

source§

fn clone(&self) -> Ray

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Ray

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Ray

source§

fn default() -> Ray

Returns the “default value” for a type. Read more
source§

impl Display for Ray

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Copy for Ray

Auto Trait Implementations§

§

impl RefUnwindSafe for Ray

§

impl Send for Ray

§

impl Sync for Ray

§

impl Unpin for Ray

§

impl UnwindSafe for Ray

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<G1, G2> Within<G2> for G1where G2: Contains<G1>,

source§

fn is_within(&self, b: &G2) -> bool