1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use crate::util::{
    bresenham_step, clip_rect_entry, clip_rect_exit, destandardize, horizontal_line, standardize,
    vertical_line, Constant, Point,
};
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Range, Rem, Sub, SubAssign};

/// Performs scan conversion of a line segment using Bresenham's algorithm,
/// while clipping it to a specified rectangle.
///
/// The function takes a line defined by two endpoints (inclusive)
/// and a clipping rectangle defined by its corners (inclusive).
/// The provided closure `pixel_op` is responsible for handling each pixel within the clipped region.
/// To ensure optimal performance, it is recommended that `pixel_op` does not perform any bounds checks.
///
/// # Arguments
///
/// * `line`: A tuple representing the endpoints of the line segment, inclusive.
/// The line segment will be drawn between these two points.
///
/// * `clip_rect`: A tuple representing the corners of the clipping rectangle, inclusive.
/// The line segment will be clipped to this rectangle, and only the visible portion will be drawn.
///
/// * `pixel_op`: A closure that takes the coordinates of a pixel within the clipped region.
/// It is invoked for each pixel along the line. You can use this closure to perform any
/// desired pixel-specific operations without the need for bounds checking.
///
/// # Returns
///
/// If any part of the line segment is visible within the clipping rectangle,
/// the function returns an `Option` containing a tuple of two `ClipPoint` values
/// representing the starting and ending points of the visible portion of the line segment.
/// If the line is entirely outside the clipping region, the function returns `None`.
///
/// # Example
///
/// ```rust
/// use clipline::clipline;
///
/// fn draw_pixel(x: isize, y: isize) {
///     // Your custom pixel drawing logic here
///     // No bounds checks necessary here
/// }
///
/// let line = ((0, 0), (10, 10));
/// let clip_rect = ((2, 2), (8, 8));
///
/// let (start, end) = clipline(line, clip_rect, draw_pixel)
///     .expect("line intersects clip_rect");
/// assert_eq!(start, clip_rect.0);
/// assert_eq!(end, clip_rect.1);
/// ```
///
/// # Note
///
/// This is slightly more optimized than the iterator version, but uses internal iteration. Unlike
/// the iterator version, vertical and horizontal lines will always be traversed in an ascending order.
pub fn clipline<T, F>(
    line: (Point<T>, Point<T>),
    clip_rect: (Point<T>, Point<T>),
    mut pixel_op: F,
) -> Option<(Point<T>, Point<T>)>
where
    T: Copy
        + Ord
        + Neg<Output = T>
        + Add<Output = T>
        + AddAssign
        + Sub<Output = T>
        + SubAssign
        + Mul<Output = T>
        + Div<Output = T>
        + Rem<Output = T>
        + MulAssign
        + Constant<Output = T>,
    Range<T>: Iterator<Item = T>,
    F: FnMut(T, T),
{
    let ((x1, y1), (x2, y2)) = line;
    let ((wx1, wy1), (wx2, wy2)) = clip_rect;

    if x1 == x2 {
        return vertical_line(x1, y1, y2, wx1, wx2, wy1, wy2, pixel_op)
            .map(|(cy1, cy2)| ((x1, cy1), (x1, cy2)));
    }

    if y1 == y2 {
        return horizontal_line(y1, x1, x2, wy1, wy2, wx1, wx2, pixel_op)
            .map(|(cx1, cx2)| ((cx1, y1), (cx2, y1)));
    }

    // Implementation of the paper by YP Kuzmin:
    // "Bresenham's Line Generation Algorithm with Built‐in Clipping." (1995)

    let (tx, x1, x2, wx1, wx2) = standardize(x1, x2, wx1, wx2)?;
    let (ty, y1, y2, wy1, wy2) = standardize(y1, y2, wy1, wy2)?;

    let dx = x2 - x1;
    let dy = y2 - y1;

    let (dx2, dy2) = (T::TWO * dx, T::TWO * dy);
    if dx >= dy {
        let (yd, xd, mut err) = clip_rect_entry(y1, x1, wy1, wy2, wx1, wx2, dx, dy2, dx2)?;
        let term = clip_rect_exit(y1, y2, x1, x2, wy2, dx, dy2, dx2);
        let (mut xd, mut yd, term) = destandardize(term, xd, yd, wx2, tx, ty);
        let dx2 = dx2 - dy2;
        let (cx1, cy1) = (xd, yd);
        let (mut cx2, mut cy2) = (cx1, cy1);
        while xd != term {
            pixel_op(xd, yd);
            (cx2, cy2) = (xd, yd);
            (err, xd, yd) = bresenham_step(err, xd, yd, tx, ty, dx2, dy2);
        }
        Some(((cx1, cy1), (cx2, cy2)))
    } else {
        let (xd, yd, mut err) = clip_rect_entry(x1, y1, wx1, wx2, wy1, wy2, dy, dx2, dy2)?;
        let term = clip_rect_exit(x1, x2, y1, y2, wx2, dy, dx2, dy2);
        let (mut yd, mut xd, term) = destandardize(term, yd, xd, wy2, ty, tx);
        let dy2 = dy2 - dx2;
        let (cx1, cy1) = (xd, yd);
        let (mut cx2, mut cy2) = (cx1, cy1);
        while yd != term {
            pixel_op(xd, yd);
            (cx2, cy2) = (xd, yd);
            (err, yd, xd) = bresenham_step(err, yd, xd, ty, tx, dy2, dx2);
        }
        Some(((cx1, cy1), (cx2, cy2)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const fn draw_pixel(_x: isize, _y: isize) {}

    #[test]
    fn test_line_outside_clip() {
        let line = ((0, 0), (10, 10));
        let clip_rect = ((12, 12), (15, 15));

        let result = clipline(line, clip_rect, draw_pixel);
        assert!(result.is_none());
    }

    #[test]
    fn test_line_inside_clip() {
        let line = ((5, 5), (8, 8));
        let clip_rect = ((2, 2), (10, 10));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((5, 5), (8, 8))));
    }

    #[test]
    fn test_line_crosses_top_boundary() {
        let line = ((3, 1), (7, 5));
        let clip_rect = ((2, 2), (8, 4));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((4, 2), (6, 4))));
    }

    #[test]
    fn test_line_crosses_right_boundary() {
        let line = ((6, 3), (12, 7));
        let clip_rect = ((5, 2), (10, 6));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((6, 3), (10, 6))));
    }

    #[test]
    fn test_line_crosses_top_and_right_boundaries() {
        let line = ((7, 1), (13, 6));
        let clip_rect = ((5, 2), (10, 5));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((8, 2), (10, 4))));
    }

    #[test]
    fn test_single_point_line_inside_clip() {
        let line = ((4, 4), (4, 4));
        let clip_rect = ((2, 2), (5, 5));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((4, 4), (4, 4))));
    }

    #[test]
    fn test_single_point_line_outside_clip() {
        let line = ((1, 1), (1, 1));
        let clip_rect = ((2, 2), (5, 5));

        let result = clipline(line, clip_rect, draw_pixel);
        assert!(result.is_none());
    }

    #[test]
    fn test_diagonal_line_top_left_to_bottom_right() {
        let line = ((1, 1), (4, 4));
        let clip_rect = ((2, 2), (3, 3));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((2, 2), (3, 3))));
    }

    #[test]
    fn test_diagonal_line_bottom_left_to_top_right() {
        let line = ((2, 5), (6, 1));
        let clip_rect = ((3, 2), (5, 4));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((3, 4), (5, 2))));
    }

    #[test]
    fn test_diagonal_line_top_left_to_bottom_right_reversed() {
        let line = ((4, 4), (1, 1));
        let clip_rect = ((2, 2), (3, 3));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((3, 3), (2, 2))));
    }

    #[test]
    fn test_diagonal_line_bottom_left_to_top_right_reversed() {
        let line = ((6, 1), (2, 5));
        let clip_rect = ((3, 2), (5, 4));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((5, 2), (3, 4))));
    }

    #[test]
    fn test_line_outside_clip_with_negatives() {
        let line = ((-3, -3), (-1, -1));
        let clip_rect = ((0, 0), (2, 2));

        let result = clipline(line, clip_rect, draw_pixel);
        assert!(result.is_none());
    }

    #[test]
    fn test_line_inside_clip_with_negatives() {
        let line = ((-1, -1), (1, 1));
        let clip_rect = ((-2, -2), (2, 2));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((-1, -1), (1, 1))));
    }

    #[test]
    fn test_line_crosses_top_and_right_boundaries_with_negatives() {
        let line = ((-1, 1), (3, 3));
        let clip_rect = ((-2, 0), (2, 2));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((-1, 1), (1, 2))));
    }

    #[test]
    fn test_line_crosses_bottom_and_left_boundaries_with_negatives() {
        let line = ((-3, -2), (1, -5));
        let clip_rect = ((-4, -4), (0, -1));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((-3, -2), (0, -4))));
    }

    #[test]
    fn test_single_point_line_inside_clip_with_negatives() {
        let line = ((-1, -1), (-1, -1));
        let clip_rect = ((-2, -2), (0, 0));

        let result = clipline(line, clip_rect, draw_pixel);
        assert_eq!(result, Some(((-1, -1), (-1, -1))));
    }

    #[test]
    fn test_single_point_line_outside_clip_with_negatives() {
        let line = ((-3, -3), (-3, -3));
        let clip_rect = ((-2, -2), (0, 0));

        let result = clipline(line, clip_rect, draw_pixel);
        assert!(result.is_none());
    }

    #[test]
    fn test_clip_horizontal_fully_outside() {
        let line = ((0, 2), (10, 2));
        let clip_rect = ((3, 0), (7, 1));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, None);
    }

    #[test]
    fn test_clip_vertical_fully_outside() {
        let line = ((4, 0), (4, 5));
        let clip_rect = ((2, 2), (3, 4));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, None,);
    }

    #[test]
    fn test_clip_horizontal_fully_inside() {
        let line = ((2, 1), (8, 1));
        let clip_rect = ((1, 0), (9, 2));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((2, 1), (8, 1))),);
    }

    #[test]
    fn test_clip_vertical_fully_inside() {
        let line = ((4, 3), (4, 7));
        let clip_rect = ((3, 2), (5, 8));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((4, 3), (4, 7))),);
    }

    #[test]
    fn test_clip_horizontal_partial_from_negative() {
        let line = ((-2, 2), (8, 2));
        let clip_rect = ((0, 0), (6, 4));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((0, 2), (6, 2))),);
    }

    #[test]
    fn test_clip_vertical_partial_from_negative() {
        let line = ((4, -1), (4, 7));
        let clip_rect = ((2, 2), (5, 8));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((4, 2), (4, 7))),);
    }

    #[test]
    fn test_clip_horizontal_partial_from_positive() {
        let line = ((2, 2), (12, 2));
        let clip_rect = ((0, 0), (6, 4));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((2, 2), (6, 2))),);
    }

    #[test]
    fn test_clip_vertical_partial_from_positive() {
        let line = ((4, 3), (4, 12));
        let clip_rect = ((2, 2), (5, 8));

        let clipped_line = clipline(line, clip_rect, |_, _| {});
        assert_eq!(clipped_line, Some(((4, 3), (4, 8))),);
    }

    #[test]
    fn test_all_signed_integers() {
        let points: [(isize, isize); 2] = [(0, 0), (1, 1)];
        fn assert(
            points: [(isize, isize); 2],
            x: impl TryInto<isize> + Sized,
            y: impl TryInto<isize> + Sized,
        ) {
            assert!(points.contains(&(
                x.try_into().unwrap_or_else(|_| unreachable!()),
                y.try_into().unwrap_or_else(|_| unreachable!())
            )))
        }
        clipline::<i8, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
        clipline::<i16, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
        clipline::<i32, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
        clipline::<i64, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
        clipline::<i128, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
        clipline::<isize, _>(((0, 0), (1, 1)), ((0, 0), (1, 1)), |x, y| {
            assert(points, x, y)
        });
    }
}