当前位置: 首页 > news >正文

3.1 角度

一、源码

use crate::approxeq::ApproxEq;
use crate::trig::Trig;use core::cmp::{Eq, PartialEq};
use core::hash::Hash;
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign};#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
use num_traits::real::Real;
use num_traits::{Float, FloatConst, NumCast, One, Zero};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};/// An angle in radians
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle<T> {pub radians: T,
}#[cfg(feature = "bytemuck")]
unsafe impl<T: Zeroable> Zeroable for Angle<T> {}#[cfg(feature = "bytemuck")]
unsafe impl<T: Pod> Pod for Angle<T> {}#[cfg(feature = "arbitrary")]
impl<'a, T> arbitrary::Arbitrary<'a> for Angle<T>
whereT: arbitrary::Arbitrary<'a>,
{// This implementation could be derived, but the derive would require an `extern crate std`.fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {Ok(Angle {radians: arbitrary::Arbitrary::arbitrary(u)?,})}fn size_hint(depth: usize) -> (usize, Option<usize>) {<T as arbitrary::Arbitrary>::size_hint(depth)}
}impl<T> Angle<T> {#[inline]pub fn radians(radians: T) -> Self {Angle { radians }}#[inline]pub fn get(self) -> T {self.radians}
}impl<T> Angle<T>
whereT: Trig,
{#[inline]pub fn degrees(deg: T) -> Self {Angle {radians: T::degrees_to_radians(deg),}}#[inline]pub fn to_degrees(self) -> T {T::radians_to_degrees(self.radians)}
}impl<T> Angle<T>
whereT: Rem<Output = T> + Sub<Output = T> + Add<Output = T> + Zero + FloatConst + PartialOrd + Copy,
{/// Returns this angle in the [0..2*PI[ range.pub fn positive(&self) -> Self {let two_pi = T::PI() + T::PI();let mut a = self.radians % two_pi;if a < T::zero() {a = a + two_pi;}Angle::radians(a)}/// Returns this angle in the ]-PI..PI] range.pub fn signed(&self) -> Self {Angle::pi() - (Angle::pi() - *self).positive()}
}impl<T> Angle<T>
whereT: Rem<Output = T>+ Mul<Output = T>+ Sub<Output = T>+ Add<Output = T>+ One+ FloatConst+ Copy,
{/// Returns the shortest signed angle between two angles.////// Takes wrapping and signs into account.pub fn angle_to(&self, to: Self) -> Self {let two = T::one() + T::one();let max = T::PI() * two;let d = (to.radians - self.radians) % max;Angle::radians(two * d % max - d)}/// Linear interpolation between two angles, using the shortest path.pub fn lerp(&self, other: Self, t: T) -> Self {*self + self.angle_to(other) * t}
}impl<T> Angle<T>
whereT: Float,
{/// Returns `true` if the angle is a finite number.#[inline]pub fn is_finite(self) -> bool {self.radians.is_finite()}
}impl<T> Angle<T>
whereT: Real,
{/// Returns `(sin(self), cos(self))`.pub fn sin_cos(self) -> (T, T) {self.radians.sin_cos()}
}impl<T> Angle<T>
whereT: Zero,
{pub fn zero() -> Self {Angle::radians(T::zero())}
}impl<T> Angle<T>
whereT: FloatConst + Add<Output = T>,
{pub fn pi() -> Self {Angle::radians(T::PI())}pub fn two_pi() -> Self {Angle::radians(T::PI() + T::PI())}pub fn frac_pi_2() -> Self {Angle::radians(T::FRAC_PI_2())}pub fn frac_pi_3() -> Self {Angle::radians(T::FRAC_PI_3())}pub fn frac_pi_4() -> Self {Angle::radians(T::FRAC_PI_4())}
}impl<T> Angle<T>
whereT: NumCast + Copy,
{/// Cast from one numeric representation to another.#[inline]pub fn cast<NewT: NumCast>(&self) -> Angle<NewT> {self.try_cast().unwrap()}/// Fallible cast from one numeric representation to another.pub fn try_cast<NewT: NumCast>(&self) -> Option<Angle<NewT>> {NumCast::from(self.radians).map(|radians| Angle { radians })}// Convenience functions for common casts./// Cast angle to `f32`.#[inline]pub fn to_f32(&self) -> Angle<f32> {self.cast()}/// Cast angle `f64`.#[inline]pub fn to_f64(&self) -> Angle<f64> {self.cast()}
}impl<T: Add<T, Output = T>> Add for Angle<T> {type Output = Self;fn add(self, other: Self) -> Self {Self::radians(self.radians + other.radians)}
}impl<T: Copy + Add<T, Output = T>> Add<&Self> for Angle<T> {type Output = Self;fn add(self, other: &Self) -> Self {Self::radians(self.radians + other.radians)}
}impl<T: Add + Zero> Sum for Angle<T> {fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {iter.fold(Self::zero(), Add::add)}
}impl<'a, T: 'a + Add + Copy + Zero> Sum<&'a Self> for Angle<T> {fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {iter.fold(Self::zero(), Add::add)}
}impl<T: AddAssign<T>> AddAssign for Angle<T> {fn add_assign(&mut self, other: Angle<T>) {self.radians += other.radians;}
}impl<T: Sub<T, Output = T>> Sub<Angle<T>> for Angle<T> {type Output = Angle<T>;fn sub(self, other: Angle<T>) -> <Self as Sub>::Output {Angle::radians(self.radians - other.radians)}
}impl<T: SubAssign<T>> SubAssign for Angle<T> {fn sub_assign(&mut self, other: Angle<T>) {self.radians -= other.radians;}
}impl<T: Div<T, Output = T>> Div<Angle<T>> for Angle<T> {type Output = T;#[inline]fn div(self, other: Angle<T>) -> T {self.radians / other.radians}
}impl<T: Div<T, Output = T>> Div<T> for Angle<T> {type Output = Angle<T>;#[inline]fn div(self, factor: T) -> Angle<T> {Angle::radians(self.radians / factor)}
}impl<T: DivAssign<T>> DivAssign<T> for Angle<T> {fn div_assign(&mut self, factor: T) {self.radians /= factor;}
}impl<T: Mul<T, Output = T>> Mul<T> for Angle<T> {type Output = Angle<T>;#[inline]fn mul(self, factor: T) -> Angle<T> {Angle::radians(self.radians * factor)}
}impl<T: MulAssign<T>> MulAssign<T> for Angle<T> {fn mul_assign(&mut self, factor: T) {self.radians *= factor;}
}impl<T: Neg<Output = T>> Neg for Angle<T> {type Output = Self;fn neg(self) -> Self {Angle::radians(-self.radians)}
}impl<T: ApproxEq<T>> ApproxEq<T> for Angle<T> {#[inline]fn approx_epsilon() -> T {T::approx_epsilon()}#[inline]fn approx_eq_eps(&self, other: &Angle<T>, approx_epsilon: &T) -> bool {self.radians.approx_eq_eps(&other.radians, approx_epsilon)}
}#[test]
fn wrap_angles() {use core::f32::consts::{FRAC_PI_2, PI};assert!(Angle::radians(0.0).positive().approx_eq(&Angle::zero()));assert!(Angle::radians(FRAC_PI_2).positive().approx_eq(&Angle::frac_pi_2()));assert!(Angle::radians(-FRAC_PI_2).positive().approx_eq(&Angle::radians(3.0 * FRAC_PI_2)));assert!(Angle::radians(3.0 * FRAC_PI_2).positive().approx_eq(&Angle::radians(3.0 * FRAC_PI_2)));assert!(Angle::radians(5.0 * FRAC_PI_2).positive().approx_eq(&Angle::frac_pi_2()));assert!(Angle::radians(2.0 * PI).positive().approx_eq(&Angle::zero()));assert!(Angle::radians(-2.0 * PI).positive().approx_eq(&Angle::zero()));assert!(Angle::radians(PI).positive().approx_eq(&Angle::pi()));assert!(Angle::radians(-PI).positive().approx_eq(&Angle::pi()));assert!(Angle::radians(FRAC_PI_2).signed().approx_eq(&Angle::frac_pi_2()));assert!(Angle::radians(3.0 * FRAC_PI_2).signed().approx_eq(&-Angle::frac_pi_2()));assert!(Angle::radians(5.0 * FRAC_PI_2).signed().approx_eq(&Angle::frac_pi_2()));assert!(Angle::radians(2.0 * PI).signed().approx_eq(&Angle::zero()));assert!(Angle::radians(-2.0 * PI).signed().approx_eq(&Angle::zero()));assert!(Angle::radians(-PI).signed().approx_eq(&Angle::pi()));assert!(Angle::radians(PI).signed().approx_eq(&Angle::pi()));
}#[test]
fn lerp() {type A = Angle<f32>;let a = A::radians(1.0);let b = A::radians(2.0);assert!(a.lerp(b, 0.25).approx_eq(&Angle::radians(1.25)));assert!(a.lerp(b, 0.5).approx_eq(&Angle::radians(1.5)));assert!(a.lerp(b, 0.75).approx_eq(&Angle::radians(1.75)));assert!(a.lerp(b + A::two_pi(), 0.75).approx_eq(&Angle::radians(1.75)));assert!(a.lerp(b - A::two_pi(), 0.75).approx_eq(&Angle::radians(1.75)));assert!(a.lerp(b + A::two_pi() * 5.0, 0.75).approx_eq(&Angle::radians(1.75)));
}#[test]
fn sum() {type A = Angle<f32>;let angles = [A::radians(1.0), A::radians(2.0), A::radians(3.0)];let sum = A::radians(6.0);assert_eq!(angles.iter().sum::<A>(), sum);
}

二、待阅读内容

编号内容说明
1.2components组件
1.3systems系统
1.4window窗口
2core核心对象
2.1primitives图元
2.1.1ArcArc对象
2.1.2LineLine对象
3.1angle.rs角度
3.2box2d.rs
3.3homogen.rs
3.4length.rs
3.5point.rs
3.6scale.rs
3.7transform2d.rs
3.8transform3d.rs
3.9vector.rs
3.10box3d.rs
3.11rect.rs
3.12rigid.rs
3.13rotation.rs
3.14side_offsets.rs
3.15size.rs
3.16translation.rs
3.17trig.rs
3.18approxeq.rs
3.19approxord.rs
3.20num.rs
3.21macros.rs

http://www.mrgr.cn/news/80432.html

相关文章:

  • $route和$router的区别
  • Jenkins 启动 程序 退出后 被杀死问题
  • opencv获取摄像头的最大分辨率图像
  • 卫生人员评审专家申报系统|Java|SSM|VUE| 前后端分离
  • 案例讲解自然语言处理(NLP)
  • 深入理解 XPath:XML 和 HTML 文档的利器
  • 【自动控制原理】学习地图
  • WES7汉化教程
  • 02、10个富士胶片模拟的设置
  • fabric.js
  • 【时间序列分析】斯皮尔曼(Spearman)相关系数理论基础及python代码实现
  • 【图像配准】方法总结
  • hive—炸裂函数explode/posexplode
  • 【漫话机器学习系列】010.Bagging算法(Bootstrap Aggregating)
  • 循环神经网络RNN笔记
  • LeetCode1.两数之和(超简单讲解)
  • 医疗领域的网络安全预防:保障患者隐私与医疗数据安全
  • LeetCode 283.移动零(超简单讲解)
  • docker离线安装、linux 安装docker
  • 概率论得学习和整理26:EXCEL 关于plot 折线图--频度折线图的一些细节
  • 通过一个例子学习回溯算法:从方法论到实际应用
  • 课设项目十:智能手电筒(使用金沙滩51单片机)
  • Qt WORD/PDF(三)使用 QAxObject 对 Word 替换(QML)
  • 【系统分析师】-收官整理-已考过
  • Day13洛谷 2043+2042+2040+2035+2034+2033+2030+2027+2031+2029
  • selenium工作原理