Convex Shapes 顶点不准确 SFML 生锈

Convex Shapes vertices are not accurate SFML rust

所以我一直在使用 Convex Shape 结构来使用 sfml 的 Rust 绑定创建三角形。

我的代码如下:

let mut shape2 = ConvexShape::new(3);
shape2.set_point(0, Vector2f::new(0.0, 0.0));
shape2.set_point(1, Vector2f::new(0.0, 100.0));
shape2.set_point(2, Vector2f::new(100.0, 0.0));

for point in shape2.points() {
    println!("x:{} y:{}", point.x, point.y);
}

然而,当遍历三角形的点时,有时我会得到这样的输出:

x:0 y:100
x:100 y:0
x:434583.44 y:-0.000000000000000000000000000000000000000047353

我不确定是什么导致了这个问题;但是,我认为这与 f32 溢出有关。 是否有任何解决此问题的方法,或者我在这里做错了什么?

这似乎是 .points() 返回的迭代器中的错误。您可以改为直接通过索引获取(通过 Shape 特征访问):

use sfml::graphics::{ConvexShape, Shape};
use sfml::system::Vector2f;

...

let mut shape2 = ConvexShape::new(3);
shape2.set_point(0, Vector2f::new(0.0, 0.0));
shape2.set_point(1, Vector2f::new(0.0, 100.0));
shape2.set_point(2, Vector2f::new(100.0, 0.0));

for i in 0..shape2.point_count() {
    let point = shape2.point(i);
    println!("x:{} y:{}", point.x, point.y);
}