implement the next() function
This commit is contained in:
parent
8ebc270fe7
commit
c1fb6a694e
3 changed files with 17 additions and 11 deletions
|
@ -10,6 +10,7 @@ crate-type = ["cdylib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
js-sys = "0.3.55"
|
js-sys = "0.3.55"
|
||||||
wasm-bindgen = "0.2.78"
|
wasm-bindgen = "0.2.78"
|
||||||
|
num-complex = "0.4.0"
|
||||||
|
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
extern crate num_complex;
|
||||||
|
|
||||||
|
use num_complex::Complex;
|
||||||
use crate::shapes::*;
|
use crate::shapes::*;
|
||||||
|
|
||||||
/// An iterator containing the shapes to display
|
/// An iterator containing the shapes to display
|
||||||
|
@ -26,6 +29,18 @@ impl Iterator for CircleShapes {
|
||||||
if self.position == 0 {
|
if self.position == 0 {
|
||||||
self.position += 1;
|
self.position += 1;
|
||||||
Some(Shape::Circle(self.circle))
|
Some(Shape::Circle(self.circle))
|
||||||
|
} else if self.position < self.modulo {
|
||||||
|
let center = Complex::new(self.circle.center.x, self.circle.center.y);
|
||||||
|
let pi = std::f64::consts::PI;
|
||||||
|
let radius = self.circle.radius;
|
||||||
|
let azimuth_1 = (self.position as f64) * 2. * pi / (self.modulo as f64);
|
||||||
|
let azimuth_2 = (self.position * self.factor) as f64 * 2. * pi / self.modulo as f64;
|
||||||
|
let complex_point_1 = Complex::from_polar(radius, azimuth_1) + center;
|
||||||
|
let complex_point_2 = Complex::from_polar(radius, azimuth_2) + center;
|
||||||
|
let p1 = Point { x: complex_point_1.re, y: complex_point_1.im };
|
||||||
|
let p2 = Point { x: complex_point_2.re, y: complex_point_2.im };
|
||||||
|
self.position += 1;
|
||||||
|
Some(Shape::Line(Line {p1, p2}))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -26,19 +26,9 @@ pub fn start() {
|
||||||
|
|
||||||
context.begin_path();
|
context.begin_path();
|
||||||
|
|
||||||
let iter = CircleShapes::new(10, 2, Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0});
|
let iter = CircleShapes::new(100, 13, Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0});
|
||||||
for shape in iter {
|
for shape in iter {
|
||||||
drawing::draw_shape(shape, &context);
|
drawing::draw_shape(shape, &context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0}, &context);
|
|
||||||
drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 35.0}, &context);
|
|
||||||
drawing::draw_circle(Circle {center: Point {x: 60.0, y: 65.0}, radius: 5.0}, &context);
|
|
||||||
|
|
||||||
drawing::draw_line(Line {p1: Point {x: 0.0, y: 0.0}, p2: Point {x: 150.0, y: 150.0}}, &context);
|
|
||||||
drawing::draw_line(Line {p1: Point {x: 0.0, y: 150.0}, p2: Point {x: 150.0, y: 0.0}}, &context);
|
|
||||||
*/
|
|
||||||
|
|
||||||
context.stroke();
|
context.stroke();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue