Inquiridor
ponto dentro de um poligono

Pergunta
-
olá
criei um poligono
agora precisava criar uma rotina que ao clicar dentro do painel ele identifica se esta ou nao dentro do poligono desenhado
Public Class Form2 Private mPoints As ArrayList = New ArrayList() Dim mPen As Pen = New Pen(Color.DarkBlue) Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue) Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown mPoints.Add(New Point(e.X, e.Y)) Panel1.Invalidate() ' refresh panel End Sub Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint Dim graphicsObject As Graphics = e.Graphics If mPoints.Count < 1 Then Return End If Dim pointArray() As Point = mPoints.ToArray(mPoints(0).GetType()) If polygonRadio.Checked Then graphicsObject.DrawPolygon(mPen, pointArray) ElseIf lineRadio.Checked Then graphicsObject.DrawLines(mPen, pointArray) ElseIf filledPolygonRadio.Checked Then graphicsObject.FillPolygon(mBrush, pointArray) End If End Sub Private Sub btnLimpar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpar.Click mPoints.Clear() Panel1.Invalidate() End Sub Private Sub btnNovaCor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNovaCor.Click Dim colorBox As ColorDialog = New ColorDialog() Dim result As DialogResult = colorBox.ShowDialog() If result = DialogResult.Cancel Then Return End If mPen.Color = colorBox.Color mBrush.Color = colorBox.Color Panel1.Invalidate() End Sub End Class
Bruno Gaiola Domus & Lepton Des. Software
Todas as Respostas
-
Eu acredito que não seja algo difícil.
Basta você pegar a posição x e y do EventArgs do evento click e verificar se esta dentro do polígono.
Eu pesquisei e achei essa solução interessante (http://stackoverflow.com/questions/4243042/c-sharp-point-in-polygon#4243079), mas caso você não consiga usar ou não seja adequada para você basta pesquisar por Point In Polygon, que você vai encontra varias soluções.
- Sugerido como Resposta Jonatas Wesley Gonçalves segunda-feira, 11 de novembro de 2013 11:13
-
Entao,
ela funcionou bem em vb, porem estou tendo um problema com o metodo .Count do polygon
como posso resolver isso?
Error 1 'System.Array' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assemblypublic static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint) { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; }
Bruno Gaiola Domus & Lepton Des. Software
-
Altere o Count() para Length, acho que isso resolve seu problema.
- Sugerido como Resposta Jonatas Wesley Gonçalves segunda-feira, 11 de novembro de 2013 11:13