Debian Codegraph @

src/lib.rs

raw symbols SCIP
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 //! # Minimum supported Rust version
28 //!
29 //! The crate is built with Rust 1.85 to match the 2024 edition. The MSRV may increase in a
30 //! future minor release, but will be noted in the changelog.
31 //!
32 //! # Usage
33 //!
34 //! ```rust
35 //! use line_clipping::cohen_sutherland::clip_line;
36 //! use line_clipping::{LineSegment, Point, Window};
37 //!
38 //! let line = LineSegment::new(Point::new(-10.0, -10.0), Point::new(20.0, 20.0));
39 //! let window = Window::new(0.0, 10.0, 0.0, 10.0);
40 //! let clipped_line = clip_line(line, window);
41 //! ```
42 //!
43 //! # License
44 //!
45 //! Copyright (c) Josh McKinney
46 //!
47 //! This project is licensed under either of
48 //!
49 //! - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
50 //! - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
51 //!
52 //! at your option.
53 //!
54 //! # Contribution
55 //!
56 //! Contributions are welcome! Please open an issue or submit a pull request.
57 //!
58 //! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
59 //! the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without
60 //! any additional terms or conditions.
61 pub mod cohen_sutherlandrust-analyzer cargo line-clipping 0.3.5 cohen_sutherland/;
62
63 /// A point in 2D space.
64 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
65 pub struct Pointrust-analyzer cargo line-clipping 0.3.5 Point# {
66 /// The x coordinate of the point.
67 pub xrust-analyzer cargo line-clipping 0.3.5 Point#x.: f64,
68
69 /// The y coordinate of the point.
70 pub yrust-analyzer cargo line-clipping 0.3.5 Point#y.: f64,
71 }
72
73 impl Pointrust-analyzer cargo line-clipping 0.3.5 Point# {
74 /// A point at the origin (0.0, 0.0).
75 pub const ORIGINrust-analyzer cargo line-clipping 0.3.5 impl#[Point]ORIGIN.: Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Point] = Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Point] { xrust-analyzer cargo line-clipping 0.3.5 Point#x.: 0.0, yrust-analyzer cargo line-clipping 0.3.5 Point#y.: 0.0 };
76
77 /// Creates a new point.
78 #[must_use]
79 pub const fn newrust-analyzer cargo line-clipping 0.3.5 impl#[Point]new().(x: f64, y: f64) -> Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Point] {
80 Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Point] { xrust-analyzer cargo line-clipping 0.3.5 Point#x., yrust-analyzer cargo line-clipping 0.3.5 Point#y. }
81 }
82 }
83
84 /// A line segment in 2D space.
85 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
86 pub struct LineSegmentrust-analyzer cargo line-clipping 0.3.5 LineSegment# {
87 /// The first point of the line segment.
88 pub p1rust-analyzer cargo line-clipping 0.3.5 LineSegment#p1.: Pointrust-analyzer cargo line-clipping 0.3.5 Point#,
89
90 /// The second point of the line segment.
91 pub p2rust-analyzer cargo line-clipping 0.3.5 LineSegment#p2.: Pointrust-analyzer cargo line-clipping 0.3.5 Point#,
92 }
93
94 impl LineSegmentrust-analyzer cargo line-clipping 0.3.5 LineSegment# {
95 /// Creates a new line segment.
96 #[must_use]
97 pub const fn newrust-analyzer cargo line-clipping 0.3.5 impl#[LineSegment]new().(p1: Pointrust-analyzer cargo line-clipping 0.3.5 Point#, p2: Pointrust-analyzer cargo line-clipping 0.3.5 Point#) -> Selfrust-analyzer cargo line-clipping 0.3.5 impl#[LineSegment] {
98 Selfrust-analyzer cargo line-clipping 0.3.5 impl#[LineSegment] { p1rust-analyzer cargo line-clipping 0.3.5 LineSegment#p1., p2rust-analyzer cargo line-clipping 0.3.5 LineSegment#p2. }
99 }
100 }
101
102 /// A rectangular region to clip lines against.
103 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
104 pub struct Windowrust-analyzer cargo line-clipping 0.3.5 Window# {
105 /// The minimum x coordinate of the window.
106 pub x_minrust-analyzer cargo line-clipping 0.3.5 Window#x_min.: f64,
107
108 /// The maximum x coordinate of the window.
109 pub x_maxrust-analyzer cargo line-clipping 0.3.5 Window#x_max.: f64,
110
111 /// The minimum y coordinate of the window.
112 pub y_minrust-analyzer cargo line-clipping 0.3.5 Window#y_min.: f64,
113
114 /// The maximum y coordinate of the window.
115 pub y_maxrust-analyzer cargo line-clipping 0.3.5 Window#y_max.: f64,
116 }
117
118 impl Windowrust-analyzer cargo line-clipping 0.3.5 Window# {
119 /// Creates a new window.
120 #[must_use]
121 pub const fn newrust-analyzer cargo line-clipping 0.3.5 impl#[Window]new().(x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Window] {
122 Selfrust-analyzer cargo line-clipping 0.3.5 impl#[Window] {
123 x_minrust-analyzer cargo line-clipping 0.3.5 Window#x_min.,
124 x_maxrust-analyzer cargo line-clipping 0.3.5 Window#x_max.,
125 y_minrust-analyzer cargo line-clipping 0.3.5 Window#y_min.,
126 y_maxrust-analyzer cargo line-clipping 0.3.5 Window#y_max.,
127 }
128 }
129 }