1pub fn generate_grid_commands(
17 width: f32,
18 height: f32,
19 zoom: f32,
20 pan_x: f32,
21 pan_y: f32,
22 spacing: f32,
23) -> String {
24 let effective_spacing = spacing * zoom;
25
26 if effective_spacing < 4.0 {
28 return String::new();
29 }
30
31 let offset_x = pan_x.rem_euclid(effective_spacing);
33 let offset_y = pan_y.rem_euclid(effective_spacing);
34
35 let mut commands = String::with_capacity(10000);
36
37 let mut x = offset_x;
39 while x < width + effective_spacing {
40 if !commands.is_empty() {
41 commands.push(' ');
42 }
43 commands.push_str(&format!("M {} 0 L {} {}", x, x, height));
44 x += effective_spacing;
45 }
46
47 let mut y = offset_y;
49 while y < height + effective_spacing {
50 commands.push(' ');
51 commands.push_str(&format!("M 0 {} L {} {}", y, width, y));
52 y += effective_spacing;
53 }
54
55 commands
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
67 fn test_grid_commands() {
68 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 24.0);
69 assert!(commands.contains("M 0 0 L 0 100")); assert!(commands.contains("M 24 0 L 24 100")); }
72
73 #[test]
74 fn test_grid_commands_format() {
75 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 50.0);
76 assert!(commands.contains("L 0 100")); assert!(commands.contains("L 100")); }
81
82 #[test]
83 fn test_grid_contains_horizontal_lines() {
84 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
85 assert!(commands.contains("M 0 0 L 100 0")); assert!(commands.contains("M 0 25 L 100 25")); }
89
90 #[test]
95 fn test_grid_commands_zoom_affects_spacing() {
96 let commands1 = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
97 let commands2 = generate_grid_commands(100.0, 100.0, 2.0, 0.0, 0.0, 20.0);
98
99 let count1 = commands1.matches("M ").count();
102 let count2 = commands2.matches("M ").count();
103
104 assert!(count1 > count2);
106 }
107
108 #[test]
109 fn test_grid_commands_very_small_zoom() {
110 let commands = generate_grid_commands(100.0, 100.0, 0.1, 0.0, 0.0, 20.0);
112 assert!(commands.is_empty());
114 }
115
116 #[test]
117 fn test_grid_commands_at_spacing_threshold() {
118 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 4.0);
121 assert!(!commands.is_empty()); let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 3.9);
125 assert!(commands.is_empty()); let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 4.1);
129 assert!(!commands.is_empty());
130 }
131
132 #[test]
137 fn test_grid_commands_pan_offsets_lines() {
138 let commands_no_pan = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
139 let commands_with_pan = generate_grid_commands(100.0, 100.0, 1.0, 10.0, 0.0, 20.0);
140
141 assert_ne!(commands_no_pan, commands_with_pan);
143 }
144
145 #[test]
146 fn test_grid_commands_pan_wraps_with_modulo() {
147 let commands1 = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
149 let commands2 = generate_grid_commands(100.0, 100.0, 1.0, 20.0, 0.0, 20.0);
150
151 assert_eq!(commands1, commands2);
152 }
153
154 #[test]
155 fn test_grid_commands_negative_pan() {
156 let commands = generate_grid_commands(100.0, 100.0, 1.0, -10.0, -10.0, 20.0);
158 assert!(!commands.is_empty());
160 assert!(commands.contains("M "));
161 assert!(commands.contains(" L "));
162 }
163
164 #[test]
169 fn test_grid_commands_zero_dimensions() {
170 let commands = generate_grid_commands(0.0, 0.0, 1.0, 0.0, 0.0, 20.0);
171 assert!(!commands.is_empty()); }
175
176 #[test]
177 fn test_grid_commands_very_small_spacing() {
178 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 3.0);
180 assert!(commands.is_empty());
182 }
183
184 #[test]
185 fn test_grid_commands_large_canvas() {
186 let commands = generate_grid_commands(1000.0, 1000.0, 1.0, 0.0, 0.0, 50.0);
187 assert!(!commands.is_empty());
189 let line_count = commands.matches("M ").count();
191 assert!(line_count >= 30);
193 }
194
195 #[test]
196 fn test_grid_commands_spacing_larger_than_canvas() {
197 let commands = generate_grid_commands(50.0, 50.0, 1.0, 0.0, 0.0, 100.0);
198 assert!(!commands.is_empty());
200 }
201
202 #[test]
203 fn test_grid_commands_non_divisible_spacing() {
204 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 33.0);
206 assert!(!commands.is_empty());
207 }
209
210 #[test]
215 fn test_grid_commands_valid_svg_format() {
216 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
217
218 for part in commands.split(' ').collect::<Vec<_>>().chunks(6) {
220 if part.len() >= 6 {
221 assert!(part[0] == "M" || part[0].parse::<f32>().is_ok());
222 }
223 }
224 }
225
226 #[test]
227 fn test_grid_commands_no_trailing_space() {
228 let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
229 assert!(!commands.ends_with(' '));
230 }
231}