feat: 实现曲线功能
This commit is contained in:
@@ -599,4 +599,48 @@ impl MSCanvas {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn recursive_bezier(&mut self, control_points: &[Point], t: f32) -> Point {
|
||||
// Implement de Casteljau's algorithm
|
||||
let n = control_points.len();
|
||||
if n == 1 {
|
||||
return control_points[0];
|
||||
}
|
||||
let mut new_points: Vec<Point> = Vec::with_capacity(n - 1);
|
||||
for i in 0..(n - 1) {
|
||||
let p0 = control_points[i];
|
||||
let p1 = control_points[i + 1];
|
||||
let point = point_add(point_muln(p0, 1.0 - t), point_muln(p1, t));
|
||||
new_points.push(point);
|
||||
}
|
||||
self.recursive_bezier(&new_points[..], t)
|
||||
}
|
||||
|
||||
pub fn bezier(&mut self, control_points: Vec<Point>) {
|
||||
// Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's recursive Bezier algorithm.
|
||||
let mut t = 0.0;
|
||||
while t <= 1.0 {
|
||||
let point = self.recursive_bezier(&control_points, t);
|
||||
self.brush_circle(point);
|
||||
t += 0.001;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn quadratic_bezier(&mut self, begin: Point, end: Point, p1: Point) {
|
||||
let points = vec![begin, p1, end];
|
||||
self.bezier(points);
|
||||
}
|
||||
|
||||
pub fn cubic_bezier(&mut self, begin: Point, end: Point, p1: Point, p2: Point) {
|
||||
let points = vec![begin, p1, p2, end];
|
||||
self.bezier(points);
|
||||
}
|
||||
}
|
||||
|
||||
fn point_muln(point: Point, t: f32) -> Point {
|
||||
Point::new(point.x * t, point.y * t)
|
||||
}
|
||||
|
||||
fn point_add(p1: Point, p2: Point) -> Point {
|
||||
Point::new(p1.x + p2.x, p1.y + p2.y)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user