如果订购点,则冻结图形

时间:2015-09-28 12:57:31

标签: c# comparator

我正在尝试用4点绘制多边形。 起初我点击了4个点并绘制了一个多边形。 以前版本的构造函数是这样的:

public Polygon(Point a1, Point b1, Point c1, Point d1, Pen myPen)
{
    this.a = a1;
    this.b = b1;
    this.c = c1;
    this.d = d1;
    this.pen = myPen;
}

然后我决定顺时针点顺序并写了一个自定义比较器。

public Polygon(Point a1, Point b1, Point c1, Point d1, Pen myPen)
{
    List<Point> pointsOfList = new List<Point>() { a1, b1, c1, d1 };
    int averageX = Convert.ToInt32(pointsOfList.Average(arg1 => arg1.X));
    int averageY = Convert.ToInt32(pointsOfList.Average(arg2 => arg2.Y));
    pointsOfList = pointsOfList.OrderBy(m => m, new Clockwise(new Point(averageX, averageY))).ToList();
    this.a = pointsOfList[0];
    this.b = pointsOfList[1];
    this.c = pointsOfList[2];
    this.d = pointsOfList[3];
    this.pen = myPen;
}
public class Clockwise : IComparer<Point>
{
    public Point refPoint { get; set; }
    public Clockwise(Point inpPoint)
    {
        refPoint = inpPoint;
    }

    public int Compare(Point pointA, Point pointB)
    {
        //  Variables to Store the atans
        double aTanA, aTanB;

        //  Fetch the atans
        aTanA = Math.Atan2(pointA.Y - refPoint.Y, pointA.X - refPoint.X);
        aTanB = Math.Atan2(pointB.Y - refPoint.Y, pointB.X - refPoint.X);

        //  Determine next point in Clockwise rotation
        if (aTanA < aTanB) return 1;
        else if (aTanB < aTanA) return -1;
        return 0;
    }
}

之后我的程序开始冻结。我单击多边形的点,应用程序占用100%的CPU使用率,没有任何反应。如果我没有订单回到上一个构造函数 - 一切都很好。如果我在调试模式下跟踪程序,一切都很好。

0 个答案:

没有答案