feat: 实现喷枪功能

This commit is contained in:
2026-02-28 14:32:39 +08:00
parent 3ed3c12f47
commit a9243c498f
4 changed files with 197 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
use iced::Point;
use rand::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MSColor {
@@ -540,7 +541,6 @@ impl MSCanvas {
}
pub fn stroke_rect1(&mut self, p1: Point, p2: Point) {
self.draw_cross_color(p1, MSColor::new(255, 0, 0, 255));
let mut x = p1.x;
let mut y = p1.y;
let mut width = (p2.x - p1.x);
@@ -582,4 +582,21 @@ impl MSCanvas {
self.pixels[index + 3] = 255; // A
}
}
/// 实现喷枪效果的函数
pub fn spray_paint(&mut self, center: Point, radius: i32, density: u32) {
let mut rng = rand::rng();
for _ in 0..density {
// 在给定半径内随机产生偏移量
let offset_x = rng.random_range(-radius..=radius);
let offset_y = rng.random_range(-radius..=radius);
// 确保我们只在圆形区域内绘制,而非整个正方形区域
if (offset_x * offset_x + offset_y * offset_y) <= (radius * radius) {
let point = Point::new(center.x + offset_x as f32, center.y + offset_y as f32);
self.draw_pixel_at(point);
}
}
}
}