From 04f93587533483b159e8c07ee3684cb42a2f8444 Mon Sep 17 00:00:00 2001 From: Finn Bear <finnbearlabs@gmail.com> Date: Sat, 30 Apr 2022 21:58:52 -0700 Subject: [PATCH] Basic triangle mosaic. --- src/component/triangle.rs | 10 ++++-- src/slide/s8_mosaic.rs | 75 ++++++++++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/component/triangle.rs b/src/component/triangle.rs index 05ebcc9..91e2450 100644 --- a/src/component/triangle.rs +++ b/src/component/triangle.rs @@ -22,11 +22,17 @@ impl Default for Triangle { } impl Triangle { + /// Draws the triangle to the ui. pub fn show(&self, ui: &mut Ui) { - ui.painter().add(Shape::convex_polygon( + ui.painter().add(self.shape()); + } + + /// Gets the raw shape. + pub fn shape(&self) -> Shape { + Shape::convex_polygon( self.points.into(), self.fill, Stroke::new(self.stroke_width, self.stroke), - )); + ) } } diff --git a/src/slide/s8_mosaic.rs b/src/slide/s8_mosaic.rs index 6d62308..6499703 100644 --- a/src/slide/s8_mosaic.rs +++ b/src/slide/s8_mosaic.rs @@ -1,15 +1,29 @@ +use crate::color::set_alpha; use crate::component::grid::Grid; use crate::component::image::{Image, ImagePosition}; use crate::component::triangle::Triangle; -use crate::ctx_img; -use crate::egui::{Color32, TextureHandle, Ui, Vec2}; +use crate::egui::{TextureHandle, Ui, Vec2}; +use crate::img; use crate::slide::Slide; use eframe::egui::style::Margin; -use eframe::egui::{Frame, Pos2}; +use eframe::egui::{ColorImage, Frame, Pos2, Shape}; +use eframe::emath::Rot2; +use rand::{thread_rng, Rng}; -#[derive(Default)] pub struct Mosaic { - squirrel: Option<TextureHandle>, + image: ColorImage, + texture: Option<TextureHandle>, + shapes: Vec<Shape>, +} + +impl Default for Mosaic { + fn default() -> Self { + Self { + image: img!("squirrel0.png"), + texture: None, + shapes: Vec::new(), + } + } } impl Slide for Mosaic { @@ -17,18 +31,6 @@ impl Slide for Mosaic { // TODO: Finish this slide. Frame::none().margin(Margin::same(20.0)).show(ui, |ui| { ui.heading("Generative Image Processing"); - - Triangle { - points: [ - Pos2::new(200.0, 150.0), - Pos2::new(500.0, 220.0), - Pos2::new(300.0, 400.0), - ], - stroke_width: 0.0, - fill: Color32::RED, - ..Triangle::default() - } - .show(ui) }); Image::default() @@ -37,8 +39,9 @@ impl Slide for Mosaic { .margin(16.0) .show( ui, - self.squirrel - .get_or_insert_with(|| ctx_img!(ui.ctx(), "squirrel0.png")), + self.texture.get_or_insert_with(|| { + ui.ctx().load_texture("squirrel0.png", self.image.clone()) + }), ); Grid { @@ -47,6 +50,38 @@ impl Slide for Mosaic { offset_x: 300.0, ..Grid::default() } - .show(ui.ctx()); + .show_with_overlays(ui.ctx(), |ui, to_screen| { + ui.ctx().request_repaint(); + + let mut rng = thread_rng(); + + for _ in 0..50 { + let x: f32 = rng.gen(); + let y: f32 = rng.gen(); + let size: f32 = rng.gen::<f32>() * 0.025; + let rot = Rot2::from_angle(rng.gen::<f32>() * std::f32::consts::TAU); + let center = Pos2::new(x, y); + let image_x = + ((x * self.image.width() as f32) as usize).min(self.image.width() - 1); + let image_y = + ((y * self.image.height() as f32) as usize).min(self.image.height() - 1); + + self.shapes.push( + Triangle { + points: [ + to_screen * (center + rot * Vec2::new(-1.0, -1.0) * size), + to_screen * (center + rot * Vec2::new(1.0, -1.0) * size), + to_screen * (center + rot * Vec2::new(-1.0, 1.0) * size), + ], + stroke_width: 0.0, + fill: set_alpha(self.image[(image_x, image_y)], 0.2), + ..Triangle::default() + } + .shape(), + ); + } + + ui.painter().extend(self.shapes.clone()); + }); } } -- GitLab