Region.IsVisible()无法记录(?)[GDI +]

时间:2010-06-28 11:23:24

标签: .net gdi+

我看到Region.IsVisible(矩形)没有像我预期的那样工作 那么,是我谁会期望不应该,或方法不应该做到这一点??!

我有以下情况:

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/TCmxn6Tzn2I/AAAAAAAADRc/GJhbStCvabQ/s800/Capture3.gif alt text http://lh5.ggpht.com/_1TPOP7DzY1E/TCmuVyrgpTI/AAAAAAAADRU/yLNn-jZQDNA/s800/Capture2.gif

以下代码:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Point[] points1 = new Point[] {
        new Point(50, 30),
        new Point(70, 30),
        new Point(40, 40),
        new Point(60, 70),
        new Point(30, 50)
    };

    Point[] points2 = new Point[] {
        new Point(70, 150),
        new Point(50, 110 ),
        new Point(60, 80),
        new Point(90, 80),
        new Point(140, 60)                
    };

    Point[] points3 = new Point[] {
        new Point(100, 10),
        new Point(130, 40)
    };

    GraphicsPath path1 = new GraphicsPath();
    GraphicsPath path2 = new GraphicsPath();
    GraphicsPath path3 = new GraphicsPath();

    path1.AddLines(points1);
    path2.AddLines(points2);
    path3.AddLines(points3);

    e.Graphics.DrawPath(Pens.DarkBlue, path1);
    e.Graphics.DrawPath(Pens.DarkGreen, path2);
    e.Graphics.DrawPath(Pens.DarkOrange, path3);

    Region r1 = new Region(path1);
    Region r2 = new Region(path2);
    Region r3 = new Region(path3);

    // Create the first rectangle and draw it to the screen in blue.
    Rectangle blueRect = new Rectangle(20, 20, 100, 100);
    e.Graphics.DrawRectangle(Pens.Blue, blueRect);

    bool contained;

    // Display the result.                        
    ControlPaint.DrawGrid(e.Graphics, this.ClientRectangle,
        new Size(10, 10), Color.Red);

    contained = r1.IsVisible(blueRect);
    e.Graphics.DrawString("Path blue contained = " + contained.ToString(),
        Font, myBrush, new PointF(20, 160));

    contained = r2.IsVisible(blueRect);
    e.Graphics.DrawString("Path green contained = " + contained.ToString(),
        Font, Brushes.Black, new PointF(20, 180));

    contained = r3.IsVisible(blueRect);
    e.Graphics.DrawString("Path orange contained = " + contained.ToString(),
        Font, Brushes.Black, new PointF(20, 200));
}

此外,不在该区域的路径可能是“可见的”:

Point[] points3 = new Point[] {
  new Point(15, 35),
  new Point(15, 130),
  new Point(60 ,130)
};

编辑:
即使Intersect不适用于第二个L路径:

Point[] points3 = new Point[] {
    new Point(10, 40),
    new Point(10, 130),
    new Point(50 ,130)
};

r3.Intersect(blueRect);
bool contained = !(r1.IsEmpty(e.Graphics)); 
e.Graphics.DrawString("Path orange contained = " + contained.ToString(),
    Font, Brushes.Black, new PointF(20, 200)); // TRUE! instead of desired FALSE

4 个答案:

答案 0 :(得分:3)

从橙色的L形示例中,我发现您误解了代码的作用。

从路径构建区域不会以绘制路径的形式生成区域。从L形橙色路径构建区域不会产生L形的一个像素宽度区域。通过连接形成三角形的两端来闭合路径。然后该区域是该三角形的内部,蓝色矩形显然部分包含在该区域中。

对于单个橙色线的初始示例,结果区域是仅具有两个角的退化多边形 - 仍然看起来像线,并且在与线正交的方向上具有零宽度。因此,该区域的面积为零,并且不包含任何内容(如果区域是闭合集,则区域边界上的点可能除外)。

您实际想要做的是对蓝色矩形的矩形区域执行路径的可见性测试。据我所知,没有内置支持这样做。

答案 1 :(得分:2)

因此,解决此问题的方法是在检查其可见性之前使用... Widen()方法:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Point[] points1 = new Point[] {
                new Point(50, 30),
                new Point(70, 30),
                new Point(40, 40),
                new Point(60, 70),
                new Point(30, 50)
            };

    Point[] points2 = new Point[] {
                new Point(70, 150),
                new Point(50, 110 ),
                new Point(60, 80),
                new Point(90, 80),
                new Point(140, 60)
            };

    Point[] points4 = new Point[] {
        new Point(50, 50),
        new Point(90, 90)
      };

    Point[] points3 = new Point[] {
                new Point(15, 35),
                new Point(15, 130),
                new Point(60 ,130)
            };

    GraphicsPath path1 = new GraphicsPath();
    GraphicsPath path2 = new GraphicsPath();
    GraphicsPath path3 = new GraphicsPath();
    GraphicsPath path4 = new GraphicsPath();

    path1.AddLines(points1);
    path2.AddLines(points2);
    path3.AddLines(points3);
    path4.AddLines(points4);

    e.Graphics.DrawPath(Pens.DarkBlue, path1);
    e.Graphics.DrawPath(Pens.DarkGreen, path2);
    e.Graphics.DrawPath(Pens.DarkRed, path3);
    e.Graphics.DrawPath(Pens.DarkGoldenrod, path4);

    // <<<< HERE >>>>>
    path3.Widen(Pens.DarkRed);
    path4.Widen(Pens.DarkGoldenrod);

    Region r1 = new Region(path1);
    Region r2 = new Region(path2);
    Region r3 = new Region(path3);
    Region r4 = new Region(path4);

    // Create the first rectangle and draw it to the screen in blue.
    Rectangle blueRect = new Rectangle(20, 20, 100, 100);
    e.Graphics.DrawRectangle(Pens.Blue, blueRect);

    bool contained;

    // Display the result.            
    ControlPaint.DrawGrid(e.Graphics, this.ClientRectangle,
         new Size(10, 10), Color.Red);

    contained = r1.IsVisible(blueRect);
    e.Graphics.DrawString("Path blue contained = " + contained.ToString(),
         Font, Brushes.Black, new PointF(20, 160));

    contained = r2.IsVisible(blueRect);
    e.Graphics.DrawString("Path green contained = " + contained.ToString(),
         Font, Brushes.Black, new PointF(20, 180));

    contained = r3.IsVisible(blueRect);
    e.Graphics.DrawString("Path orange contained = " + contained.ToString(),
         Font, Brushes.Black, new PointF(20, 200));

    contained = r4.IsVisible(blueRect);
    e.Graphics.DrawString("Path DarkGoldenrod contained = " + contained.ToString(),
         Font, Brushes.Black, new PointF(20, 220));
}

至于主题标题中的问题:IsVisible的{​​{1}}实际上没有按照MSDN中的说明工作。

Microsoft应在其文档中添加一些精确的注释。

答案 2 :(得分:1)

Region.IsVisible方法可能只检查其组成部分线段的任何端点是否在矩形内。因此,蓝色和绿色线(每个在矩形内有多个段端点)为true,而橙色线(矩形内有0个端点)为false

从技术上讲,您的代码实际上是在尝试确定每个不规则Path是否包含蓝色矩形(而不是相反)。做你正在尝试做的事情的另一种方法(但可能会返回相同的结果)是这样的:

r1.Intersect(blueRect);
e.Graphics.DrawString("Path blue contained = " + 
    (!r1.IsEmpty(e.Graphics)).ToString(), Font, Brushes.Black, 
    new PointF(20, 200));   

答案 3 :(得分:0)

来的比较晚!经过一些实验,单独检查数组中的每个点比检查区域更有意义。额外的好处是你还可以看到整个区域是否在:

Private Function AllWithin(ByVal points() As Point, ByVal r As Region) As Boolean
    ' Checks whether all the points in an array are within a region
    Dim contained As Boolean = True
    For a As Integer = 0 To points.Length - 1
        If Not r.IsVisible(points(a)) Then
            contained = False
            Exit For
        End If
    Next
    Return contained
End Function


Private Function SomeWithin(ByVal points() As Point, ByVal r As Region) As Boolean
    ' Checks whether some of the points in an array are within a region
    Dim contained As Boolean = False
    For a As Integer = 0 To points.Length - 1
        If r.IsVisible(points(a)) Then
            contained = True
            Exit For
        End If
    Next
    Return contained
End Function