introduce Line and Circle

This commit is contained in:
histausse 2022-01-16 13:57:33 +01:00
parent 12f8f86a37
commit 9b2845ca51
Signed by: histausse
GPG key ID: 67486F107F62E9E9
3 changed files with 18 additions and 13 deletions

View file

@ -1,15 +1,15 @@
use std::f64;
use crate::shapes::Point;
use crate::shapes::*;
pub fn draw_circle(center: Point, radius: f64, context: &web_sys::CanvasRenderingContext2d) {
context.move_to(center.x+radius, center.y);
pub fn draw_circle(circle: Circle, context: &web_sys::CanvasRenderingContext2d) {
context.move_to(circle.center.x+circle.radius, circle.center.y);
context
.arc(center.x, center.y, radius, 0.0, f64::consts::PI * 2.0)
.arc(circle.center.x, circle.center.y, circle.radius, 0.0, f64::consts::PI * 2.0)
.unwrap();
}
pub fn draw_line(p1: Point, p2: Point, context: &web_sys::CanvasRenderingContext2d) {
context.move_to(p1.x, p1.y);
pub fn draw_line(line: Line, context: &web_sys::CanvasRenderingContext2d) {
context.move_to(line.p1.x, line.p1.y);
context
.line_to(p2.x, p2.y);
.line_to(line.p2.x, line.p2.y);
}

View file

@ -4,7 +4,7 @@ use wasm_bindgen::JsCast;
mod drawing;
mod shapes;
use shapes::Point;
use shapes::*;
#[wasm_bindgen(start)]
pub fn start() {
@ -24,12 +24,12 @@ pub fn start() {
context.begin_path();
drawing::draw_circle(Point {x: 75.0, y: 75.0}, 70.0, &context);
drawing::draw_circle(Point {x: 75.0, y: 75.0}, 35.0, &context);
drawing::draw_circle(Point {x: 60.0, y: 65.0}, 5.0, &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(Point {x: 0.0, y: 0.0}, Point {x: 150.0, y: 150.0}, &context);
drawing::draw_line(Point {x: 0.0, y: 150.0}, Point {x: 150.0, y: 0.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();
}

View file

@ -7,3 +7,8 @@ pub struct Circle {
pub center: Point,
pub radius: f64,
}
pub struct Line {
pub p1: Point,
pub p2: Point,
}