本文介绍了用孔填充多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个fill来填充使用点列表创建的多边形的内部,但能够删除它的洞。



我的老代码:

  Private Sub DrawSomething(ByVal points as List(of Point),_ 
ByVal myBrush As System.Drawing .Brush,_
ByVal myGraphics As System.Drawing.Graphics)
myGraphics.FillPolygon(myBrush,points)
End Sub

它只是填充由列表中点的轮廓创建的多边形。



如何填充多边形,但排除它的孔(我知道它在里面,我测试过):

pre $ Private Sub DrawSomething(ByVal points作为List(of Point),_
ByVal holes作为List(of Point(of Point)),_
ByVal myBrush As System.Drawing.Brush,_
ByVal myGraphics As System.Drawing .Graphics)

'填充由点创建的轮廓,不包括孔创建的轮廓
End Sub

是th我可以使用的东西,已经创建了吗?我能以某种方式绘制原始多边形并移除这些孔吗?什么是最好的方法?



我试过了什么 - 例如:我做了以下事情:

<$ p Private Sub DrawSomething(ByVal指定为List(of Point),_
ByVal holes作为List(of List(of Point)),_
ByVal myBrush As System。 Drawing.Brush,_
ByVal myGraphics As System.Drawing.Graphics)

Dim myGraphicsPath As Drawing2D.GraphicsPath =新建Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
myGraphicsPath。 AddLines(points)
Dim myRegion As System.Drawing.Region = New System.Drawing.Region(myGraphicsPath)
Dim otherGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
ForEach otherPoints作为洞中的列表(点)
otherGraphicsPath.AddLines(otherPoints)
下一个
myRegion.Exclude(otherGraphicsPath)
myGraphics.FillRegion(myBrush,myRegion)

End Sub

这并不坏......它排除旅店呃多边形,但它也画出轮廓之间的一片空白。所以,我猜这是行不通的。



谢谢。

编辑:添加图片:



轮廓以列表的形式给出的点(点),孔作为列表(孔)的列表。右边的图片对我得到的线条有粗略的描述(即使这些孔和轮廓没有共同点) - 线条随着我移动图像而改变。


  for each otherPoints as List(of Point)in hole 
otherGraphicsPath.StartFigure()
otherGraphicsPath.AddLines(otherPoints)
otherGraphicsPath.CloseFigure()
Next

如果没有,我认为所有的对象都是相互连接的。


I want to create a "fill" that fills the inside of a polygon, created using a point list, but be able to remove its holes.

My old code:

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)
  myGraphics.FillPolygon(myBrush, points)
End Sub

It simply fills a polygon created by the contour of the points in the list.

How can I fill the polygon, but exclude the holes in it (which I know are inside, I have tested):

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal holes as List(of List(of Point)), _
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)

' fill the contour created by points, excluding the contours created by holes
End Sub

Is there something I can use, that has already been created ? Can I somehow draw the original polygon and remove the holes ? What would be the best approach ?

What have I tried - example: I have done the following:

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal holes as List(of List(of Point)), _
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)

  Dim myGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
  myGraphicsPath.AddLines(points)
  Dim myRegion As System.Drawing.Region = New System.Drawing.Region(myGraphicsPath)
  Dim otherGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
  ForEach otherPoints as List(of Point) in holes
    otherGraphicsPath.AddLines(otherPoints)
  Next
  myRegion.Exclude(otherGraphicsPath)
  myGraphics.FillRegion(myBrush, myRegion)

End Sub

This is not so bad... it does exclude the inner polygons, but it also draws a swath of "empty" between the contours. So, I guess it doesn't work.

Thank you.

Edit: Adding picture:

The contour is given as a list of points ("points"), the holes as a list of lists ("holes"). The picture on the right has a rough drawing of the swaths of lines that I get (even though the holes and the contours have no points in common) - the lines change as I move the image.

解决方案

Try using StartFigure and CloseFigure on your GraphicPath objects:

For Each otherPoints as List(of Point) in holes
  otherGraphicsPath.StartFigure()
  otherGraphicsPath.AddLines(otherPoints)
  otherGraphicsPath.CloseFigure()
Next

Without that, I think all of your objects are connecting to each other.

这篇关于用孔填充多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:22