feat: add rings.rs

This commit is contained in:
Ingolf Wagner 2021-04-16 11:02:11 +02:00
parent 75fc7c0e89
commit 29252c5c03
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
4 changed files with 68 additions and 6 deletions

View file

@ -4,7 +4,6 @@ version = "1.0.0"
authors = ["Ingolf Wagner <contact@ingolf-wagner.de>"] authors = ["Ingolf Wagner <contact@ingolf-wagner.de>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
description = "Convenience library to use geo to create art." description = "Convenience library to use geo to create art."
documentation = "https://github.com/mrVanDalo/polygon-art" documentation = "https://github.com/mrVanDalo/polygon-art"

View file

@ -6,7 +6,15 @@ Convenience library to create art using [geo](https://georust.org/).
* load SVGs * load SVGs
* scaling * scaling
# Asteroids # Binaries
I deliver some binaries to give you an impression and ideas
for your own images.
All binaries created by polygon art have the same
command line interface.
## Asteroids
asteroids is an example binary which renders an asteroids is an example binary which renders an
image inspired by the image inspired by the
@ -14,11 +22,11 @@ image inspired by the
![image](./images/asteroids.png) ![image](./images/asteroids.png)
run `asteroids --help` to get information. ## Rings
All binaries created by polygon art have the same
command line interface.
# How to run examples ![image](./images/rings.png)
# How to run /examples
``` ```
cargo run --example clipping -- --help # run the examples/clipping cargo run --example clipping -- --help # run the examples/clipping

BIN
images/rings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

55
src/bin/rings.rs Normal file
View file

@ -0,0 +1,55 @@
use geo::algorithm::rotate::{Rotate, RotatePoint};
use geo::algorithm::translate::Translate;
use geo::{polygon, Coordinate, MultiPolygon, Point};
use geo_clipper::Clipper;
use polygon_art::{Context, MergeExt, TranslateExt};
use rand::prelude::StdRng;
use rand::Rng;
fn main() {
let mut context = Context::create();
context.translate_center();
let min = f64::min(context.width, context.height);
let number_of_rings = (min / 150.) as i32;
let mut radius = min / 2. - 5.;
for _ in 0..number_of_rings {
let ring_size = context.gen_rng().gen_range(10..(radius as i32 / 5)) as f64;
radius = radius - ring_size;
if radius < 10. {
break;
}
let polygons = generate_ring(&mut context.gen_rng(), radius, ring_size);
radius = radius - ring_size;
context.draw_multipolygon(&polygons);
}
context.render();
}
fn generate_ring(rng: &mut StdRng, radius: f64, ring_size: f64) -> MultiPolygon<f64> {
let main = polygon![
(x: 0.0, y:0.0),
(x: ring_size , y:0.0),
(x: ring_size , y:ring_size ),
(x: 0.0, y:ring_size ),
]
.translate_center()
.rotate(30.)
.translate(0., radius);
let center_point = Point(Coordinate { x: 0., y: 0. });
let parts = rng.gen_range(20..120);
let rotation = 360. / parts as f64;
let direction = if rng.gen_bool(0.5) { -1. } else { 1. };
let fragment = main.difference(
&main.rotate_around_point(direction * rotation, center_point),
100.,
);
let mut polygons: MultiPolygon<f64> = MultiPolygon(Vec::new());
for index in 0..parts {
polygons.merge(&fragment.rotate_around_point(rotation * index as f64, center_point));
}
polygons
}