Struct Ray

Source
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

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.

§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()));
Source

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

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 Freeze for Ray

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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 G1
where G2: Contains<G1>,

Source§

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