@@ -53,7 +53,20 @@ export class QuadTreeVisualization {
53
53
// this.ctx.fillRect(chunk.left, chunk.top, chunk.right - chunk.left, chunk.bottom - chunk.top);
54
54
// }
55
55
56
- this . renderChunkFrame ( root , 0 , 0 , width , height ) ;
56
+ const stats = {
57
+ leafs : 0 ,
58
+ noLeafs : 0 ,
59
+ totalFrames : 0 ,
60
+ maxChunksPerFrame : 0 ,
61
+ averageChunksPerFrame : 0 ,
62
+ } ;
63
+
64
+ this . renderChunkFrame ( root , 0 , 0 , width , height , stats ) ;
65
+
66
+ stats . totalFrames = stats . leafs + stats . noLeafs ;
67
+ stats . averageChunksPerFrame = Math . round ( stats . averageChunksPerFrame / stats . totalFrames ) ;
68
+
69
+ console . log ( 'render chunks stats' , stats ) ;
57
70
58
71
this . canvasStage . needsUpdate = true ;
59
72
}
@@ -64,11 +77,20 @@ export class QuadTreeVisualization {
64
77
top : number ,
65
78
right : number ,
66
79
bottom : number ,
80
+ stats : { leafs : number ; noLeafs : number ; totalFrames : number ; maxChunksPerFrame : number ; averageChunksPerFrame : number } ,
67
81
) {
68
82
if ( chunk . isLeaf ) {
69
83
this . ctx . fillStyle = 'rgba(255, 0, 66, 0.5)' ;
84
+ stats . leafs ++ ;
70
85
} else {
71
86
this . ctx . fillStyle = 'rgba(255, 255, 66, 0.25)' ;
87
+ stats . noLeafs ++ ;
88
+ }
89
+
90
+ stats . averageChunksPerFrame += chunk . chunks . length ;
91
+
92
+ if ( chunk . chunks . length > stats . maxChunksPerFrame ) {
93
+ stats . maxChunksPerFrame = chunk . chunks . length ;
72
94
}
73
95
74
96
for ( const c of chunk . chunks ) {
@@ -84,14 +106,14 @@ export class QuadTreeVisualization {
84
106
this . ctx . lineTo ( chunk . originX , bottom ) ;
85
107
this . ctx . stroke ( ) ;
86
108
87
- chunk . nodes . northWest && this . renderChunkFrame ( chunk . nodes . northWest , left , top , chunk . originX , chunk . originY ) ;
88
- chunk . nodes . northEast && this . renderChunkFrame ( chunk . nodes . northEast , chunk . originX , top , right , chunk . originY ) ;
89
- chunk . nodes . southWest && this . renderChunkFrame ( chunk . nodes . southWest , left , chunk . originY , chunk . originX , bottom ) ;
90
- chunk . nodes . southEast && this . renderChunkFrame ( chunk . nodes . southEast , chunk . originX , chunk . originY , right , bottom ) ;
109
+ chunk . nodes . northWest && this . renderChunkFrame ( chunk . nodes . northWest , left , top , chunk . originX , chunk . originY , stats ) ;
110
+ chunk . nodes . northEast && this . renderChunkFrame ( chunk . nodes . northEast , chunk . originX , top , right , chunk . originY , stats ) ;
111
+ chunk . nodes . southWest && this . renderChunkFrame ( chunk . nodes . southWest , left , chunk . originY , chunk . originX , bottom , stats ) ;
112
+ chunk . nodes . southEast && this . renderChunkFrame ( chunk . nodes . southEast , chunk . originX , chunk . originY , right , bottom , stats ) ;
91
113
}
92
114
}
93
115
94
- makeRandomQuadTree ( count = 100 , maxChunkNodes = 2 ) {
116
+ makeRandomQuadTree ( randomCount = 100 , sortedCount = 100 , maxChunkNodes = 2 ) {
95
117
const WIDTH = 1000 ;
96
118
const HEIGHT = 600 ;
97
119
@@ -101,7 +123,7 @@ export class QuadTreeVisualization {
101
123
102
124
const chunks : NumberDataChunk2D [ ] = [ ] ;
103
125
104
- for ( let i = 0 ; i < count ; i ++ ) {
126
+ for ( let i = 0 ; i < randomCount ; i ++ ) {
105
127
const w = Math . ceil ( Math . max ( MIN_SIZE , Math . random ( ) * MAX_SIZE ) ) ;
106
128
const h = Math . ceil ( Math . max ( MIN_SIZE , Math . random ( ) * MAX_SIZE ) ) ;
107
129
const x = Math . round ( Math . random ( ) * ( WIDTH - MARGIN * 2 ) + MARGIN ) - w / 2 ;
@@ -116,6 +138,28 @@ export class QuadTreeVisualization {
116
138
) ;
117
139
}
118
140
141
+ const minDimension = Math . min ( WIDTH , HEIGHT ) ;
142
+ const rows = Math . ceil ( Math . sqrt ( sortedCount ) ) ;
143
+ const cellSize = Math . ceil ( minDimension / rows ) ;
144
+
145
+ const centerX = ( WIDTH - rows * cellSize ) * - 0.5 ;
146
+ const centerY = ( HEIGHT - rows * cellSize ) * - 0.5 ;
147
+
148
+ for ( let i = 0 ; i < sortedCount ; i ++ ) {
149
+ const w = Math . ceil ( cellSize + ( Math . random ( ) - 1 ) * cellSize * 0.25 ) ;
150
+ const h = Math . ceil ( cellSize + ( Math . random ( ) - 1 ) * cellSize * 0.25 ) ;
151
+ const y = Math . floor ( i / rows ) * cellSize - centerY + ( Math . random ( ) - 1 ) * cellSize * 0.25 ;
152
+ const x = ( i % rows ) * cellSize - centerX + ( Math . random ( ) - 1 ) * cellSize * 0.25 ;
153
+ chunks . push (
154
+ new NumberDataChunk2D ( {
155
+ x,
156
+ y,
157
+ width : w ,
158
+ height : h ,
159
+ } ) ,
160
+ ) ;
161
+ }
162
+
119
163
const root = new ChunkQuadTreeNode < NumberDataChunk2D > ( chunks ) ;
120
164
121
165
root . subdivide ( maxChunkNodes ) ;
0 commit comments