Simplified and reformatted QrCode.getAlignmentPatternPositions() in different ways in all language versions.

pull/39/merge
Project Nayuki 6 years ago
parent f5be9d38f6
commit 8d6dcf2631

@ -465,12 +465,8 @@ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
if (version == 1) if (version == 1)
return 0; return 0;
int numAlign = version / 7 + 2; int numAlign = version / 7 + 2;
int step; int step = (version == 32) ? 26 :
if (version != 32) { (version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
// ceil((size - 13) / (2*numAlign - 2)) * 2
step = (version * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2;
} else // C-C-C-Combo breaker!
step = 26;
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step) for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
result[i] = pos; result[i] = pos;
result[0] = 6; result[0] = 6;

@ -497,12 +497,8 @@ vector<int> QrCode::getAlignmentPatternPositions(int ver) {
return vector<int>(); return vector<int>();
else { else {
int numAlign = ver / 7 + 2; int numAlign = ver / 7 + 2;
int step; int step = (ver == 32) ? 26 :
if (ver != 32) { (ver*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
// ceil((size - 13) / (2*numAlign - 2)) * 2
step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2;
} else // C-C-C-Combo breaker!
step = 26;
vector<int> result; vector<int> result;
for (int i = 0, pos = ver * 4 + 10; i < numAlign - 1; i++, pos -= step) for (int i = 0, pos = ver * 4 + 10; i < numAlign - 1; i++, pos -= step)

@ -653,11 +653,10 @@ public final class QrCode {
else { else {
int numAlign = ver / 7 + 2; int numAlign = ver / 7 + 2;
int step; int step;
if (ver != 32) { if (ver == 32) // Special snowflake
// ceil((size - 13) / (2*numAlign - 2)) * 2
step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2;
} else // C-C-C-Combo breaker!
step = 26; step = 26;
else // step = ceil[(size - 13) / (numAlign*2 - 2)] * 2
step = (ver*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
int[] result = new int[numAlign]; int[] result = new int[numAlign];
result[0] = 6; result[0] = 6;

@ -604,11 +604,8 @@ var qrcodegen = new function() {
else { else {
var size = ver * 4 + 17; var size = ver * 4 + 17;
var numAlign = Math.floor(ver / 7) + 2; var numAlign = Math.floor(ver / 7) + 2;
var step; var step = (ver == 32) ? 26 :
if (ver != 32) Math.ceil((size - 13) / (numAlign*2 - 2)) * 2;
step = Math.ceil((size - 13) / (2 * numAlign - 2)) * 2;
else // C-C-C-Combo breaker!
step = 26;
var result = [6]; var result = [6];
for (var i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step) for (var i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)

@ -496,11 +496,8 @@ class QrCode(object):
return [] return []
else: else:
numalign = ver // 7 + 2 numalign = ver // 7 + 2
if ver != 32: step = 26 if (ver == 32) else \
# ceil((size - 13) / (2*numalign - 2)) * 2 (ver*4 + numalign*2 + 1) // (numalign*2 - 2) * 2
step = (ver * 4 + numalign * 2 + 1) // (2 * numalign - 2) * 2
else: # C-C-C-Combo breaker!
step = 26
start = ver * 4 + 10 start = ver * 4 + 10
result = [(start - i * step) for i in range(numalign - 1)] + [6] result = [(start - i * step) for i in range(numalign - 1)] + [6]
return list(reversed(result)) return list(reversed(result))

@ -629,12 +629,8 @@ impl QrCode {
vec![] vec![]
} else { } else {
let numalign: i32 = (ver as i32) / 7 + 2; let numalign: i32 = (ver as i32) / 7 + 2;
let step: i32 = if ver != 32 { let step: i32 = if ver == 32 { 26 } else
// ceil((size - 13) / (2*numAlign - 2)) * 2 {((ver as i32)*4 + numalign*2 + 1) / (numalign*2 - 2) * 2};
((ver as i32) * 4 + numalign * 2 + 1) / (2 * numalign - 2) * 2
} else { // C-C-C-Combo breaker!
26
};
let start = (ver as i32) * 4 + 10; let start = (ver as i32) * 4 + 10;
let mut result: Vec<i32> = (0 .. numalign - 1).map(|i| start - i * step).collect(); let mut result: Vec<i32> = (0 .. numalign - 1).map(|i| start - i * step).collect();
result.push(6); result.push(6);

@ -567,11 +567,8 @@ namespace qrcodegen {
else { else {
let size: int = ver * 4 + 17; let size: int = ver * 4 + 17;
let numAlign: int = Math.floor(ver / 7) + 2; let numAlign: int = Math.floor(ver / 7) + 2;
let step: int; let step: int = (ver == 32) ? 26 :
if (ver != 32) Math.ceil((size - 13) / (numAlign*2 - 2)) * 2;
step = Math.ceil((size - 13) / (2 * numAlign - 2)) * 2;
else // C-C-C-Combo breaker!
step = 26;
let result: Array<int> = [6]; let result: Array<int> = [6];
for (let i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step) for (let i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)

Loading…
Cancel
Save