如何将Vector2划分为Vector2阵列?

时间:2014-11-29 21:07:21

标签: c# vector xna-4.0 monogame

今天我对Vector2有一点疑问。我如何通过int数将它们分开,并将结果存储为数组。很容易想象这个任务,例如我们变成了位置和航点。

public class Waypoint
{
  public int WaypointID {get; set;}
  public Vector2 position {get; set;}
}

例如,我们有 StartX:100 StartY:100 ,我们需要成为 EndX:1500 EndY:1500 (此处为2个向量)。 因此,对于这个原因,航路点的总数应该是hmm 3 。良好:

public List<Waypoint> calculateWaypoints(Vector2 start, Vector2 end, int WaypointAmount)
{
 //they should return a list with 3 Waypoints in this cause
 List<Waypoint> points = new List<Waypoint>();
 //
 //what code should be here?
 //
 return points
}

我正在寻找,但很难找到任何好的解决方案。我怎么能解决这个任务?

1 个答案:

答案 0 :(得分:2)

这样的事情怎么样:

// For N waypoints between a start and end, we need N+1 segments

segments = WaypointAmount + 1;
xSeg = (end.x - start.x) / segments;
ySeg = (end.y - start.y) / segments;

// even though we have N+1 segments, adding the last segment would take
// us to the endpoint, not another waypoint, so stop when we have all
// our waypoints
for(var i = 1; i<=waypoints, i++)
{
    points.Add(new WayPoint { 
        WaypointId = i, 
        Vector2 = new Vector2(  
                            x = start.x+(xSeg*i),
                            y = start.y+(ySeg*i)});
}