feat: 实现多边形功能

This commit is contained in:
2026-02-28 18:29:32 +08:00
parent 25c906278c
commit 9337f15da1
2 changed files with 153 additions and 13 deletions

View File

@@ -37,6 +37,20 @@ impl MSColor {
};
}
pub struct Path2D {
points: Vec<Point>,
}
impl Path2D {
pub fn new() -> Self {
Self { points: vec![] }
}
pub fn push(&mut self, point: Point) {
self.points.push(point);
}
}
pub struct MSCanvas {
width: i32,
height: i32,
@@ -50,6 +64,8 @@ pub struct MSCanvas {
color: MSColor,
line_width: i32,
path2d: Path2D,
}
#[allow(unused)]
@@ -62,6 +78,7 @@ impl MSCanvas {
pixels_bak: Vec::new(),
color: MSColor::BLACK,
line_width: 1,
path2d: Path2D::new(),
}
}
@@ -635,6 +652,37 @@ impl MSCanvas {
let points = vec![begin, p1, p2, end];
self.bezier(points);
}
pub fn begin_path(&mut self) {
self.path2d = Path2D::new();
}
pub fn close_path(&mut self) {
let points = &self.path2d.points;
if points.len() < 3 {
return;
}
self.path2d.push(points[0]);
}
pub fn move_to(&mut self, point: Point) {
self.path2d.push(point);
}
pub fn line_to(&mut self, point: Point) {
self.path2d.push(point);
}
pub fn stroke(&mut self) {
let mut points = self.path2d.points.clone();
if points.len() < 2 {
return;
}
// 连线
for i in 0..(points.len() - 1) {
self.draw_line_with_circle_brush(points[i], points[i + 1]);
}
}
}
fn point_muln(point: Point, t: f32) -> Point {