Mutating an item inside of nested loops

前端 未结 2 777
鱼传尺愫
鱼传尺愫 2020-12-07 01:08

I\'m trying to write a program to evenly (or as close to possible) distribute points about the surface of a sphere. I\'m trying to achieve this by placing N points randomly

2条回答
  •  北海茫月
    2020-12-07 01:54

    I've found an answer to my own question

    for (point, movement) in points.iter().zip(movements.iter_mut()) {
        *movement = Quaternion::identity();
        for neighbour in &points {
            if neighbour.id == point.id {
                continue;
            }
            let angle = point.pos.angle(&neighbour.pos);
            let axis = point.pos.cross(&neighbour.pos);
            let force = -(1.0/(angle*angle)) * update_amt;
            *movement = (*movement) * Quaternion::angle_axis(angle, axis);
        }
    }
    

    By creating a separate vector to hold the movements, rather than having movement be a trait of the point, I can borrow the points vector twice as immutable, and borrow the movements vector once as mutable.

    I think a C++ mindset still has me thinking too much in terms of classes, but I may still be wrong about this being a ideal way to go about this sort of pattern in rust. If anyone has a problem with this solution, or just knows a better way to go about it, any suggestions would be great.

提交回复
热议问题