从点数组中查找给定点坐标的索引

Find an index for a given Point coordinate from an array of Points

给定一个 Point 数组和任意 x,y 坐标,找到最接近给定坐标的 _points 的 index

PointD[] _points
//create a list of x,y coordinates:
for (int i = 0; i < _numberOfArcSegments + 1; i++)
{

    double x1 = _orbitEllipseSemiMaj * Math.Sin(angle) - _focalDistance; //we add the focal distance so the focal point is "center"
    double y1 = _orbitEllipseSemiMinor * Math.Cos(angle);

    //rotates the points to allow for the LongditudeOfPeriapsis. 
    double x2 = (x1 * Math.Cos(_orbitAngleRadians)) - (y1 * Math.Sin(_orbitAngleRadians));
    double y2 = (x1 * Math.Sin(_orbitAngleRadians)) + (y1 * Math.Cos(_orbitAngleRadians));
    angle += _segmentArcSweepRadians;
    _points[i] = new PointD() { x = x2, y = y2 };
}

我正在画一个代表轨道的椭圆。我首先在上面创建点数组,然后在绘制它时,我(尝试)找到最接近轨道 body 所在位置的点。

为此,我一直在尝试计算从椭圆中心到 body:

的角度
public void Update()
{    
    //adjust so moons get the right positions (body position - focal point position) 
    Vector4 pos = _bodyPositionDB.AbsolutePosition - _positionDB.AbsolutePosition;   
    //adjust for focal point
    pos.X += _focalDistance; 

    //rotate to the LonditudeOfPeriapsis. 
    double x2 = (pos.X * Math.Cos(-_orbitAngleRadians)) - (pos.Y * Math.Sin(-_orbitAngleRadians));
    double y2 = (pos.X * Math.Sin(-_orbitAngleRadians)) + (pos.Y * Math.Cos(-_orbitAngleRadians));

    _ellipseStartArcAngleRadians = (float)(Math.Atan2(y2, x2));  //Atan2 returns a value between -180 and 180; 
}

然后:

double unAdjustedIndex = (_ellipseStartArcAngleRadians / _segmentArcSweepRadians);
while (unAdjustedIndex < 0)
{
    unAdjustedIndex += (2 * Math.PI);
}
int index = (int)unAdjustedIndex;

椭圆画得很好,(点阵列是正确的,一旦针对屏幕和相机偏移和缩放进行了调整,一切都很好) 但不是从正确的点开始(我正在减少颜色中的 alpha,因此生成的椭圆随着距离 body 的距离越来越远而逐渐消失) 我花了几天时间试图找出我在这里做错了什么,并尝试了十几种不同的方法试图找出我的数学错误的地方,但我没有看到它。

我假设_points应该是一个PointD数组; 这是获得最接近数组的点的最短方法(calcdistance 应该是一个计算欧氏距离的简单函数):

PointD p = _points.OrderBy(p => CalcDistance(p, gievnPoint)).First();