Skip to content

Commit 1754ae0

Browse files
author
gODealOAple
committed
Solve
1 parent f39053a commit 1754ae0

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

DebugReplay/MainWindow.axaml.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Drawing;
45
using Avalonia.Controls;
56
using Avalonia.Media;
67
using DebugReplay.Helpers;
8+
using Pen = System.Drawing.Pen;
79

810
namespace DebugReplay;
911

@@ -38,7 +40,7 @@ private Bitmap BuildScene()
3840

3941
for (var i = 0; i < 5; i++)
4042
{
41-
for (var j = 0; i < 3; i++)
43+
for (var j = 0; j < 3; j++)
4244
{
4345
var rootX = (i * 250 - 100 * j)
4446
+ 50 * random.NextDouble(); // Немного случайности в положение дерева
@@ -73,17 +75,17 @@ private void DrawTree(Graphics graphics,
7375
var newBranchAngle = branchAngle + i * Math.PI / 6 * (1 + 0.4 * random.NextDouble());
7476
var newBranchLength = branchLength * 0.6 * (1 + 0.2 * random.NextDouble());
7577
DrawTree(graphics, endX, endY, newBranchAngle, newBranchLength,
76-
currentGeneration, maxGeneration);
78+
currentGeneration + 1, maxGeneration);
7779
}
7880
}
7981

80-
private System.Drawing.Pen GetBranchPen(int currentGeneration, int maxGeneration)
82+
private Pen GetBranchPen(int currentGeneration, int maxGeneration)
8183
{
8284
//// Для кэширования.
8385
//// Если pen для текущего поколения уже построен и закэширован, то берем его из кэша
84-
//var generationKey = BuildGenerationKey(currentGeneration, maxGeneration);
85-
//if (branchPenCache.ContainsKey(generationKey))
86-
// return branchPenCache[generationKey];
86+
var generationKey = BuildGenerationKey(currentGeneration, maxGeneration);
87+
if (branchPenCache.ContainsKey(generationKey))
88+
return branchPenCache[generationKey];
8789

8890
// Иначе строим новый pen
8991
var startBranchColor = Colors.SaddleBrown;
@@ -99,21 +101,24 @@ private System.Drawing.Pen GetBranchPen(int currentGeneration, int maxGeneration
99101

100102
var branchWidth = Math.Pow(1.5, 6 * (maxGeneration - currentGeneration) / maxGeneration);
101103

102-
var branchPen = new System.Drawing.Pen(branchColor, (float)branchWidth);
104+
var branchPen = new Pen(branchColor, (float)branchWidth);
103105

104106
//// Для кэширования.
105107
//// Кэшируем построенный для поколения pen
106-
//branchPenCache[generationKey] = branchPen;
108+
branchPenCache[generationKey] = branchPen;
107109

108110
return branchPen;
109111
}
110112

111-
//// Для кэширования.
112-
//private Dictionary<int[], Pen> branchPenCache = new Dictionary<int[], Pen>();
113+
// Для кэширования.
114+
private Dictionary<long, Pen> branchPenCache = new();
113115

114-
//// Для кэширования.
115-
//private int[] BuildGenerationKey(int currentGeneration, int maxGeneration)
116-
//{
117-
// return new[] { currentGeneration, maxGeneration };
118-
//}
116+
// Для кэширования.
117+
private long BuildGenerationKey(int currentGeneration, int maxGeneration)
118+
{
119+
long key = currentGeneration;
120+
key <<= 32;
121+
key += maxGeneration;
122+
return key;
123+
}
119124
}

0 commit comments

Comments
 (0)