Skip to content
Snippets Groups Projects
Commit 04f93587 authored by Finn Bear's avatar Finn Bear
Browse files

Basic triangle mosaic.

parent 6d023bad
No related branches found
No related tags found
No related merge requests found
...@@ -22,11 +22,17 @@ impl Default for Triangle { ...@@ -22,11 +22,17 @@ impl Default for Triangle {
} }
impl Triangle { impl Triangle {
/// Draws the triangle to the ui.
pub fn show(&self, ui: &mut 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.points.into(),
self.fill, self.fill,
Stroke::new(self.stroke_width, self.stroke), Stroke::new(self.stroke_width, self.stroke),
)); )
} }
} }
use crate::color::set_alpha;
use crate::component::grid::Grid; use crate::component::grid::Grid;
use crate::component::image::{Image, ImagePosition}; use crate::component::image::{Image, ImagePosition};
use crate::component::triangle::Triangle; use crate::component::triangle::Triangle;
use crate::ctx_img; use crate::egui::{TextureHandle, Ui, Vec2};
use crate::egui::{Color32, TextureHandle, Ui, Vec2}; use crate::img;
use crate::slide::Slide; use crate::slide::Slide;
use eframe::egui::style::Margin; 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 { 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 { impl Slide for Mosaic {
...@@ -17,18 +31,6 @@ impl Slide for Mosaic { ...@@ -17,18 +31,6 @@ impl Slide for Mosaic {
// TODO: Finish this slide. // TODO: Finish this slide.
Frame::none().margin(Margin::same(20.0)).show(ui, |ui| { Frame::none().margin(Margin::same(20.0)).show(ui, |ui| {
ui.heading("Generative Image Processing"); 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() Image::default()
...@@ -37,8 +39,9 @@ impl Slide for Mosaic { ...@@ -37,8 +39,9 @@ impl Slide for Mosaic {
.margin(16.0) .margin(16.0)
.show( .show(
ui, ui,
self.squirrel self.texture.get_or_insert_with(|| {
.get_or_insert_with(|| ctx_img!(ui.ctx(), "squirrel0.png")), ui.ctx().load_texture("squirrel0.png", self.image.clone())
}),
); );
Grid { Grid {
...@@ -47,6 +50,38 @@ impl Slide for Mosaic { ...@@ -47,6 +50,38 @@ impl Slide for Mosaic {
offset_x: 300.0, offset_x: 300.0,
..Grid::default() ..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());
});
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment