Example #1
0
        public long GetClosestDegree(Vector2d target)
        {
            if (Points.Length == 1)
            {
                return(0);
            }
            else if (Points.Length <= 0)
            {
                return(0);
            }
            target -= Offset;
            long closestDegree = 0;
            long closestDist   = long.MaxValue;

            for (int i = 0; i < _axes.Length; i++)
            {
                Vector2d axis       = _axes [i];
                Vector2d axisOffset = _points [i];
                long     min        = 0;
                long     max        = (_points [i + 1 < _points.Length ? i + 1 : 0] - axisOffset).Dot(axis);
                long     projection = (target - axisOffset).Dot(axis);
                if (projection < min)
                {
                    projection = min;
                }
                if (projection > max)
                {
                    projection = max;
                }
                if (projection >= min && projection <= max)
                {
                    Vector2d point = axis * projection + axisOffset;
                    long     dist  = point.Distance(target);
                    if (dist < closestDist)
                    {
                        closestDist   = dist;
                        closestDegree = _cachedPointMags[i] + projection;
                    }
                }
                else
                {
                }
            }
            closestDegree = closestDegree.Div(TotalLength);
            return(closestDegree);
        }