Results 1 to 24 of 24

Thread: Draw region and fill the closed regions with a color

Hybrid View

  1. #1
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Draw region and fill the closed regions with a color

    junlo, I gave you some code to draw colored regions in one of your other threats.
    The thing you want is to draw those regions on a picturebox, so you need to convert those coordinates (you have got the code needed).
    You have a different syntax in your file now, the problem how to get the regions surrounded by START and STOP you do request yet in another threat.
    Why don't you use all the help given so far instead of asking nearly the same stuff over and over again.
    As you see there lots of people willing to help!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: Draw region and fill the closed regions with a color

    Code:
    Private Type Point
        Longitude As Long
        Latitude As Long
    End Type
    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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Const ALTERNATE = 1 ' ALTERNATE and WINDING are
    Const WINDING = 2 ' constants for FillMode.
    Dim PicHght As Long 'the height of your picturebox
    Dim PicWdth As Long 'the width of your picturebox
    Dim MapHght As Double 'the height of your map
    Dim MapWdth As Double 'the width of your picturebox
    Dim Map0X As Double 'the maximum X or width of your map
    Dim Map0Y As Double 'the maximum Y or height of your map
    Dim arrDepth(1000, 1000) As Single
    Dim Region2() As Point
    Dim Region10() As Point
    Dim Region20() As Point
    Dim Region30() As Point
    Dim Region50() As Point
    Dim Region100() As Point
    Dim Region2Count As Integer
    Dim Region10Count As Integer
    Dim Region20Count As Integer
    Dim Region30Count As Integer
    Dim Region50Count As Integer
    Dim Region100Count As Integer
    
    Private Sub Form_load()
    Dim filenr As Long
    Dim filename As String
    Dim data As String
    Dim i As Integer
    Dim j As Integer
    Dim Latitude As Single
    Dim Longitude As Single
    Dim Depth As Single
    Dim Changer As Single
    With Picture1
              .AutoRedraw = True
              .ScaleMode = vbPixels
              PicHght = .ScaleHeight
              PicWdth = .ScaleWidth
         End With
         'Map0X = 101.46667
         'Map0Y = 2.85667
         'MapWdth = 99.7 - Map0X
         'MapHght = 4.01666 - Map0Y
            Map0X = 99.7
            Map0Y = 4.01666
            MapWdth = 101.46667 - Map0X
            MapHght = 2.85667 - Map0Y
         
    filenr = FreeFile
    'put your filepath in here!!!!
    'filename = "C:\XXX\depth.txt"
    
    filename = "D:\Testing\ENC Project\Setting\Output.txt"
    
    Open filename For Input As filenr
    Do
    Input #filenr, data
    If Not data = "" Then
        Longitude = Val(Left(data, InStr(1, data, " ") - 1))
        data = Mid(data, InStr(1, data, " ") + 1)
        Latitude = Val(Left(data, InStr(1, data, ";") - 1))
        data = Mid(data, InStr(1, data, ";") + 1)
        Depth = CSng(data)
        If Longitude < Latitude Then
            Changer = Longitude
            Longitude = Latitude
            Latitude = Changer
        End If
        arrDepth(GetX(Longitude), GetY(Latitude)) = Depth
        CreateRegion GetX(Longitude), GetY(Latitude), Depth
        Picture1.PSet (GetX(Longitude), GetY(Latitude))
    End If
    Loop Until EOF(filenr)
    Close filenr
    'fill regions
    FillRegions Region2, UBound(Region2) - 1, vbYellow
    FillRegions Region10, UBound(Region10) - 1, vbGreen
    FillRegions Region20, UBound(Region20) - 1, vbBlue
    FillRegions Region30, UBound(Region30) - 1, vbRed
    FillRegions Region50, UBound(Region50) - 1, vbWhite
    FillRegions Region100, UBound(Region100) - 1, vbBlack
    End Sub
    Private Function GetLatitude(y As Single) As Double
     GetLatitude = Round((y * MapHght / PicHght) + Map0Y, 5)
    End Function
    
    Private Function GetLongitude(x As Single) As Double
     GetLongitude = Round((x * MapWdth / PicWdth) + Map0X, 5)
    End Function
    
    Private Function GetY(Latitude As Single) As Long
     GetY = (Latitude - Map0Y) * PicHght / MapHght
    End Function
    
    Private Function GetX(Longitude As Single) As Long
     GetX = (Longitude - Map0X) * PicWdth / MapWdth
    End Function
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    Picture1.ToolTipText = "x=" & CStr(x) & " y=" & CStr(y) & "Lat=" & CStr(GetLatitude(y)) & " Long=" & CStr(GetLongitude(x)) & " Depth: " & CStr(arrDepth(x, y))
    
    End Sub
    
    Private Sub CreateRegion(Longitude As Long, Latitude As Long, Depth As Single)
    'will add the coordinates to the region of this depth
    'only depth of 2, 10,20,30,50,100 are supported!
    Dim i As Integer
    Select Case Depth
        Case Is = 2
                Region2Count = Region2Count + 1
                ReDim Preserve Region2(Region2Count)
                Region2(Region2Count - 1).Latitude = Latitude
                Region2(Region2Count - 1).Longitude = Longitude
        Case Is = 10
                Region10Count = Region10Count + 1
                ReDim Preserve Region10(Region10Count)
                Region10(Region10Count - 1).Latitude = Latitude
                Region10(Region10Count - 1).Longitude = Longitude
        Case Is = 20
                Region20Count = Region20Count + 1
                ReDim Preserve Region20(Region20Count)
                Region20(Region20Count - 1).Latitude = Latitude
                Region20(Region20Count - 1).Longitude = Longitude
        Case Is = 30
                Region30Count = Region30Count + 1
                ReDim Preserve Region30(Region30Count)
                Region30(Region30Count - 1).Latitude = Latitude
                Region30(Region30Count - 1).Longitude = Longitude
        Case Is = 50
                Region50Count = Region50Count + 1
                ReDim Preserve Region50(Region50Count)
                Region50(Region50Count - 1).Latitude = Latitude
                Region50(Region50Count - 1).Longitude = Longitude
        Case Is = 100
                Region100Count = Region100Count + 1
                ReDim Preserve Region100(Region100Count)
                Region100(Region100Count - 1).Latitude = Latitude
                Region100(Region100Count - 1).Longitude = Longitude
    End Select
    End Sub
    
    Private Sub FillRegions(Region() As Point, NumPoints As Long, color As Long)
    Dim hBrush As Long
    Dim hRgn As Long
    Polygon Picture1.hdc, Region(0), NumPoints
    ' Gets stock black brush.
    'hBrush = GetStockObject(color)
    hBrush = CreateSolidBrush(color)
    ' Creates region to fill with color.
    hRgn = CreatePolygonRgn(Region(0), NumPoints, ALTERNATE)
    ' If the creation of the region was successful then color.
    If hRgn Then FillRgn Picture1.hdc, hRgn, hBrush
    DeleteObject hRgn
    End Sub
    opus, thanks you for your code.
    i not understand some part of the code(i have highlight in red color). Can you explain to me that what consept is taken to generate the coordinates.

    Code:
    Region2Count = Region2Count + 1
                ReDim Preserve Region2(Region2Count)
                Region2(Region2Count - 1).Latitude = Latitude
                Region2(Region2Count - 1).Longitude = Longitude

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: Draw region and fill the closed regions with a color

    Quote Originally Posted by opus
    You have a different syntax in your file now, the problem how to get the regions surrounded by START and STOP you do request yet in another threat.
    for the previous request in other thread:i was asking to change format below
    START 43, L, 133=20000;174=10
    101.32412440 2.97892000
    101.32416420 2.97895800
    STOP
    to became like this
    101.32412440 2.97892000;10
    101.32416420 2.97895800;10

    but this time, i just found that when has the "START 42,A"
    START 42, A, 87=5;88=10
    101.28147000 3.20742000
    101.27951000 3.20788000
    101.27933000 3.20833000
    101.27951000 3.20865000
    101.28088000 3.20978000
    101.28121000 3.21074000
    101.28121000 3.21074000
    101.28147000 3.20742000
    STOP

    A refer as area,it can connect to became a closed area. because the starting point and the end point is same.(highlight in red).
    87=xx represent minimum depth in that area
    88=xxrepresent maximum depth in that area

    my question now is,seen my data is all in the format "start .......stop".then can i used it without changing to other kind of format.
    when meet the "Start 42,A", draw a closed area with a color represent the depth for that area.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width