Skip to content

Commit

Permalink
Add features to test pattern 16
Browse files Browse the repository at this point in the history
  • Loading branch information
longmathemagician committed Aug 18, 2022
1 parent 5df4eec commit be1da57
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions piet/src/samples/picture_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const SIZE: Size = Size::new(200., 200.);

const RED: Color = Color::rgb8(255, 0, 0);
const BLUE: Color = Color::rgb8(0, 0, 255);
const GREEN: Color = Color::rgb8(0, 255, 0);
const INTERPOLATION_MODE: InterpolationMode = InterpolationMode::NearestNeighbor;
const BORDER_WIDTH: f64 = 4.0;

Expand All @@ -17,13 +18,17 @@ pub fn draw<R: RenderContext>(rc: &mut R) -> Result<(), Error> {

let outer_rect_red = Rect::new(20., 20., 180., 180.);
let inner_rect_blue = outer_rect_red.inset(-BORDER_WIDTH);
let inner_rect_green = Rect::new(25., 25., 150., 50.);

// Draw a box with a red border
// Draw boxes
rc.fill(outer_rect_red, &RED);
rc.fill(inner_rect_blue, &BLUE);
rc.fill(inner_rect_green, &GREEN);

// Cache the box, clear the image and re-draw the box from the cache
let cache = rc.capture_image_area(outer_rect_red).unwrap();
// Cache the image, clear the context and re-draw the box from the cache
let cache = rc
.capture_image_area(Rect::from_origin_size(kurbo::Point::ZERO, SIZE))
.unwrap();
rc.clear(None, Color::BLACK);
rc.draw_image(&cache, outer_rect_red, INTERPOLATION_MODE);

Expand All @@ -38,5 +43,45 @@ pub fn draw<R: RenderContext>(rc: &mut R) -> Result<(), Error> {
rc.draw_image(&cache, bottom_left_corner, INTERPOLATION_MODE);
rc.draw_image(&cache, bottom_right_corner, INTERPOLATION_MODE);

// Capture the top left corner and redrew it inset diagonally towards the center of the image
let corner_tl_rect = Rect::from_origin_size((0., 0.), (50., 50.));
let corner_tl_cache = rc.capture_image_area(corner_tl_rect).unwrap();
let corner_tl_cachedraw_rect = Rect::from_origin_size((50., 50.), (50., 50.));
rc.draw_image(
&corner_tl_cache,
corner_tl_cachedraw_rect,
INTERPOLATION_MODE,
);

// Capture the top right corner and redrew it inset diagonally towards the center of the image
let corner_tr_rect = Rect::from_origin_size((150., 0.), (50., 50.));
let corner_tr_cache = rc.capture_image_area(corner_tr_rect).unwrap();
let corner_tr_cachedraw_rect = Rect::from_origin_size((100., 50.), (50., 50.));
rc.draw_image(
&corner_tr_cache,
corner_tr_cachedraw_rect,
INTERPOLATION_MODE,
);

// Capture the bottom left corner and redrew it inset diagonally towards the center of the image
let corner_bl_rect = Rect::from_origin_size((0., 150.), (50., 50.));
let corner_bl_cache = rc.capture_image_area(corner_bl_rect).unwrap();
let corner_bl_cachedraw_rect = Rect::from_origin_size((50., 100.), (50., 50.));
rc.draw_image(
&corner_bl_cache,
corner_bl_cachedraw_rect,
INTERPOLATION_MODE,
);

// Capture the bottom right corner and redrew it inset diagonally towards the center of the image
let corner_br_rect = Rect::from_origin_size((150., 150.), (50., 50.));
let corner_br_cache = rc.capture_image_area(corner_br_rect).unwrap();
let corner_br_cachedraw_rect = Rect::from_origin_size((100., 100.), (50., 50.));
rc.draw_image(
&corner_br_cache,
corner_br_cachedraw_rect,
INTERPOLATION_MODE,
);

Ok(())
}

0 comments on commit be1da57

Please sign in to comment.