Ellipse

Struct Ellipse 

Source
pub struct Ellipse { /* private fields */ }
Expand description

表示椭圆的结构体。

§字段

  • h - 椭圆中心的 x 坐标。
  • k - 椭圆中心的 y 坐标。
  • a - 长轴的长度。
  • b - 短轴的长度。
  • phi - 旋转角度(可能为零)。

椭圆是一个二维图形,定义了平面上所有满足特定数学条件的点的集合。 椭圆可以通过其中心坐标、长轴和短轴的长度以及旋转角度来描述。 长轴是通过椭圆中心的两个焦点的距离,短轴是垂直于长轴的轴的长度。 旋转角度表示椭圆相对于坐标轴的旋转程度。

Implementations§

Source§

impl Ellipse

Source

pub fn new(a: f64, b: f64, h: f64, k: f64, phi: f64) -> Ellipse

构造函数,用于创建新的椭圆实例。

§参数
  • a - 长轴的长度。
  • b - 短轴的长度。
  • h - 椭圆中心的 x 坐标。
  • k - 椭圆中心的 y 坐标。
  • phi - 旋转角度(可能为零)。
§返回值

返回一个包含给定参数的椭圆实例。

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 45.0);
Source

pub fn eccentricity(&self) -> f64

计算椭圆离心率的方法。

§返回值

返回椭圆的离心率,计算公式为 sqrt(1 - (b^2 / a^2))。

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 45.0);
let eccentricity = ellipse.eccentricity();
Source

pub fn print_equation(&self)

输出椭圆的方程。

打印椭圆的标准方程,其中 (x - h)^2 / a^2 + (y - k)^2 / b^2 = 1

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 45.0);
ellipse.print_equation();
Source

pub fn area(&self) -> f64

计算椭圆的面积。

使用椭圆的长轴和短轴计算椭圆的面积,公式为 πab,其中 a 为长轴长度,b 为短轴长度。

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 0.0);
let area = ellipse.area();
println!("椭圆面积:{}", area);
Source

pub fn estimate_circumference(&self) -> f64

估算椭圆的周长。

使用椭圆的长轴和短轴估算椭圆的周长。这是一个近似值,使用 Ramanujan 的公式: π * [3(a + b) - sqrt((3a + b)(a + 3b))]

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 0.0);
let circumference = ellipse.estimate_circumference();
println!("椭圆周长的估算值:{}", circumference);
Source

pub fn point_position(&self, x: f64, y: f64) -> PointPosition

判断点和椭圆的关系并返回枚举值。

§参数
  • x - 点的 x 坐标。
  • y - 点的 y 坐标。
§返回值

返回 PointPosition 枚举值,表示点和椭圆的关系。

§示例
use rs_math::graphical::ellipse::{Ellipse, PointPosition};
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 0.0);
let position = ellipse.point_position(2.0, 1.0);
match position {
    PointPosition::OnEllipse => println!("点在椭圆上"),
    PointPosition::InsideEllipse => println!("点在椭圆内部"),
    PointPosition::OutsideEllipse => println!("点在椭圆外部"),
}
Source

pub fn calculate_foci(&self) -> ((f64, f64), (f64, f64))

计算椭圆的焦点坐标。

§返回值

返回包含两个焦点坐标的元组。

§示例
use rs_math::graphical::ellipse::Ellipse;
let ellipse = Ellipse::new(5.0, 3.0, 0.0, 0.0, 0.0);
let foci_coordinates = ellipse.calculate_foci();
println!("焦点1坐标: {:?}", foci_coordinates.0);
println!("焦点2坐标: {:?}", foci_coordinates.1);
Source

pub fn fit(points: &[(f64, f64)]) -> Option<Ellipse>

通过拟合给定点集合来估算椭圆。

§参数
  • points - 包含待拟合椭圆的点集合。
§返回值

如果拟合成功,返回一个包含椭圆参数的 Option<Ellipse>;否则返回 None。

§注意

至少需要 5 个点进行椭圆拟合。

§示例
use rs_math::graphical::ellipse::Ellipse;

let points = vec![(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (0.0, -1.0), (0.0, 0.0)];
let fitted_ellipse = Ellipse::fit(&points);

match fitted_ellipse {
    Some(ellipse) => {
        println!("拟合椭圆成功!");
        ellipse.print_equation();
    }
    None => println!("拟合椭圆失败,至少需要 5 个点。"),
}

Trait Implementations§

Source§

impl Clone for Ellipse

Source§

fn clone(&self) -> Ellipse

Returns a duplicate 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 Ellipse

Source§

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

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

impl PartialEq for Ellipse

Source§

fn eq(&self, other: &Ellipse) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Ellipse

Source§

impl StructuralPartialEq for Ellipse

Auto Trait Implementations§

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, 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.