line_clipping/
lib.rs

1#![no_std]
2//! A rust crate to implement several line clipping algorithms. See the
3//! [documentation](https://docs.rs/line_clipping) for more information. The choice of algorithms is
4//! based on the following article which contains a good summary of the options:
5//!
6//! Matthes D, Drakopoulos V. [Line Clipping in 2D: Overview, Techniques and
7//! Algorithms](https://pmc.ncbi.nlm.nih.gov/articles/PMC9605407/). J Imaging. 2022 Oct
8//! 17;8(10):286. doi: 10.3390/jimaging8100286. PMID: 36286380; PMCID: PMC9605407.
9//!
10//! Supports:
11//!
12//! - [x] [Cohen-Sutherland](crate::cohen_sutherland)
13//!
14//! TODO
15//!
16//! - [ ] Cyrus-Beck
17//! - [ ] Liang-Barsky
18//! - [ ] Nicholl-Lee-Nicholl
19//! - [ ] More comprehensive testing
20//!
21//! # Installation
22//!
23//! ```shell
24//! cargo add line-clipping
25//! ```
26//!
27//! # Usage
28//!
29//! ```rust
30//! use line_clipping::cohen_sutherland::clip_line;
31//! use line_clipping::{LineSegment, Point, Window};
32//!
33//! let line = LineSegment::new(Point::new(-10.0, -10.0), Point::new(20.0, 20.0));
34//! let window = Window::new(0.0, 10.0, 0.0, 10.0);
35//! let clipped_line = clip_line(line, window);
36//! ```
37//!
38//! # License
39//!
40//! Copyright (c) Josh McKinney
41//!
42//! This project is licensed under either of
43//!
44//! - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
45//! - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
46//!
47//! at your option.
48//!
49//! # Contribution
50//!
51//! Contributions are welcome! Please open an issue or submit a pull request.
52//!
53//! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
54//! the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without
55//! any additional terms or conditions.
56pub mod cohen_sutherland;
57
58/// A point in 2D space.
59#[derive(Debug, Clone, Copy, PartialEq)]
60pub struct Point {
61    /// The x coordinate of the point.
62    pub x: f64,
63
64    /// The y coordinate of the point.
65    pub y: f64,
66}
67
68impl Point {
69    /// A point at the origin (0.0, 0.0).
70    pub const ORIGIN: Self = Self { x: 0.0, y: 0.0 };
71
72    /// Creates a new point.
73    #[must_use]
74    pub const fn new(x: f64, y: f64) -> Self {
75        Self { x, y }
76    }
77}
78
79/// A line segment in 2D space.
80#[derive(Debug, Clone, Copy, PartialEq)]
81pub struct LineSegment {
82    /// The first point of the line segment.
83    pub p1: Point,
84
85    /// The second point of the line segment.
86    pub p2: Point,
87}
88
89impl LineSegment {
90    /// Creates a new line segment.
91    #[must_use]
92    pub const fn new(p1: Point, p2: Point) -> Self {
93        Self { p1, p2 }
94    }
95}
96
97/// A rectangular region to clip lines against.
98#[derive(Debug, Clone, Copy)]
99pub struct Window {
100    /// The minimum x coordinate of the window.
101    pub x_min: f64,
102
103    /// The maximum x coordinate of the window.
104    pub x_max: f64,
105
106    /// The minimum y coordinate of the window.
107    pub y_min: f64,
108
109    /// The maximum y coordinate of the window.
110    pub y_max: f64,
111}
112
113impl Window {
114    /// Creates a new window.
115    #[must_use]
116    pub const fn new(x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Self {
117        Self {
118            x_min,
119            x_max,
120            y_min,
121            y_max,
122        }
123    }
124}