MCPcopy Index your code
hub / github.com/endbasic/endbasic / clip_x2y2

Method clip_x2y2

std/src/gfx/lcd/buffered/mod.rs:159–179  ·  view source on GitHub ↗

Given a top-left `xy` coordinate, adds the user-supplied `size` to it and clamps the result to the LCD space.

(&self, xy: PixelsXY, size: SizeInPixels)

Source from the content-addressed store, hash-verified

157 /// Given a top-left `xy` coordinate, adds the user-supplied `size` to it and clamps the result
158 /// to the LCD space.
159 fn clip_x2y2(&self, xy: PixelsXY, size: SizeInPixels) -> Option<LcdXY> {
160 fn clamp(value: i16, delta: u16, max: usize) -> Option<usize> {
161 let value = i32::from(value);
162 let delta = i32::from(delta);
163
164 let value = value + delta;
165 if value < 0 {
166 None
167 } else {
168 let value = usize::try_from(value).expect("Positive value must fit");
169 if value > max { Some(max) } else { Some(value) }
170 }
171 }
172
173 let x = clamp(xy.x, size.width - 1, self.size_pixels.width - 1);
174 let y = clamp(xy.y, size.height - 1, self.size_pixels.height - 1);
175 match (x, y) {
176 (Some(x), Some(y)) => Some(LcdXY { x, y }),
177 _ => None,
178 }
179 }
180
181 /// Make sure that the coordinates are within the LCD space.
182 ///

Callers 3

read_pixelsMethod · 0.80
put_pixelsMethod · 0.80
draw_rect_filledMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected