0% found this document useful (0 votes)
3 views2 pages

Rust 2D Shape Manipulation Code

The document defines a Rust module for creating 2D shapes, specifically circles and rectangles, using a struct called Shape. It utilizes synchronization primitives like Mutex and Arc to manage the position, rotation, and scale of the shapes safely in a concurrent environment. The Shape struct includes methods for setting and getting these properties, ensuring thread-safe access.

Uploaded by

zzirioss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Rust 2D Shape Manipulation Code

The document defines a Rust module for creating 2D shapes, specifically circles and rectangles, using a struct called Shape. It utilizes synchronization primitives like Mutex and Arc to manage the position, rotation, and scale of the shapes safely in a concurrent environment. The Shape struct includes methods for setting and getting these properties, ensuring thread-safe access.

Uploaded by

zzirioss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

use std::sync::{Mutex, Arc};

use glam::Vec2;

use crate::object::Object;
use crate::draw::Draw;

pub fn circle(r: f32) -> Object<Shape> {


shape_new(Draw{})
}

pub fn rect(w: f32, h: f32) -> Object<Shape> {


shape_new(Draw{})
}

fn shape_new(draw: Draw) -> Object<Shape> {


Object::new(Shape::default(), draw)
}

#[derive(Default)]
pub struct Shape {
// Transform
position: Arc<Mutex<Vec2>>,
rotation: Arc<Mutex<Vec2>>,
scale: Arc<Mutex<Vec2>>,
}

impl Shape {
pub fn position_set(&self, v: Vec2) {
let mut position_lock = [Link]
.lock()
.expect("Failed set position of object2d");
*position_lock = v;
}

pub fn position_get(&self) -> Vec2 {


let position_lock = [Link]
.lock()
.expect("Failed get position of object2d");
position_lock.clone()
}

pub fn rotation_set(&self, v: Vec2) {


let mut rotation_lock = [Link]
.lock()
.expect("Failed set rotation of object2d");
*rotation_lock = v;
}

pub fn rotation_get(&self) -> Vec2 {


let rotation_lock = [Link]
.lock()
.expect("Failed get rotation of object2d");
rotation_lock.clone()
}

pub fn scale_set(&self, v: Vec2) {


let mut scale_lock = [Link]
.lock()
.expect("Failed set scale of object2d");
*scale_lock = v;
}

pub fn scale_get(&self) -> Vec2 {


let scale_lock = [Link]
.lock()
.expect("Failed get scale of object2d");
scale_lock.clone()
}
}

You might also like