string_art 0.1.0-alpha.1

Convert images into thread patterns for creating string art. It generates detailed instructions in text format and provides graphical previews of the resulting patterns.
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::{iter::FusedIterator, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}};

use super::Point;
use crate::Float;
use bresenham::Bresenham;
use image::GenericImage;
use num_traits::AsPrimitive;
use std::fmt;

#[derive(Clone, Copy)]
pub struct Segment<T> {
    pub start: Point<T>,
    pub end: Point<T>,
}

impl<T> Add for Segment<T>
where
    T: Add<Output = T>,
{
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Segment {
            start: self.start + other.start,
            end: self.end + other.end,
        }
    }
}

impl<T> Add<T> for Segment<T>
where
    T: Add<Output = T> + Clone,
{
    type Output = Self;

    fn add(self, scalar: T) -> Self {
        Segment {
            start: self.start + scalar.clone(),
            end: self.end + scalar,
        }
    }
}

impl<T> Sub for Segment<T>
where
    T: Sub<Output = T>,
{
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Segment {
            start: self.start - other.start,
            end: self.end - other.end,
        }
    }
}

impl<T> Sub<T> for Segment<T>
where
    T: Sub<Output = T> + Clone,
{
    type Output = Self;

    fn sub(self, scalar: T) -> Self {
        Segment {
            start: self.start - scalar.clone(),
            end: self.end - scalar,
        }
    }
}

impl<T> Mul for Segment<T>
where
    T: Mul<Output = T>,
{
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        Segment {
            start: self.start * other.start,
            end: self.end * other.end,
        }
    }
}

impl<T> Mul<T> for Segment<T>
where
    T: Mul<Output = T> + Clone,
{
    type Output = Self;

    fn mul(self, scalar: T) -> Self {
        Segment {
            start: self.start * scalar.clone(),
            end: self.end * scalar,
        }
    }
}

impl<T> Div for Segment<T>
where
    T: Div<Output = T>,
{
    type Output = Self;

    fn div(self, other: Self) -> Self {
        Segment {
            start: self.start / other.start,
            end: self.end / other.end,
        }
    }
}

impl<T> Div<T> for Segment<T>
where
    T: Div<Output = T> + Clone,
{
    type Output = Self;

    fn div(self, scalar: T) -> Self {
        Segment {
            start: self.start / scalar.clone(),
            end: self.end / scalar,
        }
    }
}

impl<T> AddAssign for Segment<T>
where
    T: AddAssign,
{
    fn add_assign(&mut self, other: Self) {
        self.start += other.start;
        self.end += other.end;
    }
}

impl<T> AddAssign<T> for Segment<T>
where
    T: AddAssign + Clone,
{
    fn add_assign(&mut self, scalar: T) {
        self.start += scalar.clone();
        self.end += scalar;
    }
}

impl<T> SubAssign for Segment<T>
where
    T: SubAssign,
{
    fn sub_assign(&mut self, other: Self) {
        self.start -= other.start;
        self.end -= other.end;
    }
}

impl<T> SubAssign<T> for Segment<T>
where
    T: SubAssign + Clone,
{
    fn sub_assign(&mut self, scalar: T) {
        self.start -= scalar.clone();
        self.end -= scalar;
    }
}

impl<T> MulAssign for Segment<T>
where
    T: MulAssign,
{
    fn mul_assign(&mut self, other: Self) {
        self.start *= other.start;
        self.end *= other.end;
    }
}

impl<T> MulAssign<T> for Segment<T>
where
    T: MulAssign + Clone,
{
    fn mul_assign(&mut self, scalar: T) {
        self.start *= scalar.clone();
        self.end *= scalar;
    }
}

impl<T> DivAssign for Segment<T>
where
    T: DivAssign,
{
    fn div_assign(&mut self, other: Self) {
        self.start /= other.start;
        self.end /= other.end;
    }
}

impl<T> DivAssign<T> for Segment<T>
where
    T: DivAssign + Clone,
{
    fn div_assign(&mut self, scalar: T) {
        self.start /= scalar.clone();
        self.end /= scalar;
    }
}

impl<T: Neg> Neg for Segment<T>{
    type Output = Segment<T::Output>;

    fn neg(self) -> Self::Output {
        Segment{            
            start: -self.start,
            end: -self.end,
        }
    }
}

impl<T: fmt::Display> fmt::Display for Segment<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:2}, {:2}]", self.start, self.end)
    }
}

impl<T> Segment<T> {
    pub fn new(start: Point<T>, end: Point<T>) -> Self {
        Self { start, end }
    }
}

impl<T: num_traits::NumCast> Segment<T> {
    pub fn cast<I: num_traits::NumCast>(self) -> Option<Segment<I>> {
        self.start
            .cast()
            .and_then(|start| self.end.cast().map(|end| Segment { start, end }))
    }
}

impl<T> Segment<T> {
    pub fn as_<S: Copy + 'static>(self) -> Segment<S>
    where
        T: AsPrimitive<S>,
    {
        Segment {
            start: self.start.as_(),
            end: self.end.as_(),
        }
    }
}

impl<T: Float> Segment<T> {
    pub fn intersection(&self, other: &Segment<T>) -> Option<Point<T>> {
        let p = self.start;
        let q = other.start;
        let r = Point {
            x: self.end.x - self.start.x,
            y: self.end.y - self.start.y,
        };
        let s = Point {
            x: other.end.x - other.start.x,
            y: other.end.y - other.start.y,
        };

        let rxs = r.x * s.y - r.y * s.x;
        let qmp = Point {
            x: q.x - p.x,
            y: q.y - p.y,
        };
        let qpxr = qmp.x * r.y - qmp.y * r.x;

        if rxs.abs() < T::epsilon() {
            // Los segmentos son colineales o paralelos
            return None;
        }

        let t = (qmp.x * s.y - qmp.y * s.x) / rxs;
        let u = qpxr / rxs;

        if (T::ZERO..=T::ONE).contains(&t) && (T::ZERO..=T::ONE).contains(&u) {
            Some(Point {
                x: p.x + t * r.x,
                y: p.y + t * r.y,
            })
        } else {
            None
        }
    }

    pub fn is_m_positive(&self) -> bool {
        let dx = self.end.x - self.start.x;
        if dx.abs() < T::EPSILON {
            return true; // Pendiente infinita
        }
        let dy = self.end.y - self.start.y;

        dy.signum() == dx.signum()
    }

    pub fn draw<I: GenericImage>(&self, img: &mut I, pixel: I::Pixel) {
        for point in self.cast().unwrap().points_between() {
            if point.x >= 0
                && point.y >= 0
                && point.x < img.width() as isize
                && point.y < img.height() as isize
            {
                img.put_pixel(point.x as u32, point.y as u32, pixel);
            }
        }
    }

    pub fn parallel_at_distance(&self, distance: T) -> Self {
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        let length = num_traits::Float::sqrt(dx * dx + dy * dy);
        let ux = dx / length;
        let uy = dy / length;

        let offset_x = -uy * distance;
        let offset_y = ux * distance;

        let new_start = Point {
            x: self.start.x + offset_x,
            y: self.start.y + offset_y,
        };
        let new_end = Point {
            x: self.end.x + offset_x,
            y: self.end.y + offset_y,
        };

        Segment::new(new_start, new_end)
    }

    pub fn floor(&self) -> Self {
        Self {
            start: self.start.floor(),
            end: self.end.floor(),
        }
    }
}

impl Segment<isize> {
    pub fn points_between(&self) -> impl Iterator<Item = Point<isize>> {
        Bresenham::new((self.start.x, self.start.y), (self.end.x, self.end.y))
            .map(|(x, y)| Point { x, y })
    }
}

pub trait IntoSegments<T> {
    type Iterator: Iterator<Item = Segment<T>>;
    fn into_edges(self) -> Self::Iterator;
}

impl<'a, T: Copy> IntoSegments<T> for &'a [Point<T>] {
    type Iterator = PointsIterator<'a, T>;
    fn into_edges(self) -> PointsIterator<'a, T> {
        PointsIterator::new(self)
    }
}

pub struct PointsIterator<'a, T> {
    curr: usize,
    points: &'a [Point<T>],
}

impl<'a, T> PointsIterator<'a, T> {
    fn new(points: &'a [Point<T>]) -> Self {
        Self { curr: 0, points }
    }
}

impl<'a, T: Copy> Iterator for PointsIterator<'a, T> {
    type Item = Segment<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.curr < self.points.len() {
            unsafe {
                let start = *self.points.get_unchecked(self.curr);
                self.curr = self.curr.unchecked_add(1);
                Some(Segment::new(
                    start,
                    *self.points.get_unchecked(if self.curr < self.points.len() {
                        self.curr
                    } else {
                        0
                    }),
                ))
            }
        } else {
            None
        }
    }
}

impl<'a, T: Copy> DoubleEndedIterator for PointsIterator<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.curr > 0 {
            unsafe {
                let start = *self.points.get_unchecked(self.curr);
                self.curr = self.curr.unchecked_sub(1);
                Some(Segment::new(
                    start,
                    *self.points.get_unchecked(if self.curr > 0 {
                        self.curr
                    } else {
                        self.points.len().unchecked_sub(1)
                    }),
                ))
            }
        } else {
            None
        }
    }
}

impl<'a, T: Copy> ExactSizeIterator for PointsIterator<'a, T> {
    fn len(&self) -> usize {
        self.points.len()
    }
}

impl<'a, T> FusedIterator for PointsIterator<'a, T> where T: Copy {}