Skip to content

Commit ee872e9

Browse files
committed
Use a map instead of a list of rectangles
1 parent 419d5da commit ee872e9

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

QRCoder/QRCodeGenerator.ModulePlacer.BlockedModules.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
22

33
namespace QRCoder
44
{
@@ -11,15 +11,19 @@ private static partial class ModulePlacer
1111
/// </summary>
1212
public struct BlockedModules
1313
{
14-
private readonly List<Rectangle> _blockedModules;
14+
private readonly BitArray[] _blockedModules;
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
1818
/// </summary>
1919
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
20-
public BlockedModules(int capacity)
20+
public BlockedModules(int size)
2121
{
22-
_blockedModules = new List<Rectangle>(capacity);
22+
_blockedModules = new BitArray[size];
23+
for (int i = 0; i < size; i++)
24+
{
25+
_blockedModules[i] = new BitArray(size);
26+
}
2327
}
2428

2529
/// <summary>
@@ -29,7 +33,7 @@ public BlockedModules(int capacity)
2933
/// <param name="y">The y-coordinate of the module.</param>
3034
public void Add(int x, int y)
3135
{
32-
_blockedModules.Add(new Rectangle(x, y, 1, 1));
36+
_blockedModules[y][x] = true;
3337
}
3438

3539
/// <summary>
@@ -38,7 +42,13 @@ public void Add(int x, int y)
3842
/// <param name="rect">The rectangle that defines the blocked module.</param>
3943
public void Add(Rectangle rect)
4044
{
41-
_blockedModules.Add(rect);
45+
for (int y = rect.Y; y < rect.Y + rect.Height; y++)
46+
{
47+
for (int x = rect.X; x < rect.X + rect.Width; x++)
48+
{
49+
_blockedModules[y][x] = true;
50+
}
51+
}
4252
}
4353

4454
/// <summary>
@@ -49,7 +59,7 @@ public void Add(Rectangle rect)
4959
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
5060
public bool IsBlocked(int x, int y)
5161
{
52-
return IsBlocked(new Rectangle(x, y, 1, 1));
62+
return _blockedModules[y][x];
5363
}
5464

5565
/// <summary>
@@ -59,12 +69,13 @@ public bool IsBlocked(int x, int y)
5969
/// <returns><c>true</c> if the rectangle is blocked; otherwise, <c>false</c>.</returns>
6070
public bool IsBlocked(Rectangle r1)
6171
{
62-
// Iterate through the list of blocked modules to check for any intersection.
63-
foreach (var r2 in _blockedModules)
72+
for (int y = r1.Y; y < r1.Y + r1.Height; y++)
6473
{
65-
// Check if any part of the rectangles overlap.
66-
if (r2.X < r1.X + r1.Width && r1.X < r2.X + r2.Width && r2.Y < r1.Y + r1.Height && r1.Y < r2.Y + r2.Height)
67-
return true;
74+
for (int x = r1.X; x < r1.X + r1.Width; x++)
75+
{
76+
if (_blockedModules[y][x])
77+
return true;
78+
}
6879
}
6980
return false;
7081
}

QRCoder/QRCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ QRCodeData PlaceModules()
321321
{
322322
var qr = new QRCodeData(version, true);
323323
var size = qr.ModuleMatrix.Count - 8;
324-
var blockedModules = new ModulePlacer.BlockedModules(17);
324+
var blockedModules = new ModulePlacer.BlockedModules(size);
325325
ModulePlacer.PlaceFinderPatterns(qr, blockedModules);
326326
ModulePlacer.ReserveSeperatorAreas(size, blockedModules);
327327
ModulePlacer.PlaceAlignmentPatterns(qr, alignmentPatternTable[version].PatternPositions, blockedModules);

0 commit comments

Comments
 (0)