From 3e2770d6c01b40d5c72e5308779df39e2476514c Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sun, 26 Aug 2018 03:53:36 +0000 Subject: [PATCH] Somewhat simplified black/white balance penalty calculation. --- src/io/nayuki/fastqrcodegen/QrCode.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/io/nayuki/fastqrcodegen/QrCode.java b/src/io/nayuki/fastqrcodegen/QrCode.java index a4d623f..797d59e 100644 --- a/src/io/nayuki/fastqrcodegen/QrCode.java +++ b/src/io/nayuki/fastqrcodegen/QrCode.java @@ -436,10 +436,10 @@ public final class QrCode { } // Balance of black and white modules - int total = size * size; - // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% - for (int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++) - result += PENALTY_N4; + int total = size * size; // Note that size is odd, so black/total != 1/2 + // Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)% + int k = (Math.abs(black * 20 - total * 10) + total - 1) / total - 1; + result += k * PENALTY_N4; return result; }