编程语言
首页 > 编程语言> > c# – GraphicsPath.IsClockWise()扩展方法

c# – GraphicsPath.IsClockWise()扩展方法

作者:互联网

我需要为GraphicsPath对象创建一个扩展方法,以确定GraphicsPath是顺时针还是逆时针缠绕.

像这样的东西:

    public static bool IsClockwise(GraphicsPath gp)
    {
        bool bClockWise = false;
        PointF[] pts = gp.PathPoints;

        foreach (PointF pt in pts)
        {
            //??
        }

        return bClockWise;
    }

注意:我只是假设我们在这里需要关于GraphicsPath中各点的foreach,但如果有更好的方法,请随时提供.

原因是因为GraphicsPath的FillMode确定相邻GraphicsPath的重叠部分是“填充”(绕组)还是“填充”(备用).但是,仅当相邻的GraphicsPath以相同的方向缠绕时才会出现这种情况.

  

如果两个路径以相反的方向缠绕,即使将FillMode设置为绕组也会导致(不需要的)孔.

有趣的是,GraphicsPath DOES有一个.Reverse()方法,允许人们改变路径的方向(并且这可以解决问题),但是不可能知道什么时候需要的GraphicsPath.在不知道方向的情况下反转的路径.

你能帮忙解决这个扩展方法吗?

解决方法:

我大部分都把它扔到那里因为这个问题引起了我的兴趣,这是我没有专业知识的东西,我想引发讨论.

我拿了Point in Polygon pseudo-code并试图让它适合你的情况.似乎你只对确定是顺时针还是逆时针缠绕的东西感兴趣.这是一个简单的测试和一个非常简单(粗略的边缘)C#实现.

[Test]
public void Test_DetermineWindingDirection()
{

    GraphicsPath path = new GraphicsPath();

    // Set up points collection
    PointF[] pts = new[] {new PointF(10, 60),
                    new PointF(50, 110),
                    new PointF(90, 60)};
    path.AddLines(pts);

    foreach(var point in path.PathPoints)
    {
        Console.WriteLine("X: {0}, Y: {1}",point.X, point.Y);
    }

    WindingDirection windingVal = DetermineWindingDirection(path.PathPoints);
    Console.WriteLine("Winding value: {0}", windingVal);
    Assert.AreEqual(WindingDirection.Clockwise, windingVal);

    path.Reverse();
    foreach(var point in path.PathPoints)
    {
        Console.WriteLine("X: {0}, Y: {1}",point.X, point.Y);
    }

    windingVal = DetermineWindingDirection(path.PathPoints);
    Console.WriteLine("Winding value: {0}", windingVal);
    Assert.AreEqual(WindingDirection.CounterClockWise, windingVal);
}

public enum WindingDirection
{
    Clockwise,
    CounterClockWise
}

public static WindingDirection DetermineWindingDirection(PointF[] polygon)
{
    // find a point in the middle
    float middleX = polygon.Average(p => p.X);
    float middleY = polygon.Average(p => p.Y);
    var pointInPolygon = new PointF(middleX, middleY);
    Console.WriteLine("MiddlePoint = {0}", pointInPolygon);

    double w = 0;
    var points = polygon.Select(point =>
                                    { 
                                        var newPoint = new PointF(point.X - pointInPolygon.X, point.Y - pointInPolygon.Y);
                                        Console.WriteLine("New Point: {0}", newPoint);
                                        return newPoint;
                                    }).ToList();

    for (int i = 0; i < points.Count; i++)
    {
        var secondPoint = i + 1 == points.Count ? 0 : i + 1;
        double X = points[i].X;
        double Xp1 = points[secondPoint].X;
        double Y = points[i].Y;
        double Yp1 = points[secondPoint].Y;

        if (Y * Yp1 < 0)
        {
            double r = X + ((Y * (Xp1 - X)) / (Y - Yp1));
            if (r > 0)
                if (Y < 0)
                    w = w + 1;
                else
                    w = w - 1;
        }
        else if ((Y == 0) && (X > 0))
        {
            if (Yp1 > 0)
                w = w + .5;
            else
                w = w - .5;
        }
        else if ((Yp1 == 0) && (Xp1 > 0))
        {
            if (Y < 0)
                w = w + .5;
            else
                w = w - .5;
        }
    }
    return w > 0 ? WindingDirection.ClockWise : WindingDirection.CounterClockwise;
}

我所提出的不同之处在于,我对你的问题有点具体,就是我计算了所有点的X和Y的平均值,并将其用作“多边形点”值.所以,传递一个点数组,它会在它们中间找到一些东西.

我还假设V i 1应该在到达points数组的边界时环绕

if (i + 1 == points.Count)
    // use points[0] instead

这还没有得到优化,它只是可以回答你的问题.也许你自己已经想出了这个.

这里希望有建设性的批评.

标签:c,algorithm,methods,graphicspath
来源: https://codeday.me/bug/20190606/1189349.html