Circle

Struct Circle 

Source
pub struct Circle {
    pub x: f64,
    pub y: f64,
    pub radius: f64,
}
Expand description

表示一个圆的结构体。

Fields§

§x: f64

圆的中心坐标

§y: f64§radius: f64

圆的半径

Implementations§

Source§

impl Circle

Source

pub fn from_points_and_radius( point1: &Point2D, point2: &Point2D, radius: f64, ) -> Option<Circle>

通过两点和半径创建圆。

§参数
  • point1 - 圆上的第一个点。
  • point2 - 圆上的第二个点。
  • radius - 圆的半径。
§返回值

如果给定半径有效,返回一个包含圆心和半径的 Circle 结构体实例;否则返回 None

§示例
use rs_math::graphical::point_2d::Point2D;
use rs_math::graphical::circle::Circle;

let point1 = Point2D { x: 0.0, y: 0.0 };
let point2 = Point2D { x: 1.0, y: 0.0 };
let radius = 0.5;
let circle = Circle::from_points_and_radius(&point1, &point2, radius);
Source

pub fn from_points(p1: &Point2D, p2: &Point2D, p3: &Point2D) -> Option<Circle>

通过三个点创建圆。

§参数
  • p1 - 圆上的第一个点。
  • p2 - 圆上的第二个点。
  • p3 - 圆上的第三个点。
§返回值

如果给定三个点共线,返回 None;否则返回一个包含圆心和半径的 Circle 结构体实例。

§示例
use rs_math::graphical::point_2d::Point2D;
use rs_math::graphical::circle::Circle;

let p1 = Point2D { x: 0.0, y: 0.0 };
let p2 = Point2D { x: 1.0, y: 0.0 };
let p3 = Point2D { x: 0.0, y: 1.0 };
let circle = Circle::from_points(&p1, &p2, &p3);
Source

pub fn new(x: f64, y: f64, radius: f64) -> Circle

创建一个新的圆实例。

§参数
  • x - 圆的中心横坐标。
  • y - 圆的中心纵坐标。
  • radius - 圆的半径。
§返回值

返回一个包含给定圆心和半径的 Circle 结构体实例。

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

let circle = Circle::new(0.0, 0.0, 1.0);
Source

pub fn area(&self) -> f64

计算圆的面积。

§返回值

返回圆的面积,使用标准的 π(pi) 值。

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

let circle = Circle::new(0.0, 0.0, 1.0);
let area = circle.area();
Source

pub fn is_point_inside(&self, point_x: f64, point_y: f64) -> bool

判断给定点是否在圆内。

§参数
  • point_x - 待判断点的横坐标。
  • point_y - 待判断点的纵坐标。
§返回值

如果给定点在圆内或在圆上,返回 true;否则返回 false

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

let circle = Circle::new(0.0, 0.0, 1.0);
let is_inside = circle.is_point_inside(0.5, 0.5);
Source

pub fn generate_points(&self, num_points: usize) -> Vec<Point2D>

生成圆上的点。

§参数
  • num_points - 要生成的点的数量。
§返回值

返回一个包含圆上生成点的 Vec<Point2D>

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::point_2d::Point2D;

let circle = Circle::new(0.0, 0.0, 1.0);
let points = circle.generate_points(10);
Source

pub fn is_point_on_arc( &self, start_angle: f64, end_angle: f64, point: &Point2D, ) -> bool

判断点是否在圆弧上。

§参数
  • start_angle - 圆弧的起始角度。
  • end_angle - 圆弧的结束角度。
  • point - 待判断的点。
§返回值

如果给定点在圆弧上,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::point_2d::Point2D;

let circle = Circle::new(0.0, 0.0, 1.0);
let start_angle = 0.0;
let end_angle = std::f64::consts::PI;
let point = Point2D { x: 1.0, y: 0.0 };
let is_on_arc = circle.is_point_on_arc(start_angle, end_angle, &point);
Source

pub fn is_angle_in_range( &self, start_angle: f64, end_angle: f64, point: &Point2D, ) -> bool

判断夹角是否在指定范围内的辅助函数。

§参数
  • start_angle - 范围的起始角度。
  • end_angle - 范围的结束角度。
  • point - 待判断的点。
§返回值

如果给定点的夹角在指定范围内,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::point_2d::Point2D;

let circle = Circle::new(0.0, 0.0, 1.0);
let start_angle = 0.0;
let end_angle = std::f64::consts::PI;
let point = Point2D { x: 1.0, y: 0.0 };
let is_in_range = circle.is_angle_in_range(start_angle, end_angle, &point);
Source

pub fn is_point_on_circle_boundary(&self, point: &Point2D) -> bool

判断点是否在圆边界上。

§参数
  • point - 待判断的点。
§返回值

如果给定点在圆边界上,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::point_2d::Point2D;

let circle = Circle::new(0.0, 0.0, 1.0);
let point = Point2D { x: 1.0, y: 0.0 };
let is_on_boundary = circle.is_point_on_circle_boundary(&point);
Source

pub fn find_line_intersection(&self, p1: &Point2D, p2: &Point2D) -> Vec<Point2D>

寻找与直线的交点。

§参数
  • p1 - 直线上的第一个点。
  • p2 - 直线上的第二个点。
§返回值

返回一个包含交点的 Vec<Point2D>

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::point_2d::Point2D;

let circle = Circle::new(0.0, 0.0, 1.0);
let point1 = Point2D { x: -2.0, y: 0.0 };
let point2 = Point2D { x: 2.0, y: 0.0 };
let intersections = circle.find_line_intersection(&point1, &point2);
Source

pub fn circles_intersect(&self, other: &Circle) -> bool

判断两个圆是否相交。

§参数
  • other - 另一个圆的实例。
§返回值

如果两个圆相交,返回 true;否则返回 false

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

let circle1 = Circle::new(0.0, 0.0, 1.0);
let circle2 = Circle::new(2.0, 0.0, 1.0);
let do_intersect = circle1.circles_intersect(&circle2);
Source

pub fn circles_touch(&self, other: &Circle) -> bool

判断两个圆是否相切。

§参数
  • other - 另一个圆的实例。
§返回值

如果两个圆相切,返回 true;否则返回 false

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

let circle1 = Circle::new(0.0, 0.0, 1.0);
let circle2 = Circle::new(2.0, 0.0, 1.0);
let do_touch = circle1.circles_touch(&circle2);
Source

pub fn circle_contains(&self, other: &Circle) -> bool

判断一个圆是否完全包含另一个圆。

§参数
  • other - 另一个圆的实例。
§返回值

如果一个圆完全包含另一个圆,返回 true;否则返回 false

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

let large_circle = Circle::new(0.0, 0.0, 2.0);
let small_circle = Circle::new(0.0, 0.0, 1.0);
let does_contain = large_circle.circle_contains(&small_circle);
Source

pub fn circle_inside_rectangle(&self, rect: &Rectangle) -> bool

判断圆心是否在矩形内。

公式:circle_x >= rect.x1 && circle_x <= rect.x2 && circle_y >= rect.y1 && circle_y <= rect.y2

圆心的 x 坐标在矩形的 x 范围内,且圆心的 y 坐标在矩形的 y 范围内。

§参数
  • rect - 包含矩形的实例。
§返回值

如果圆心在矩形内,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::rectangle::Rectangle;

let circle = Circle::new(1.0, 1.0, 2.0);
let rectangle = Rectangle::new(0.0, 0.0, 3.0, 3.0);
let is_inside = circle.circle_inside_rectangle(&rectangle);
Source

pub fn circle_on_rectangle_edge(&self, rect: &Rectangle) -> bool

判断圆心是否在矩形的某个边上。

公式:

  • (circle_x == rect.x1 || circle_x == rect.x2) && circle_y >= rect.y1 && circle_y <= rect.y2
  • circle_x >= rect.x1 && circle_x <= rect.x2 && (circle_y == rect.y1 || circle_y == rect.y2)

圆心在矩形的 x 或 y 范围的一个边界上,但不在矩形内部。

§参数
  • rect - 包含矩形的实例。
§返回值

如果圆心在矩形的边上,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::rectangle::Rectangle;

let circle = Circle::new(2.0, 2.0, 1.0);
let rectangle = Rectangle::new(1.0, 1.0, 3.0, 3.0);
let on_edge = circle.circle_on_rectangle_edge(&rectangle);
Source

pub fn circle_on_rectangle_corner(&self, rect: &Rectangle) -> bool

判断圆心是否在矩形的角上。

公式:

  • (circle_x == rect.x1 && circle_y == rect.y1)
  • (circle_x == rect.x1 && circle_y == rect.y2)
  • (circle_x == rect.x2 && circle_y == rect.y1)
  • (circle_x == rect.x2 && circle_y == rect.y2)

圆心在矩形的 x 或 y 范围的一个边界上,并且与另一个边界相交。

§参数
  • rect - 包含矩形的实例。
§返回值

如果圆心在矩形的角上,返回 true;否则返回 false

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::rectangle::Rectangle;

let circle = Circle::new(1.0, 1.0, 0.5);
let rectangle = Rectangle::new(0.0, 0.0, 2.0, 2.0);
let on_corner = circle.circle_on_rectangle_corner(&rectangle);
Source

pub fn bounding_box(&self) -> Rectangle

获取圆的外接矩形。

外接矩形的左上角坐标为 (circle_x - radius, circle_y - radius), 右下角坐标为 (circle_x + radius, circle_y + radius)

§返回值

返回一个包含外接矩形坐标的 Rectangle 结构体实例。

§示例
use rs_math::graphical::circle::Circle;
use rs_math::graphical::rectangle::Rectangle;

let circle = Circle::new(3.0, 4.0, 2.0);
let bounding_box = circle.bounding_box();

Trait Implementations§

Source§

impl Debug for Circle

Source§

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

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

impl PartialEq for Circle

Source§

fn eq(&self, other: &Circle) -> 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 StructuralPartialEq for Circle

Auto Trait Implementations§

§

impl Freeze for Circle

§

impl RefUnwindSafe for Circle

§

impl Send for Circle

§

impl Sync for Circle

§

impl Unpin for Circle

§

impl UnwindSafe for Circle

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.