21
Сентябрь
2011
Определения принадлежности точек полигону
Определения принадлежности точек полигону
Как идея: закрашивать полигон цветом, а потом проверять цвет точки - если равна цвету полигона, то принадлежит, если нет - не принадлежит. Option Explicit Private Type COORD X As Long Y As Long End Type Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Const ALTERNATE = 1 ' ALTERNATE and WINDING are Const WINDING = 2 ' constants for FillMode. Const BLACKBRUSH = 4 ' Constant for brush type. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'ПРОВЕРЯЕМ НА ПРИНАДЛЕЖНОСТЬ ПОЛИГОНУ ПО ЦВЕТУ ПИКСЕЛЯ Dim kolor As Long kolor = GetPixel(Me.hdc, X, Y) If kolor <> 0 Then Me.Caption = "точка вне полигона" Else Me.Caption = "точка в полигоне" End Sub Private Sub Form_Paint() 'KPD-Team 1999 Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long Me.Cls ' Number of vertices in polygon. NumCoords = 3 ' Set scalemode to pixels to set up points of triangle. Me.ScaleMode = vbPixels ' Assign values to points. poly(1).X = Form1.ScaleWidth / 2 poly(1).Y = Form1.ScaleHeight / 2 poly(2).X = Form1.ScaleWidth / 4 poly(2).Y = 3 * Form1.ScaleHeight / 4 poly(3).X = 3 * Form1.ScaleWidth / 4 poly(3).Y = 3 * Form1.ScaleHeight / 4 ' Polygon function creates unfilled polygon on screen. ' Remark FillRgn statement to see results. Polygon Me.hdc, poly(1), NumCoords ' Gets stock black brush. hBrush = GetStockObject(BLACKBRUSH) ' Creates region to fill with color. hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE) ' If the creation of the region was successful then color. If hRgn Then FillRgn Me.hdc, hRgn, hBrush DeleteObject hRgn End Sub Private Sub Form_Resize() Form_Paint End Sub