This commit is contained in:
ntr 2019-09-14 18:54:59 +10:00
parent de088b73c5
commit fcd7a66880
2 changed files with 68 additions and 43 deletions

View File

@ -84,7 +84,7 @@ pub fn invader_write(id: Uuid) -> Result<Uuid, Error> {
Ok(id) Ok(id)
} }
enum ConstructShape { enum ConstructShapes {
Square, Square,
Triangle, Triangle,
Circle, Circle,
@ -96,13 +96,13 @@ enum ConstructShape {
} }
// default ? shape // default ? shape
pub fn construct(id: Uuid) -> Result<Uuid, Error> { pub fn shapes_write(id: Uuid) -> Result<Uuid, Error> {
let mut rng = thread_rng(); let mut rng = thread_rng();
let mut svg = Vec::new(); let mut svg = Vec::new();
// distribution for lightness // distribution for lightness
// bellcurve around 75% // bellcurve around 75%
let l_dist = Normal::new(50.0, 10.0); let l_dist = Normal::new(70.0, 10.0);
let s_dist = Normal::new(25.0, 10.0); let s_dist = Normal::new(25.0, 10.0);
// 8 6 or 4 points in shape // 8 6 or 4 points in shape
@ -112,60 +112,81 @@ pub fn construct(id: Uuid) -> Result<Uuid, Error> {
// add up to 100 for % // add up to 100 for %
let shapes = [ let shapes = [
(ConstructShape::Square, 100), (ConstructShapes::Square, 10),
// (ConstructShape::Triangle, 10), (ConstructShapes::Triangle, 10),
// (ConstructShape::Circle, 10), (ConstructShapes::Circle, 10),
// (ConstructShape::Line, 10), (ConstructShapes::Line, 10),
// (ConstructShape::V, 10), (ConstructShapes::V, 10),
// (ConstructShape::Tri, 3), // (ConstructShapes::Tri, 3),
// (ConstructShape::Plus, 5), // (ConstructShapes::Plus, 5),
// (ConstructShape::Blank, 1), (ConstructShapes::Blank, 1),
]; ];
let shape_dist = WeightedIndex::new(shapes.iter().map(|v| v.1))?; let shape_dist = WeightedIndex::new(shapes.iter().map(|v| v.1))?;
let n_shapes = rng.gen_range(2, 10); let n_shapes = rng.gen_range(4, 5);
let n_shapes = 4;
write!(&mut svg, "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='-500 -500 1000 1000' width='1000' height='1000'><g>")?; write!(&mut svg, "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='-500 -500 1000 1000' width='1000' height='1000'><g>")?;
for i in 0..n_shapes - 1 { for i in 0..n_shapes {
let h = rng.gen_range(0, 360); let h = rng.gen_range(0, 360);
let s = s_dist.sample(&mut rng) as usize; let s = s_dist.sample(&mut rng) as usize;
let l = l_dist.sample(&mut rng) as usize; let l = l_dist.sample(&mut rng) as usize;
let colour = format!("hsl({:}, {:}%, {:}%)", h, s, l); let colour = format!("hsl({:}, {:}%, {:}%)", h, s, l);
let fraction: f64 = (1.0 / n_shapes as f64) * i as f64; let scalar = rng.gen_range(50.0, 200.0);
let angle: f64 = (fraction * 180.0) / f64::consts::PI; let rotation = rng.gen_range(0, 180);
let angle: f64 = i as f64 * (1.0 / n_shapes as f64) * f64::consts::PI;
let x_translate = angle.cos() * scalar;
let y_translate = angle.sin() * scalar;
match shapes[shape_dist.sample(&mut rng)].0 { match shapes[shape_dist.sample(&mut rng)].0 {
ConstructShape::Square => { ConstructShapes::Square => {
let size = rng.gen_range(5, 50); let size = rng.gen_range(20.0, 50.0);
let distance = rng.gen_range(50, 200); write!(&mut svg, "<rect fill=\"{fill}\" x=\"-{x}\" y=\"-{y}\" width=\"{width}\" height=\"{height}\" transform=\"translate({x_t}, {y_t}) rotate({rotation})\" />",
let rotation = rng.gen_range(0, 180); fill = colour, x = size / 2.0, y = size / 2.0, width = size, height = size, x_t = x_translate, y_t = y_translate, rotation = rotation)?;
write!(&mut svg, "<rect fill=\"{}\" x=\"-{}\" y=\"{}\" width=\"{}\" height=\"{}\" transform=\"rotate({}) translate(0, {}) rotate({})\" />", write!(&mut svg, "<rect fill=\"{fill}\" x=\"-{x}\" y=\"-{y}\" width=\"{width}\" height=\"{height}\" transform=\"translate({x_t}, {y_t}) rotate({rotation})\" />",
colour, size / 2, size / 2, size, size, rotation, distance, angle)?; fill = colour, x = size / 2.0, y = size / 2.0, width = size, height = size, x_t = -x_translate, y_t = -y_translate, rotation = rotation)?;
write!(&mut svg, "<rect fill=\"{}\" x=\"-{}\" y=\"{}\" width=\"{}\" height=\"{}\" transform=\"rotate({}) translate(0, {}) rotate({})\" />",
colour, size / 2, size / 2, size, size, rotation, distance, angle + 180.0)?;
}, },
ConstructShape::Triangle => { ConstructShapes::Triangle => {
let h = rng.gen_range(20.0, 50.0);
let b = rng.gen_range(20.0, 50.0);
write!(&mut svg, "<polygon fill=\"{fill}\" x=\"{x}\" y=\"{y}\" points=\"{x0} {y0}, {x1} {y1}, {x2} {y2}\" transform=\"translate({x_translate}, {y_translate}) rotate({rotation})\" />",
fill = colour, x = -b / 2.0, y = h / 2.0, x0 = -b / 2.0, y0 = -h / 2.0, x1 = 0, y1 = b / 2.0, x2 = b / 2.0, y2 = -h / 2.0, rotation = rotation, x_translate = x_translate, y_translate = y_translate)?;
write!(&mut svg, "<polygon fill=\"{fill}\" x=\"{x}\" y=\"{y}\" points=\"{x0} {y0}, {x1} {y1}, {x2} {y2}\" transform=\"translate({x_translate}, {y_translate}) rotate({rotation})\" />",
fill = colour, x = -b / 2.0, y = h / 2.0, x0 = -b / 2.0, y0 = -h / 2.0, x1 = 0, y1 = b / 2.0, x2 = b / 2.0, y2 = -h / 2.0, rotation = rotation + 180, x_translate = -x_translate, y_translate = -y_translate)?;
},
ConstructShapes::Circle => {
let r = rng.gen_range(10.0, 20.0);
write!(&mut svg, "<ellipse fill=\"{fill}\" cx=\"{x}\" cy=\"{y}\" rx=\"{r}\" ry=\"{r}\"/>",
fill = colour, r = r, x = x_translate, y = y_translate)?;
write!(&mut svg, "<ellipse fill=\"{fill}\" cx=\"{x}\" cy=\"{y}\" rx=\"{r}\" ry=\"{r}\"/>",
fill = colour, r = r, x = -x_translate, y = -y_translate)?;
},
ConstructShapes::Line => {
let width = rng.gen_range(2.0, 8.0);
let height = rng.gen_range(20.0, 50.0);
write!(&mut svg, "<rect fill=\"{fill}\" x=\"-{x}\" y=\"-{y}\" width=\"{width}\" height=\"{height}\" transform=\"translate({x_t}, {y_t}) rotate({rotation})\" />",
fill = colour, x = width / 2.0, y = height / 2.0, width = width, height = height, x_t = x_translate, y_t = y_translate, rotation = rotation)?;
write!(&mut svg, "<rect fill=\"{fill}\" x=\"-{x}\" y=\"-{y}\" width=\"{width}\" height=\"{height}\" transform=\"translate({x_t}, {y_t}) rotate({rotation})\" />",
fill = colour, x = width / 2.0, y = height / 2.0, width = width, height = height, x_t = -x_translate, y_t = -y_translate, rotation = rotation)?;
},
ConstructShapes::V => {
let h = rng.gen_range(20.0, 50.0);
let b = rng.gen_range(20.0, 50.0);
let width = rng.gen_range(2.0, 8.0);
write!(&mut svg, "<polyline fill=\"none\" stroke=\"{fill}\" stroke-width=\"{width}\" x=\"{x}\" y=\"{y}\" points=\"{x0} {y0}, {x1} {y1}, {x2} {y2}\" transform=\"translate({x_translate}, {y_translate}) rotate({rotation})\" />",
fill = colour, width = width, x = -b / 2.0, y = h / 2.0, x0 = -b / 2.0, y0 = -h / 2.0, x1 = 0, y1 = b / 2.0, x2 = b / 2.0, y2 = -h / 2.0, rotation = rotation, x_translate = x_translate, y_translate = y_translate)?;
write!(&mut svg, "<polyline fill=\"none\" stroke=\"{fill}\" stroke-width=\"{width}\" x=\"{x}\" y=\"{y}\" points=\"{x0} {y0}, {x1} {y1}, {x2} {y2}\" transform=\"translate({x_translate}, {y_translate}) rotate({rotation})\" />",
fill = colour, width = width, x = -b / 2.0, y = h / 2.0, x0 = -b / 2.0, y0 = -h / 2.0, x1 = 0, y1 = b / 2.0, x2 = b / 2.0, y2 = -h / 2.0, rotation = rotation + 180, x_translate = -x_translate, y_translate = -y_translate)?;
},
ConstructShapes::Tri => {
}, },
ConstructShape::Circle => { ConstructShapes::Plus => {
},
ConstructShape::Line => {
},
ConstructShape::V => {
},
ConstructShape::Tri => {
},
ConstructShape::Plus => {
},
ConstructShape::Blank => {
}, },
ConstructShapes::Blank => (),
} }
} }
@ -214,9 +235,9 @@ mod tests {
// } // }
#[test] #[test]
fn construct_img_test() { fn shapes_img_test() {
for i in 0..100 { for i in 0..100 {
construct(Uuid::new_v4()).unwrap(); shapes_write(Uuid::new_v4()).unwrap();
} }
} }
} }

View File

@ -14,8 +14,9 @@ use construct::{Construct, construct_select, construct_write, construct_spawn};
use names::{name as generate_name}; use names::{name as generate_name};
use img; use img;
pub const FREE_MTX: [MtxVariant; 1] = [ pub const FREE_MTX: [MtxVariant; 2] = [
MtxVariant::Rename, MtxVariant::Rename,
MtxVariant::Shapes,
]; ];
pub const SHOP_LISTINGS: [Listing; 2] = [ pub const SHOP_LISTINGS: [Listing; 2] = [
@ -44,6 +45,7 @@ pub enum MtxVariant {
Rename, Rename,
Molecular, Molecular,
Invader, Invader,
Shapes,
} }
impl MtxVariant { impl MtxVariant {
@ -152,9 +154,11 @@ pub fn apply(tx: &mut Transaction, account: &Account, variant: MtxVariant, const
MtxVariant::Rename => construct.new_name(name), MtxVariant::Rename => construct.new_name(name),
_ => construct.new_img(), _ => construct.new_img(),
}; };
match mtx.variant { match mtx.variant {
MtxVariant::Invader => img::invader_write(construct.img)?, MtxVariant::Invader => img::invader_write(construct.img)?,
MtxVariant::Molecular => img::molecular_write(construct.img)?, MtxVariant::Molecular => img::molecular_write(construct.img)?,
MtxVariant::Shapes => img::shapes_write(construct.img)?,
_ => construct.img, _ => construct.img,
}; };