【问题标题】:Convex Shapes vertices are not accurate SFML rust凸形状顶点不准确 SFML 锈
【发布时间】:2021-11-27 02:55:24
【问题描述】:

所以我一直在使用 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 溢出有关。 是否有任何解决此问题的方法,或者我在这里做错了什么?

【问题讨论】:

    标签: rust sfml


    【解决方案1】:

    这似乎是.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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多