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

This commit is contained in:
Project Nayuki 2018-08-22 20:58:16 +00:00
parent f5be9d38f6
commit 8d6dcf2631
7 changed files with 15 additions and 37 deletions

View File

@ -465,12 +465,8 @@ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
if (version == 1)
return 0;
int numAlign = version / 7 + 2;
int step;
if (version != 32) {
// 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;
int step = (version == 32) ? 26 :
(version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
result[i] = pos;
result[0] = 6;

View File

@ -497,12 +497,8 @@ vector<int> QrCode::getAlignmentPatternPositions(int ver) {
return vector<int>();
else {
int numAlign = ver / 7 + 2;
int step;
if (ver != 32) {
// 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;
int step = (ver == 32) ? 26 :
(ver*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
vector<int> result;
for (int i = 0, pos = ver * 4 + 10; i < numAlign - 1; i++, pos -= step)

View File

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

View File

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

View File

@ -496,11 +496,8 @@ class QrCode(object):
return []
else:
numalign = ver // 7 + 2
if ver != 32:
# 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 if (ver == 32) else \
(ver*4 + numalign*2 + 1) // (numalign*2 - 2) * 2
start = ver * 4 + 10
result = [(start - i * step) for i in range(numalign - 1)] + [6]
return list(reversed(result))

View File

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

View File

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