Login Register
Frontpage Code library Pastebin

Code snippet #285

Added: 1.8.2013 7:31

Select all
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// =================== POINT ==========================

Type Point
    Field x As Integer
    Field y As Integer
End Type

// Luo piste
Function CreatePoint( xx, yy )

    p.Point = New(Point)
    
    p\x = xx
    p\y = yy
    
    rtn = ConvertToInteger( p )
    
    Return rtn
End Function

// =================== RECTANGLE ==========================

Type Rectangle
    Field x As Integer
    Field y As Integer
    Field w As Integer
    Field h As Integer
End Type

// Luo neliö
Function CreateRectangle( xx, yy, ww, hh )

    rect.Rectangle = New(Rectangle)
    
    rect\x = xx
    rect\y = yy
    rect\w = ww
    rect\h = hh
    
    rtn = ConvertToInteger( rect )
    
    Return rtn
End Function

// Törmäävätkö neliöt
Function RectangleIntersects( inRect1, inRect2 )
    
    rect1.Rectangle = ConvertToType( inRect1 )
    rect2.Rectangle = ConvertToType( inRect2 )
    
    Return BoxOverlap( rect1\x, rect1\y, rect1\w, rect1\h, rect2\x, rect2\y, rect2\w, rect2\h )
End Function

// Onko piste neliön sisällä
Function IsInsideRectangle( inRect, inPoint )

    rect.Rectangle = ConvertToType( inRect )
    p.Point = ConvertToType( inPoint )
    
    If (p\x > rect\x) And (p\x < rect\x + rect\w) And (p\y > rect\y) And (p\y < rect\y + rect\h) Then
        Return True
    End If
    
    Return False
End Function

// =================== QUADTREE ==========================

Type Quadtree
    Field tileID As Integer

    Field rect As Integer
    Field nodesMem As Integer // Point luokan jäseniä

    Field topLeft As Integer
    Field topRight As Integer
    Field botLeft As Integer
    Field botRight As Integer
End Type

// Luo nelipuu
Function CreateQuadtree( inRect )

    qt.Quadtree = New( Quadtree )
    
    qt\rect = inRect
    qt\nodesMem = MakeMEMBlock( 1+ 4*4 )
    PokeByte qt\nodesMem, 0, 0 ' Ei yhtään nodea täällä
    
    rtn = ConvertToInteger( qt )
    
    Return rtn
End Function

// Luo lapsi-nodet
Function SubDivide( inQt )
    
    qt.Quadtree = ConvertToType( inQt )
    rect.Rectangle = ConvertToType( qt\rect )
    
    // Uudet neliöt lapsille (inQt:n neliö jaettuna 4 pienempään neliöön)
    topLeftRect = CreateRectangle( rect\x              , rect\y             , rect\w / 2, rect\h / 2 )
    topRightRect = CreateRectangle( rect\x + rect\w / 2, rect\y             , rect\w / 2, rect\h / 2 )
    botLeftRect = CreateRectangle( rect\x              , rect\y + rect\h / 2, rect\w / 2, rect\h / 2 )
    botRightRect = CreateRectangle( rect\x + rect\w / 2, rect\y + rect\h / 2, rect\w / 2, rect\h / 2 )
    
    // Uudet lapset
    qt\topLeft = CreateQuadtree( topLeftRect )
    qt\topRight = CreateQuadtree( topRightRect )
    qt\botLeft = CreateQuadtree( botLeftRect )
    qt\botRight = CreateQuadtree( botRightRect )
    
End Function

// Lisää nelipuuhun piste
// Palauttaa FALSE, mikäli piste ei mennyt/sopinut nelipuuhun
Function InsertToQuadtree( inQt, inPoint )

    qt.Quadtree = ConvertToType( inQt )
    p.Point = ConvertToType( inPoint )
    
    ' Jos piste ei oo neliön sisällä, se ei kuulu tänne
    If Not IsInsideRectangle( qt\rect, inPoint ) Then Return False
    
    nodesCount = PeekByte(qt\nodesMem, 0)
    
    ' Jos nodeja vielä mahtuu
    If nodesCount < 4 Then
        PokeInt qt\nodesMem, 1 + nodesCount * 4, inPoint
        PokeByte qt\nodesMem, 0, nodesCount + 1
        
        Return True ' Jeah, success!
    End If
    
    If( qt\topLeft = 0 ) Then SubDivide( inQt )
    
    // Koita tunkee Point johonkin lapsi-nodeen
    If( InsertToQuadtree( qt\topLeft , inPoint ) = False ) Then Return False
    If( InsertToQuadtree( qt\topRight, inPoint ) = False ) Then Return False
    If( InsertToQuadtree( qt\botLeft , inPoint ) = False ) Then Return False
    If( InsertToQuadtree( qt\botRight, inPoint ) = False ) Then Return False
    
    Return False // Point ei menny mihinkään, joku kusi tms
End Function

// Etsi ja palauta pisteet halutulta alueelta
// Palauttaa muistipalan
Function GetPointsFromArea( inQt, inRect )

    qt.Quadtree = ConvertToType( inQt )
    rect.Rectangle = ConvertToType( inRect )

    // Palautuspino
    rtnStack = CreateStack()
    
    // Katotaan onko tämä quadtree-node haetun neliön sisällä
    If Not RectangleIntersects( inRect, qt\rect ) Then Return rtnStack
    
    nodesCount = PeekByte( qt\nodesMem, 0 )
    
    For i = 0 To nodesCount - 1
        
        // Pisteen muistiosoite
        point = PeekInt( qt\nodesMem, 1 + i * 4 )
        
        If IsInsideRectangle( inRect, point ) Then
            Push( rtnStack, point )
        End If
    Next i
    
    // Jos ei ole lapsia, palauta pino
    If qt\topLeft = 0 Then Return rtnStack
    
    MergeStacks( rtnStack, GetPointsFromArea( qt\topLeft, inRect ) )
    MergeStacks( rtnStack, GetPointsFromArea( qt\topRight, inRect ) )
    MergeStacks( rtnStack, GetPointsFromArea( qt\botLeft, inRect ) )
    MergeStacks( rtnStack, GetPointsFromArea( qt\botRight, inRect ) )
    
    Return rtnStack
End Function


// =================== STACK ==========================

// Luo pino
Function CreateStack()
    
    mem = MakeMEMBlock(1)
    
    PokeByte mem, 0, 0
    
    Return mem
End Function

// Työnnä pinoon
Function Push( stack, in )

    stackSize = PeekByte( stack, 0 )
    ResizeMEMBlock stack, stackSize + 4
    PokeByte stack, 0, stackSize + 4
    
    PokeInt stack, stackSize, in
End Function

// Palauta pinosta
Function Pop( stack )

    stackSize = PeekByte( stack, 0 )

    rtn = PeekInt( stack, stackSize - 4 )
    
    ResizeMEMBlock stack, stackSize - 4
    PokeByte stack, 0, stackSize - 4
    
    Return rtn
End Function

// Yhdistä kaksi pinoa
Function MergeStacks( stack, fromStack )
    
    count = PeekByte( stack, 0 )
    
    For i = 0 To count - 1
        Push( stack, Pop( fromStack ) )
    Next i
    
    Return stack
End Function


// ======================= EXAMPLE ========================

qRect = CreateRectangle( 0, 0, 100, 100 ) // Luodaan uusi alue
qTree = CreateQuadtree( qRect ) // Luodaan uusi quadtree

// Luodaan pisteet
For i = 0 To 10
    point = CreatePoint( Rand(1,99), Rand(1,99) )
    
    InsertToQuadtree( qTree, point )
Next i

// Luodaan alue, jolta haetaan pisteet
aRect = CreateRectangle( 25, 25, 50, 50 )

// Etsitään ja haetaan kaikki pisteet alueelta
points = GetPointsFromArea( qTree, aRect )

// Käydään saadut pisteet läpi, ja piirretään ne ruudulle
Box 0,0,100,100, 0 // Quadtreen alue
Box 25,25,50,50, 0 // Haku alue

For i = 0 To PeekByte( points, 0 ) - 1
    p.Point = ConvertToType( Pop( points ) )
    
    Circle p\x-1, p\y-1, 3
Next i

DrawScreen
WaitKey
End

Function WriteLog(_text$)

    If FileExists( "debug.txt" ) = 0 Then
        fo = OpenToWrite( "debug.txt" )
            WriteLine f, LSet(Date() + " " + Time(), 22) + _text
        CloseFile fo
        Return False
    Else
    
        fs = FileSize("debug.txt")
        
        f = OpenToEdit("debug.txt")
        SeekFile f, fs
        WriteLine f, LSet(Date() + " " + Time(), 22) + _text
        CloseFile f
    End If
EndFunction