我是rust库Nannou的新手,我正在尝试绘制饼状图。我知道在javascript中你可以通过
来指定椭圆的长度ctx.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
我在南瓯找不到类似的文字:
draw.ellipse().angle(PI);
nannou::draw::primitive::ellipse::Ellipse
不支持section
但是,您可以使用nannou::geom::Ellipse
中的Section
。
下面是一个简单的例子:
use nannou::{
geom::{Ellipse, Tri},
prelude::*,
};
fn main() {
nannou::sketch(view).run()
}
fn view(app: &App, frame: Frame) {
let draw = app.draw();
let win = app.window_rect();
draw.background().color(CORNFLOWERBLUE);
let t = app.time;
let radius = if win.h() < win.w() {
win.w() / 3f32
} else {
win.h() / 3f32
};
let section = Ellipse::new(Rect::from_x_y_w_h(0f32, 0f32, radius, radius), 120f32)
.section(0f32, t.sin() * 2f32 * std::f32::consts::PI);
let triangles = section.trangles();
let mut tris = Vec::new();
for t in triangles {
tris.push(Tri([
[t.0[0][0], t.0[0][1], 0f32],
[t.0[1][0], t.0[1][1], 0f32],
[t.0[2][0], t.0[2][1], 0f32],
]));
}
draw.mesh().tris(tris.into_iter()).color(RED);
draw.to_frame(app, &frame).unwrap();
}