LinearEquation

Struct LinearEquation 

Source
pub struct LinearEquation {
    pub a: f64,
    pub b: f64,
    pub c: f64,
}
Expand description

代表二维平面上的直线方程。

Fields§

§a: f64

直线方程的 x 系数。

§b: f64

直线方程的 y 系数。

§c: f64

直线方程的常数项。

Implementations§

Source§

impl LinearEquation

Source

pub fn to_string(&self) -> String

将直线方程转换为字符串表示。

Source

pub fn from_points(x1: f64, y1: f64, x2: f64, y2: f64) -> LinearEquation

通过两点计算切线方程的一般式表示。

§参数
  • x1 - 第一个点的 x 坐标。
  • y1 - 第一个点的 y 坐标。
  • x2 - 第二个点的 x 坐标。
  • y2 - 第二个点的 y 坐标。
§返回值

返回切线方程的一般式表示。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let equation = LinearEquation::from_points(1.0, 2.0, 3.0, 4.0);
Source

pub fn from_point_slope(x1: f64, y1: f64, slope: f64) -> Self

从点斜式参数创建一般式方程。

§参数
  • x1 - 直线上的一点的 x 坐标。
  • y1 - 直线上的一点的 y 坐标。
  • slope - 直线的斜率。
§返回值

返回一般式方程的实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let equation = LinearEquation::from_point_slope(1.0, 2.0, 3.0);
Source

pub fn is_parallel_to(&self, other: &LinearEquation) -> bool

检查两条直线是否平行。

§参数
  • other - 与当前直线比较的另一条直线。
§返回值

如果两条直线平行,返回 true,否则返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line1 = LinearEquation { a: 2.0, b: 3.0, c: 4.0 };
let line2 = LinearEquation { a: 2.0, b: 3.0, c: 7.0 };
assert!(line1.is_parallel_to(&line2));
Source

pub fn from_slope_intercept(m: f64, b: f64) -> Self

从截距式方程创建一般式方程。

§参数
  • m - 直线的斜率。
  • b - y 轴截距。
§返回值

返回包含一般式方程的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation::from_slope_intercept(2.0, 3.0);
assert_eq!(line.to_string(), "-2x + y - 3 = 0");
Source

pub fn from_arc( _radius: f64, x1: f64, y1: f64, x2: f64, y2: f64, ) -> LinearEquation

通过圆弧的两个端点计算切线方程的一般式表示。

§参数
  • radius - 圆弧的半径。
  • x1 - 第一个端点的 x 坐标。
  • y1 - 第一个端点的 y 坐标。
  • x2 - 第二个端点的 x 坐标。
  • y2 - 第二个端点的 y 坐标。
§返回值

返回包含切线方程的一般式表示的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation::from_arc(1.0, 0.0, 0.0, 1.0, 0.0);
assert_eq!(line.to_string(), "0x + y - 1 = 0");

todo: 是否需要增加两点+半径求圆弧,再求切线方程

Source

pub fn translate_along_x(&self, h: f64) -> LinearEquation

将直线沿 x 轴平移指定单位,返回新的直线方程。

§参数
  • h - 沿 x 轴的平移距离。
§返回值

返回包含平移后直线方程的一般式表示的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation { a: 1.0, b: 2.0, c: 3.0 };
let translated_line = line.translate_along_x(2.0);
assert_eq!(translated_line.to_string(), "1x + 2y - 1 = 0");
Source

pub fn rotate_around_origin(&self, theta: f64) -> LinearEquation

将直线绕原点逆时针旋转指定弧度,返回新的直线方程。

§参数
  • theta - 逆时针旋转的弧度。
§返回值

返回包含旋转后直线方程的一般式表示的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation { a: 1.0, b: 2.0, c: 3.0 };
let rotated_line = line.rotate_around_origin(1.0);
// 根据旋转后的具体结果进行验证
Source

pub fn rotate_around_point( &self, theta: f64, center: (f64, f64), ) -> LinearEquation

将直线绕任意点逆时针旋转指定弧度,返回新的直线方程。

§参数
  • theta - 逆时针旋转的弧度。
  • center - 旋转中心的坐标。
§返回值

返回包含旋转后直线方程的一般式表示的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation { a: 1.0, b: 2.0, c: 3.0 };
let rotated_line = line.rotate_around_point(1.0, (0.0, 0.0));
// 根据旋转后的具体结果进行验证
Source

pub fn translate(&self, h: f64, k: f64) -> LinearEquation

将直线沿 x 轴平移指定距离,沿 y 轴平移指定距离。

§参数
  • h - 沿 x 轴的平移距离。
  • k - 沿 y 轴的平移距离。
§返回值

返回包含平移后直线方程的一般式表示的 LinearEquation 结构体实例。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation { a: 1.0, b: 2.0, c: 3.0 };
let translated_line = line.translate(2.0, 3.0);
Source

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

计算直线与 X 轴和 Y 轴的夹角(弧度)。

§返回值

返回包含直线与 X 轴和 Y 轴夹角的元组 (angle_with_x_axis, angle_with_y_axis)

§示例
use rs_math::graphical::linear_equation::LinearEquation;
let line = LinearEquation { a: 1.0, b: 2.0, c: 3.0 };
let (angle_with_x_axis, angle_with_y_axis) = line.angles_with_axes();
 
Source

pub fn point_line_relationship(&self, point: &Point2D) -> PointLineRelationship

判断点与直线的位置关系。

§参数
  • point - 要判断的点的坐标。
§返回值

返回 PointLineRelationship 枚举,表示点与直线的位置关系。

§示例
use rs_math::graphical::linear_equation::{LinearEquation, PointLineRelationship};
use rs_math::graphical::point_2d::Point2D;

let line = LinearEquation { a: 1.0, b: -1.0, c: 0.0 };
let point = Point2D { x: 2.0, y: 2.0 };
let relationship = line.point_line_relationship(&point);
 
Source

pub fn is_tangent_to_circle(&self, circle: &Circle) -> bool

判断直线与圆是否相切。

§参数
  • circle - 圆的信息。
§返回值

如果直线与圆相切,返回 true;否则,返回 false

§注意

在比较距离时,考虑到浮点数误差,使用了 EPSILON 常量。

§示例
use rs_math::graphical::linear_equation::LinearEquation;
use rs_math::graphical::circle::Circle;

let line = LinearEquation { a: 1.0, b: -1.0, c: 0.0 };
let circle = Circle::new(0.0, 0.0, 1.0);
let is_tangent = line.is_tangent_to_circle(&circle);
 
Source

pub fn is_vertical_to_x_axis(&self) -> bool

判断直线是否垂直于 X 轴。

§返回值

如果直线垂直于 X 轴,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line = LinearEquation { a: 2.0, b: 0.0, c: 3.0 };
let is_vertical = line.is_vertical_to_x_axis();
 
Source

pub fn is_vertical_to_y_axis(&self) -> bool

判断直线是否垂直于 Y 轴。

§返回值

如果直线垂直于 Y 轴,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line = LinearEquation { a: 0.0, b: 3.0, c: 5.0 };
let is_vertical = line.is_vertical_to_y_axis();
 
Source

pub fn are_intersecting(&self, other: &LinearEquation) -> bool

判断两条直线是否相交。

§返回值

如果两条直线相交,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line1 = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let line2 = LinearEquation { a: -4.0, b: 6.0, c: 8.0 };
let do_intersect = line1.are_intersecting(&line2);
 
Source

pub fn are_parallel(&self, other: &LinearEquation) -> bool

判断两条直线是否平行。

§返回值

如果两条直线平行,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line1 = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let line2 = LinearEquation { a: 4.0, b: -6.0, c: 10.0 };
let are_parallel = line1.are_parallel(&line2);
 
Source

pub fn are_perpendicular(&self, other: &LinearEquation) -> bool

判断两条直线是否垂直。

§返回值

如果两条直线垂直,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line1 = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let line2 = LinearEquation { a: 3.0, b: 2.0, c: -4.0 };
let are_perpendicular = line1.are_perpendicular(&line2);
Source

pub fn is_equal_to(&self, other: &LinearEquation) -> bool

判断两条直线是否相等。

§返回值

如果两条直线相等,返回 true;否则,返回 false

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line1 = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let line2 = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let are_equal = line1.is_equal_to(&line2);
Source

pub fn slope(&self) -> Option<f64>

获取直线的斜率。

§返回值

如果直线与 X 轴垂直,返回 None;否则,返回直线的斜率 Some(slope)

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let slope = line.slope();
Source

pub fn to_slope_intercept_form(&self) -> Option<(f64, f64)>

将一般式直线方程转换为斜率截距形式。

§返回值

如果直线方程的 B 不为零,则返回 Some((slope, intercept)), 其中 slope 是斜率,intercept 是截距。

如果直线方程的 B 为零(垂直于 X 轴),则返回 None,因为斜率不存在。

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let result = line.to_slope_intercept_form();
Source

pub fn to_point_slope_form(&self) -> Option<(f64, (f64, f64))>

将一般式直线方程转换为点斜式。

§返回值

如果直线方程的 B 不为零,则返回 Some((slope, point)), 其中 slope 是斜率,point 是直线上的一点 (x, y)。

如果直线方程的 B 为零(垂直于 X 轴),则返回 None,因为斜率不存在。

§示例
use rs_math::graphical::linear_equation::LinearEquation;

let line = LinearEquation { a: 2.0, b: -3.0, c: 5.0 };
let result = line.to_point_slope_form();

Trait Implementations§

Source§

impl Debug for LinearEquation

Source§

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

Formats the value using the given formatter. Read more

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