Skip to content

Commit

Permalink
Use a map instead of a list of rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed May 19, 2024
1 parent 419d5da commit ee872e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 23 additions & 12 deletions QRCoder/QRCodeGenerator.ModulePlacer.BlockedModules.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections;

namespace QRCoder
{
Expand All @@ -11,15 +11,19 @@ private static partial class ModulePlacer
/// </summary>
public struct BlockedModules
{
private readonly List<Rectangle> _blockedModules;
private readonly BitArray[] _blockedModules;

/// <summary>
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
/// </summary>
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
public BlockedModules(int capacity)
public BlockedModules(int size)
{
_blockedModules = new List<Rectangle>(capacity);
_blockedModules = new BitArray[size];
for (int i = 0; i < size; i++)
{
_blockedModules[i] = new BitArray(size);
}
}

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

/// <summary>
Expand All @@ -38,7 +42,13 @@ public void Add(int x, int y)
/// <param name="rect">The rectangle that defines the blocked module.</param>
public void Add(Rectangle rect)
{
_blockedModules.Add(rect);
for (int y = rect.Y; y < rect.Y + rect.Height; y++)
{
for (int x = rect.X; x < rect.X + rect.Width; x++)
{
_blockedModules[y][x] = true;
}
}
}

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

/// <summary>
Expand All @@ -59,12 +69,13 @@ public bool IsBlocked(int x, int y)
/// <returns><c>true</c> if the rectangle is blocked; otherwise, <c>false</c>.</returns>
public bool IsBlocked(Rectangle r1)
{
// Iterate through the list of blocked modules to check for any intersection.
foreach (var r2 in _blockedModules)
for (int y = r1.Y; y < r1.Y + r1.Height; y++)
{
// Check if any part of the rectangles overlap.
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)
return true;
for (int x = r1.X; x < r1.X + r1.Width; x++)
{
if (_blockedModules[y][x])
return true;
}
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ QRCodeData PlaceModules()
{
var qr = new QRCodeData(version, true);
var size = qr.ModuleMatrix.Count - 8;
var blockedModules = new ModulePlacer.BlockedModules(17);
var blockedModules = new ModulePlacer.BlockedModules(size);
ModulePlacer.PlaceFinderPatterns(qr, blockedModules);
ModulePlacer.ReserveSeperatorAreas(size, blockedModules);
ModulePlacer.PlaceAlignmentPatterns(qr, alignmentPatternTable[version].PatternPositions, blockedModules);
Expand Down

0 comments on commit ee872e9

Please sign in to comment.