Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code comments to QR code generation #517

Merged
merged 16 commits into from
May 6, 2024
Prev Previous commit
Next Next commit
Apply suggestions from code review
  • Loading branch information
Shane32 authored May 4, 2024
commit 59bb62d6267107b3dbd902acb2803f1612dd183f
2 changes: 0 additions & 2 deletions QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ public static int Score(QRCodeData qrCode)
if (qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y][x + 1] &&
qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y + 1][x] &&
qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y + 1][x + 1])
{
score2 += 3;
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions QRCoder/QRCodeGenerator.ModulePlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void AddQuietZone(QRCodeData qrCode)
/// <param name="versionStr">The bit array containing the version information.</param>
public static void PlaceVersion(QRCodeData qrCode, BitArray versionStr)
{
var size = qrCode.ModuleMatrix.Count; // The size of the QR code matrix.
var size = qrCode.ModuleMatrix.Count;

// Loop through each module position intended for version information, placed adjacent to the separators.
for (var x = 0; x < 6; x++)
Expand Down Expand Up @@ -214,7 +214,7 @@ public static void PlaceDataWords(QRCodeData qrCode, BitArray data, List<Rectang
if (up)
{
y = size - yMod; // Calculate y for upward direction.
// Place data if within data length, current position is not blocked, and leftward column is in bounds.
// Place data if within data length, current position is not blocked, and leftward column is in bounds.
if (index < count && !IsBlocked(new Rectangle(x, y, 1, 1), blockedModules))
qrCode.ModuleMatrix[y][x] = data[index++];
if (index < count && x > 0 && !IsBlocked(new Rectangle(x - 1, y, 1, 1), blockedModules))
Expand All @@ -223,7 +223,7 @@ public static void PlaceDataWords(QRCodeData qrCode, BitArray data, List<Rectang
else
{
y = yMod - 1; // Calculate y for downward direction.
// Similar checks and data placement for the downward direction.
// Similar checks and data placement for the downward direction.
if (index < count && !IsBlocked(new Rectangle(x, y, 1, 1), blockedModules))
qrCode.ModuleMatrix[y][x] = data[index++];
if (index < count && x > 0 && !IsBlocked(new Rectangle(x - 1, y, 1, 1), blockedModules))
Expand Down Expand Up @@ -284,7 +284,7 @@ public static void ReserveVersionAreas(int size, int version, List<Rectangle> bl
public static void PlaceDarkModule(QRCodeData qrCode, int version, List<Rectangle> blockedModules)
{
// Place the dark module, which is always required to be black.
qrCode.ModuleMatrix[4 * version + 9][8] = true; // Calculated position for the dark module based on the version
qrCode.ModuleMatrix[4 * version + 9][8] = true;
// Block the dark module area to prevent overwriting during further QR code generation steps.
blockedModules.Add(new Rectangle(8, 4 * version + 9, 1, 1));
}
Expand All @@ -296,7 +296,7 @@ public static void PlaceDarkModule(QRCodeData qrCode, int version, List<Rectangl
/// <param name="blockedModules">A list of rectangles representing areas that must not be overwritten. This is updated with the areas occupied by the finder patterns.</param>
public static void PlaceFinderPatterns(QRCodeData qrCode, List<Rectangle> blockedModules)
{
var size = qrCode.ModuleMatrix.Count; // Get the size of the QR code matrix.
var size = qrCode.ModuleMatrix.Count;

// Loop to place three finder patterns in the top-left, top-right, and bottom-left corners of the QR code.
for (var i = 0; i < 3; i++)
Expand Down Expand Up @@ -399,9 +399,6 @@ public static void PlaceTimingPatterns(QRCodeData qrCode, List<Rectangle> blocke
/// <summary>
/// Determines if two rectangles intersect with each other.
/// </summary>
/// <param name="r1">The first rectangle.</param>
/// <param name="r2">The second rectangle to check for intersection with the first.</param>
/// <returns>True if the rectangles intersect; otherwise, false.</returns>
private static bool Intersects(Rectangle r1, Rectangle r2)
{
// Check if any part of the rectangles overlap.
Expand All @@ -413,16 +410,15 @@ private static bool Intersects(Rectangle r1, Rectangle r2)
/// </summary>
/// <param name="r1">The rectangle to check.</param>
/// <param name="blockedModules">The list of rectangles representing blocked areas.</param>
/// <returns>True if the rectangle is blocked; otherwise, false.</returns>
private static bool IsBlocked(Rectangle r1, List<Rectangle> blockedModules)
{
// Iterate through the list of blocked modules to check for any intersection.
foreach (var blockedMod in blockedModules)
{
if (Intersects(blockedMod, r1))
return true; // Return true if an intersection is found, indicating the area is blocked.
return true;
}
return false; // Return false if no intersections are found, indicating the area is not blocked.
return false;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions QRCoder/QRCodeGenerator.Polynom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override string ToString()

foreach (var polyItem in this.PolyItems)
{
sb.Append($"a^{polyItem.Coefficient}*x^{polyItem.Exponent} + ");
sb.Append("a^" + polyItem.Coefficient + "*x^" + polyItem.Exponent + " + ");
}

// Remove the trailing " + " if the string builder has added terms
Expand All @@ -45,6 +45,5 @@ public override string ToString()
return sb.ToString();
}
}

}
}
Loading