Euclidean grid distances and heightmaps (Post #6)

This one is pretty much the base of all distance calculations, just thought I'll throw in some quick examples of it.

So what is Euclidean distance applied to our grid? 

C^2 = square root of (B^2+A^2) (Pythagorean theorem)

 

And in the above image that would translate in code to the following

So let's create an array that will hold the distance for all grid points based on a Point(X,Y)

    Public MidX As Integer = 12
    Public MidY As Integer = 12
    Public MidSwitch As Integer
    Public EuclDis(MaxGridX, MaxGridY)
    Public Sub EuclideanDistanceColorize(ByVal Xs As Integer, ByVal Ys As Integer)
        Dim X As Integer
        Dim Y As Integer

        For X = 0 To MaxGridX
            For Y = 0 To MaxGridY
                EuclDis(X, Y) = Math.Sqrt((X - Xs) ^ 2 + (Y - Ys) ^ 2)
            Next
        Next

    End Sub

And change our SoftRender("DRAW") calls to the following

For X = 0 To MaxGridX
                For Y = 0 To MaxGridY
                    QRect = New Rectangle(GridWorld(X, Y).X, GridWorld(X, Y).Y, GridWidth, GridWidth)
                    Dim NewBrush As Brush = New SolidBrush(Color.FromArgb(255, 4 * EuclDis(X, Y), 4 * EuclDis(X, Y), 4 * EuclDis(X, Y)))
                    Gs.FillRectangle(NewBrush, QRect)

                    Gs.DrawRectangle(Pens.Black, QRect)
                Next
            Next

And lastly the animation sequencing in a sub that's called by our timer or inside our SoftRender("DRAW")

 If MidX < 20 And MidSwitch = 0 Then
            MidX += 1
        ElseIf MidX = 20 And MidSwitch = 0 Then
            MidSwitch = 1
        ElseIf MidX > 10 And MidSwitch = 1 Then
            MidX -= 1
        Else
            MidSwitch = 0
        End If
        EuclideanDistanceColorize(MidX, MidY)

The above should give us the following render

EuclDis(X, Y) = Math.Sqrt((X - Xs) ^ 2 + (Y - Ys) ^ 2)


So we see that the furthest a grid cell is from our designated point the bigger the distance and the brighter the color will be. (Color.FromArgb(255, 4 * EuclDis(X, Y), 4 * EuclDis(X, Y), 4 * EuclDis(X, Y)))

Now with Euclidean distances we could create a Heightmap generation algorithm which would work like this (Picking a height for a point and subtracting the adjacent tile distance from the point's height)


        Public Sub EuclideanHeightMap()
        Dim X, Y, Z As Integer
        Dim PointX, PointY As Integer
        Dim CompareInt As Integer
        Dim RangeQ As Integer
        Randomize()

        For X = 0 To MaxGridX
            For Y = 0 To MaxGridY
                EuclDis(X, Y) = 0
            Next
        Next


        For Z = 0 To 8 'Number of random points to apply

            PointX = Int(Rnd() * MaxGridX)
            PointY = Int(Rnd() * MaxGridY)
            RangeQ = Int(Rnd() * 11)

            For X = 0 To MaxGridX
                For Y = 0 To MaxGridY
                    CompareInt = Math.Sqrt((X - PointX) ^ 2 + (Y - PointY) ^ 2)
                    If (RangeQ - CompareInt) > EuclDis(X, Y) Then
                        EuclDis(X, Y) = RangeQ - CompareInt
                    End If
                Next
            Next
        Next

    End Sub

Since we have multiple points we check if we will apply the height on adjacent grid cells that are already elevated ((RangeQ - CompareInt) > EuclDis(X, Y))

Multiple points
 

Changing our grid and cell size we can get results like the ones in the image below


Now if we want more radius on our points we can change this RangeQ = Int(Rnd() * 11) to get higher values.



Comments