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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
extern crate reed_solomon;
extern crate num;
use std::ops::{Range, BitXorAssign};
use std::fmt::{Binary, Debug};
use self::reed_solomon::{Encoder, Buffer};
use self::num::PrimInt;
use qr_encoder::cell::{
Cell,
Point,
CellType,
Color,
PlotPoint
};
use qr_encoder::util::{CodeWord, get_indices_for_dimensions};
pub enum ECLevel {
Low,
Medium,
Q,
High,
}
pub enum EncodingMode {
Numeric,
AlphaNumeric,
Byte,
Japanese
}
pub struct QRConfig {
pub version: usize,
pub data: Vec<u8>,
pub codewords: Vec<u8>,
pub codeword_properties: CodeWord,
pub mask: usize,
pub encoding: u8,
pub encoding_mode: EncodingMode,
pub debug_mode: bool,
pub requires_alignment: bool,
pub finder_points: [(usize, usize); 3],
pub size: usize,
pub err_correction_level: ECLevel
}
const ECC_FORMAT_MASK: u16 = 21522;
const GEN_POLY_VERSION: u32 = 7973;
const GEN_POLY_FORMAT: u16 = 1335;
pub fn ecc_format<T>(data: T, gen_poly: T, gen_mask: Option<T>) -> T where
T: PrimInt + Binary + Debug + BitXorAssign<T> {
let (limit, mut format_str) = if gen_mask.is_some() {
(5, data << 10)
} else {
(19, data << 12)
};
while format_str.leading_zeros() <= limit {
let diff = gen_poly.leading_zeros() - format_str.leading_zeros();
format_str ^= gen_poly << diff as usize;
}
match gen_mask {
Some(mask) => ((data << 10) | format_str) ^ mask,
None => (data << 12) | format_str
}
}
fn interleave_blocks(blocks: &[Buffer], block_size: usize, ecc_block_size: usize) -> Vec<u8> {
let mut data: Vec<u8> = vec![];
for i in 0..block_size {
for block in blocks {
if let Some(cw) = block.data().get(i) {
data.push(*cw);
} else {
println!("bleh: {}, block_size: {}", i, block_size);
}
}
}
for i in 0..ecc_block_size {
for block in blocks {
if let Some(cw) = block.ecc().get(i) {
data.push(*cw);
} else {
panic!("ADLKJSDLFKJD ECC {}", i);
}
}
}
data
}
impl QRConfig {
pub fn get_ecc_length(&self) -> usize {
self.codeword_properties.ecc_codeword_count
}
pub fn get_remainder_bit_length(&self) -> isize {
match self.version {
2...6 => 7,
14...20 | 28...34 => 3,
_ => 0
}
}
pub fn get_mask_pattern(&self, n: usize) -> Box<Fn(usize, usize) -> bool> {
match n {
0 => Box::new(move |row: usize, col: usize| (row + col) % 2 == 0),
1 => Box::new(move |row: usize, _: usize| row % 2 == 0),
2 => Box::new(move |_: usize, col: usize| col % 3 == 0),
3 => Box::new(move |row: usize, col: usize| (row + col) % 3 == 0),
4 => Box::new(move |row: usize, col: usize| ((row / 2) + (col / 3)) % 2 == 0),
5 => Box::new(move |row: usize, col: usize| ((row * col) % 2) + ((row * col) % 3) == 0),
6 => Box::new(move |row: usize, col: usize| (((row * col) % 2) + ((row * col) % 3) ) % 2 == 0),
_ => Box::new(move |row: usize, col: usize| (((row + col) % 2) + ((row * col) % 3) ) % 2 == 0)
}
}
pub fn apply_version_information(&self, body: &mut Vec<Cell>) {
let canvas_size = self.size as isize;
let origin = (canvas_size - 12) as isize;
let bit_string = ecc_format::<u32>(self.version as u32, GEN_POLY_VERSION, None);
let upper_right_indices = get_indices_for_dimensions(origin, 1, canvas_size - 3);
let lower_left_indices = get_indices_for_dimensions(origin * canvas_size, canvas_size, (-canvas_size * 3) + 1);
for (bit_pos, i) in upper_right_indices.iter().enumerate() {
let is_bit = ((bit_string >> bit_pos) & 1) > 0;
let color: Color = if is_bit {
Color { r: 0, b: 0, g: 0 }
} else {
Color { r: 255, b: 255, g: 255 }
};
match body.get_mut(*i) {
Some(c) => c.color = color,
None => {}
}
}
for (bit_index, idx) in lower_left_indices.iter().enumerate() {
let is_bit = (bit_string & (1 << bit_index)) > 0;
let color: Color = if is_bit {
Color { r: 0, b: 0, g: 0 }
} else {
Color { r: 255, b: 255, g: 255 }
};
match body.get_mut(*idx) {
Some(c) => c.color = color,
None => {}
}
}
}
pub fn encode_format_areas(&self, body: &mut Vec<Cell>, pattern: u8) {
let ec_level: u8 = match self.err_correction_level {
ECLevel::Low => 1,
ECLevel::Medium => 0,
ECLevel::Q => 3,
_ => 2
};
let data = (ec_level << 3) | pattern;
let format_str = ecc_format::<u16>(data as u16, GEN_POLY_FORMAT, Some(ECC_FORMAT_MASK));
let mut bit_position = 14;
let mut x = 8;
let mut y = 0;
while y != self.size {
let bit = format_str & (1 << bit_position);
let color: Color = if bit == 0 {
Color { r: 255, g: 255, b: 255 }
} else {
Color { r: 0, g: 0, b: 0 }
};
let idx = (x * self.size) + y;
let cell = body.get_mut(idx).unwrap();
match cell.module_type {
CellType::Format => cell.color = color,
CellType::Timing => {
if x == 8 {
y += 1;
} else {
x -= 1;
}
continue;
},
CellType::DarkModule => {
x = 8;
y = self.size - 8;
continue;
}
_ => panic!("What is this? {:?}", cell)
}
if x == 8 && (y < 8 || y >= self.size - 8) {
y += 1;
} else if y == 8 && x > 0 {
x -= 1;
} else if x == 0 && y == 8 {
x = self.size - 1;
}
if bit_position == 0 {
bit_position = 14;
} else {
bit_position -= 1;
}
}
}
pub fn apply_mask_pattern(&self, body: &mut Vec<Cell>, n: usize) {
let pattern = self.get_mask_pattern(n);
for cell in body {
match cell.module_type {
CellType::Message => {
let flip_module = pattern(cell.point.0, cell.point.1);
if flip_module && cell.is_black() {
cell.color = Color { r: 255, g: 255, b: 255 };
} else if flip_module {
cell.color = Color { r: 0, g: 0, b: 0 };
}
},
_ => {}
}
}
}
pub fn eval_penalty_scores(&self, body: &Vec<Cell>) -> usize {
let one = self.penalty_score_eval_one(body);
let two = self.penalty_score_eval_two(body);
let three = self.penalty_score_eval_three(body);
let four = self.penalty_score_eval_four(body);
let total = one + two + three + four;
total
}
pub fn penalty_score_eval_two(&self, body: &Vec<Cell>) -> usize {
let mut penalty_total = 0;
let canvas_size = self.size;
let adjacent_coords = [
canvas_size,
canvas_size + 1,
1
];
for x in 0..(canvas_size - 1) {
for y in 0..(canvas_size - 1) {
let idx = (x * canvas_size) + y;
let is_black = body[idx].is_black();
let square = adjacent_coords.into_iter()
.map(|&i| body[i + idx].is_black())
.all(|p| p == is_black);
if square {
penalty_total += 3;
}
}
}
penalty_total
}
fn check_column(&self, body: &Vec<Cell>, column: isize) -> usize {
let mut subtotal = 0;
let pattern_mask: u16 = 0b00001011101;
let reverse_mask: u16 = 0b10111010000;
let remap_pattern: u16 = 0b11111111111;
let mut current_pattern = 0;
for row in 0..self.size {
let idx = (row * self.size) + column as usize;
let cell = &body[idx];
if cell.is_black() {
current_pattern = ((current_pattern << 1) ^ 1) & remap_pattern;
} else {
current_pattern = (current_pattern << 1) & remap_pattern;
}
if row >= 9 && current_pattern == pattern_mask || current_pattern == reverse_mask {
subtotal += 40;
}
}
subtotal
}
fn check_row(&self, body: &Vec<Cell>, row: isize) -> usize {
let mut subtotal = 0;
let pattern_mask: u16 = 0b00001011101;
let reverse_mask: u16 = 0b10111010000;
let remap_pattern: u16 = 0b11111111111;
let mut current_pattern = 0;
for column in 0..self.size {
let idx = ((row as usize) * self.size) + column as usize;
let cell = &body[idx];
if cell.is_black() {
current_pattern = ((current_pattern << 1) ^ 1) & remap_pattern;
} else {
current_pattern = (current_pattern << 1) & remap_pattern;
}
if column >= 9 && current_pattern == pattern_mask || current_pattern == reverse_mask {
subtotal += 40;
}
}
subtotal
}
pub fn penalty_score_eval_three(&self, body: &Vec<Cell>) -> usize {
let mut penalty_total = 0;
for i in 0..self.size {
penalty_total += self.check_column(body, i as isize);
penalty_total += self.check_row(body, i as isize);
}
penalty_total
}
pub fn penalty_score_eval_one(&self, body: &Vec<Cell>) -> usize {
let canvas_size = self.size;
let mut current_row = 0;
let mut consecutive_same_color = 1;
let mut tracking_black = false;
let mut penalty_total = 0;
for cell in body {
if cell.point.0 != current_row {
if consecutive_same_color >= 5 {
penalty_total += consecutive_same_color - 2;
}
consecutive_same_color = 1;
tracking_black = cell.is_black();
current_row = cell.point.0;
continue;
}
let is_black = cell.is_black();
if (tracking_black && is_black) || (!tracking_black && !is_black) {
consecutive_same_color += 1;
} else if (tracking_black && !is_black) || (!tracking_black && is_black) {
if consecutive_same_color >= 5 {
penalty_total += consecutive_same_color - 2;
}
consecutive_same_color = 1;
tracking_black = is_black;
}
}
let cell_count = body.len();
let mut idx = 0;
while idx < cell_count {
let cell = &body[idx];
let is_black = cell.is_black();
if (tracking_black && is_black) || (!tracking_black && !is_black) {
consecutive_same_color += 1;
} else if (tracking_black && !is_black) || (!tracking_black && is_black) {
if consecutive_same_color >= 5 {
penalty_total += consecutive_same_color - 2;
}
consecutive_same_color = 1;
tracking_black = is_black;
}
if cell.point.1 == (canvas_size - 1) && cell.point.0 == (canvas_size - 1) {
break;
} else if cell.point.0 == canvas_size - 1 {
idx = cell.point.1 + 1;
consecutive_same_color = 1;
} else {
idx += canvas_size;
}
}
penalty_total
}
pub fn penalty_score_eval_four(&self, body: &Vec<Cell>) -> usize {
let total_modules = body.len() as f64;
let black_modules: f64 = body.iter()
.fold(0.0, |acc, ref c| {
if c.is_black() {
acc + 1.0
} else {
acc
}
});
let black_percentage = ((black_modules / total_modules) * 100.0).round() as usize;
let remainder = black_percentage % 5;
let prev_mul = black_percentage - remainder;
let next_mul = prev_mul + 5;
let prev_abs = (50 - prev_mul as isize).abs();
let next_abs = (50 - next_mul as isize).abs();
let prev_div = prev_abs / 5;
let next_div = next_abs / 5;
if prev_div < next_div {
(prev_div * 10) as usize
} else {
(next_div * 10) as usize
}
}
pub fn verify_version(&mut self) {
}
pub fn debug_data(&self) {
let ref data = self.data;
let ref codewords = self.codewords;
println!("data {}, codewords {}", data.len(), codewords.len());
println!("cw: {:?}", self.codeword_properties);
}
pub fn encode_error_correction_codewords(&mut self) {
let ecc_len = self.codeword_properties.ecc_codeword_count;
let ecc_per_block = ecc_len / self.codeword_properties.block_count;
let encoder = Encoder::new(ecc_per_block);
let (group_one, group_two) = self.codeword_properties.get_data_cw_total_for_groups();
let data_codewords = &mut self.codewords;
let mid_point = group_one.blocks * group_one.codewords_per_block;
let mut blocks: Vec<Buffer> = vec![];
let mut data_section: Vec<u8> = vec![];
{
let (first_group_data, second_group_data) = data_codewords.split_at(mid_point);
for data_block in first_group_data.chunks(group_one.codewords_per_block) {
let buffer = encoder.encode(data_block);
blocks.push(buffer);
}
if group_two.blocks > 0 {
for data_block in second_group_data.chunks(group_two.codewords_per_block) {
let buffer = encoder.encode(data_block);
blocks.push(buffer);
}
}
let codeword_max = if group_one.codewords_per_block > group_two.codewords_per_block {
group_one.codewords_per_block
} else {
group_two.codewords_per_block
};
let mut interleaved_data = interleave_blocks(&blocks[..], codeword_max, ecc_per_block);
data_section.append(&mut interleaved_data);
}
*data_codewords = data_section;
}
pub fn translate_data(&mut self) {
let data_cw_length = self.codeword_properties.get_data_codeword_length();
let data_length = self.data.len() as u16;
let encoding = self.encoding;
let copied_data = self.data.clone();
{
let codewords = &mut self.codewords;
let mut first_byte = encoding << 4;
let mut second_byte: u8 = data_length as u8;
if self.version > 9 {
second_byte = (data_length >> 8) as u8;
codewords.push(first_byte | (second_byte >> 4));
first_byte = second_byte << 4;
second_byte = data_length as u8;
} else {
second_byte = data_length as u8;
}
let mut index = 0;
loop {
codewords.push(first_byte | (second_byte >> 4));
first_byte = second_byte << 4;
if let Some(byte) = copied_data.get(index) {
second_byte = *byte;
index += 1;
} else {
codewords.push(second_byte << 4);
break;
}
}
}
let mut swap = false;
let mut n = 0;
while self.codewords.len() < data_cw_length {
n += 1;
if swap {
self.codewords.push(17u8);
} else {
self.codewords.push(236u8);
}
swap = !swap;
}
if self.debug_mode {
println!("THIS IS HOW MANY PADDED BYTES ARE ADDED BEFORE INTERLEAVING {}", n);
println!("after translate {}", self.codewords.len());
for (idx, cw) in self.codewords.iter().enumerate() {
println!("Codeword {}: {:08b}", idx, cw);
}
}
}
pub fn create_body(&self) -> Vec<Cell> {
let mut rows: Vec<Cell> = vec![];
let row_len = self.size;
for x in 0..row_len {
for y in 0..row_len {
let cell = Cell {
point: Point(x as usize, y as usize),
value: 0,
color: Color { r: 255, g: 255, b: 255 },
module_type: CellType::None
};
rows.push(cell);
}
}
rows
}
pub fn get_content_length(&self) -> usize {
let modifier = match self.version {
1...10 => 0,
11...27 => 2,
_ => 4
};
match self.encoding {
1 => 10 + modifier,
2 => 9 + modifier,
8 => 12 + modifier,
_ => {
if self.version < 10 {
8
} else {
16
}
}
}
}
pub fn apply_version_information_areas(&self, body: &mut Vec<Cell>) {
let mut x = self.size - 11;
let mut y = 0;
let mut blocks = 6 * 3;
while blocks > 0 {
let indices: [usize; 2] = [
x * self.size + y,
y * self.size + x
];
for index in indices.into_iter() {
match body.get_mut(*index) {
Some(cell) => {
cell.module_type = CellType::VersionInformation;
cell.color = Color { r: 200, g: 200, b: 123 };
},
None => {}
}
}
if y < 5 {
y += 1;
} else {
y = 0;
x += 1;
}
blocks -= 1;
}
}
pub fn apply_reserve_format_areas(&self, body: &mut Vec<Cell>) {
let mut vertical: Point<usize> = Point(0, 8);
let mut horizontal: Point<usize> = Point(8, 0);
while horizontal.1 < self.size {
let idx = (horizontal.0 * self.size) + horizontal.1;
match body.get_mut(idx) {
Some(cell) => {
cell.module_type = CellType::Format;
cell.color = Color { r: 10, g: 140, b: 230 };
},
None => {}
}
if horizontal.1 > 7 && horizontal.1 < self.size - 8 {
horizontal = Point(8, self.size - 8);
} else {
horizontal = Point(8, horizontal.1 + 1);
}
}
while vertical.0 < self.size {
let idx = (vertical.0 * self.size) + vertical.1;
match body.get_mut(idx) {
Some(cell) => {
cell.module_type = CellType::Format;
cell.color = Color { r: 10, g: 140, b: 230 };
},
None => {}
}
if vertical.0 > 7 && vertical.0 < self.size - 8 {
vertical = Point(self.size - 8, 8);
} else {
vertical = Point(vertical.0 + 1, 8);
}
}
}
pub fn apply_dark_module(&self, body: &mut Vec<Cell>) {
let dark_module_coord: Point<usize> = Point((4 * self.version) + 9, 8);
let idx = (dark_module_coord.0 * self.size) + dark_module_coord.1;
match body.get_mut(idx) {
Some(cell) => {
cell.module_type = CellType::DarkModule;
cell.color = Color { r: 0, g: 0, b: 0 };
},
None => {}
}
}
pub fn apply_alignment_patterns(&self, body: &mut Vec<Cell>, points: &Vec<PlotPoint>) {
for plot_point in points {
let idx = (plot_point.point.0 * self.size) + plot_point.point.1;
match body.get_mut(idx) {
Some(cell) => {
cell.module_type = CellType::Alignment;
cell.color = plot_point.color
},
None => {}
}
}
}
pub fn apply_separators(&self, body: &mut Vec<Cell>, alignment_point: (usize, usize)) {
let row_len = self.size;
let (mut x, mut y) = alignment_point;
let mut start_x = 0;
let mut start_y = 0;
let mut end_x = 0;
let mut end_y = 0;
if x == y {
start_x = 7;
end_y = 7;
} else if x > y {
start_x = row_len - 8;
end_x = row_len;
end_y = 7;
} else {
start_y = row_len - 8;
end_y = row_len;
end_x = 7;
}
x = start_x;
y = start_y;
loop {
let pt: Point<usize> = Point(x, y);
let idx = (pt.0 * self.size) + pt.1;
match body.get_mut(idx) {
Some(c) => {
c.module_type = CellType::Separator;
c.color = Color { r: 255, g: 255, b: 255 };
},
None => panic!("dunno idx {} x: {} y: {}", idx, x, y)
}
if start_x == end_y && y < end_y {
y += 1;
} else if end_y == y && x > end_x {
x -= 1;
} else if end_x > x && start_y > x {
x += 1;
} else if end_x == x && end_y - 1 > y {
y += 1;
} else if end_y > y && start_x > y {
y += 1;
} else if (end_x > 0 && end_x - 1 > x) && end_y == y {
x += 1;
} else {
break;
}
}
}
pub fn apply_finder_patterns(&self, body: &mut Vec<Cell>, alignment_point: Point<usize>) {
for plot_point in self.plot_spiral(&alignment_point, 6, 0) {
let idx = (plot_point.point.0 * self.size) + plot_point.point.1;
match body.get_mut(idx) {
Some(cell) => {
cell.module_type = CellType::Finder;
cell.color = plot_point.color
},
None => {}
}
}
}
pub fn apply_timer_patterns(&self, body: &mut Vec<Cell>) {
let (mut x, mut y) = (6, self.size - 8);
loop {
if x >= self.size - 7 {
break;
}
let pt: Point<usize> = Point(x, y);
let idx = (pt.0 * self.size) + pt.1;
match body.get_mut(idx) {
Some(cell) => {
match cell.module_type {
CellType::None | CellType::Format => {
let direction = if y > x {
y
} else {
x
};
cell.module_type = CellType::Timing;
if direction % 2 == 0 {
cell.color = Color { r: 0, g: 0, b: 0 };
}
},
_ => {}
}
},
None => {}
}
if y > x {
y -= 1;
} else if y == 7 {
y = 6;
x = 8;
} else {
x += 1;
}
}
}
pub fn get_alignment_points(&self, body: &Vec<Cell>) -> Vec<PlotPoint> {
let mut pts: Vec<usize> = vec![6];
let mut n = 6;
let version_bracket = match self.version {
1 => 0,
2...6 => 1,
7...13 => 2,
14...21 => 3,
22...28 => 4,
29...36 => 5,
37...41 => 6,
_ => 0
};
let modifier = if version_bracket == 1 {
self.size - 13
} else {
(self.size - 12) / version_bracket
};
for _ in 0..version_bracket {
n += modifier;
pts.push(n);
}
let pts: Vec<PlotPoint> = self.get_point_combinations(pts)
.into_iter()
.filter(|pt| {
let idx = (pt.0 * self.size) + pt.1;
let cell_ref = body.get(idx);
if cell_ref.is_none() {
return false
}
let cell = cell_ref.unwrap();
let result = match cell.module_type {
CellType::None => true,
_ => false
};
result
})
.flat_map(|pt| {
self.plot_spiral(&pt, 4, 2)
})
.collect();
pts
}
pub fn get_point_combinations(&self, numbers: Vec<usize>) -> Vec<Point<usize>> {
let mut pairs: Vec<Point<usize>> = vec![];
let xnumbers: Vec<usize> = numbers.iter().cloned().collect();
for n in numbers {
for xn in xnumbers.iter() {
pairs.push(Point(n, *xn));
}
}
pairs
}
pub fn plot_spiral(&self, origin_pt: &Point<usize>, size: usize, diff: usize) -> Vec<PlotPoint> {
let mut plot_points: Vec<PlotPoint> = vec![];
let mut max = size;
let mut depth = 0;
let (mut x, mut y) = (origin_pt.0 - diff, origin_pt.1 - diff);
while max > 1 {
let mut cell_steps = max * 4;
let color = match depth % 2 {
0 => Color { r: 0, g: 0, b: 0 },
_ => Color { r: 255, g: 255, b: 255 },
};
while cell_steps > 0 {
let plot_point = PlotPoint { point: Point(x, y), color: color };
plot_points.push(plot_point);
if cell_steps > 3 * max {
y += 1;
} else if cell_steps > 2 * max {
x += 1;
} else if cell_steps > max {
y -= 1;
} else {
x -= 1;
}
cell_steps -= 1;
}
depth += 1;
max -= 2;
x += 1;
y += 1;
}
plot_points.push(PlotPoint { point: Point(x, y), color: Color { r: 0, g: 0, b: 0 } });
plot_points
}
}