mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Issue #146 : add unittest for line & uniform lines
Uniform lines step is now capped so that the step remains lower than min(dx, dy). If the step would be greater than this value, use Infinity and draw a straight line
This commit is contained in:
parent
00f0debf12
commit
f5a33dc39a
@ -6,6 +6,9 @@
|
|||||||
return Math.max(Math.min(val, max), min);
|
return Math.max(Math.min(val, max), min);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the distance between {x0, y0} and {x1, y1}
|
||||||
|
*/
|
||||||
distance : function (x0, x1, y0, y1) {
|
distance : function (x0, x1, y0, y1) {
|
||||||
var dx = Math.abs(x1 - x0);
|
var dx = Math.abs(x1 - x0);
|
||||||
var dy = Math.abs(y1 - y0);
|
var dy = Math.abs(y1 - y0);
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
/**
|
/**
|
||||||
* Maximum step for uniform lines
|
* Maximum step for uniform lines
|
||||||
*/
|
*/
|
||||||
var MAX_LINE_STEP = 5;
|
var MAX_LINE_STEP = 4;
|
||||||
|
|
||||||
ns.PixelUtils = {
|
ns.PixelUtils = {
|
||||||
|
|
||||||
@ -293,24 +293,17 @@
|
|||||||
x1 = pskl.utils.normalize(x1, 0);
|
x1 = pskl.utils.normalize(x1, 0);
|
||||||
y1 = pskl.utils.normalize(y1, 0);
|
y1 = pskl.utils.normalize(y1, 0);
|
||||||
|
|
||||||
var dx = Math.abs(x1 - x0);
|
var dx = Math.abs(x1 - x0) + 1;
|
||||||
var dy = Math.abs(y1 - y0);
|
var dy = Math.abs(y1 - y0) + 1;
|
||||||
|
|
||||||
var sx = (x0 < x1) ? 1 : -1;
|
var sx = (x0 < x1) ? 1 : -1;
|
||||||
var sy = (y0 < y1) ? 1 : -1;
|
var sy = (y0 < y1) ? 1 : -1;
|
||||||
|
|
||||||
var ratio = Math.max(dx, dy) / Math.min(dx, dy);
|
var ratio = Math.max(dx, dy) / Math.min(dx, dy);
|
||||||
// in pixel art, lines should use uniform number of pixels for each step
|
// in pixel art, lines should use uniform number of pixels for each step
|
||||||
var pixelStep = Math.round(ratio);
|
var pixelStep = Math.round(ratio) || 0;
|
||||||
// invalid step, bail out
|
|
||||||
if (pixelStep === 0 || isNaN(pixelStep)) {
|
|
||||||
return pixels;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pixelStep > MAX_LINE_STEP && pixelStep < MAX_LINE_STEP * 2) {
|
if (pixelStep > Math.min(dx, dy)) {
|
||||||
pixelStep = MAX_LINE_STEP;
|
|
||||||
} else if (pixelStep >= MAX_LINE_STEP * 2) {
|
|
||||||
// straight line
|
|
||||||
pixelStep = Infinity;
|
pixelStep = Infinity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
95
test/js/utils/PixelUtilsTest.js
Normal file
95
test/js/utils/PixelUtilsTest.js
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
describe("PixelUtils test suite", function() {
|
||||||
|
|
||||||
|
beforeEach(function() {});
|
||||||
|
afterEach(function() {});
|
||||||
|
|
||||||
|
var checkPixel = function (pixel, col, row) {
|
||||||
|
expect(pixel.col).toBe(col);
|
||||||
|
expect(pixel.row).toBe(row);
|
||||||
|
};
|
||||||
|
|
||||||
|
var checkPixels = function (pixels /*, expectedPoints ... */) {
|
||||||
|
var expectedPoints = Array.prototype.slice.call(arguments, 1);
|
||||||
|
pixels.forEach(function (pixel, i) {
|
||||||
|
checkPixel(pixel, expectedPoints[i][0], expectedPoints[i][1]);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var logPixels = function (pixels) {
|
||||||
|
var buffer = [];
|
||||||
|
pixels.forEach(function (p) {
|
||||||
|
buffer.push('[' + p.col + ',' + p.row + ']');
|
||||||
|
});
|
||||||
|
console.log(buffer.join(','));
|
||||||
|
};
|
||||||
|
|
||||||
|
it("calculates line pixels", function() {
|
||||||
|
// single point
|
||||||
|
console.log('Check getLinePixels(0, 0, 0, 0)');
|
||||||
|
var pixels = pskl.PixelUtils.getLinePixels(0, 0, 0, 0);
|
||||||
|
expect(pixels.length).toBe(1);
|
||||||
|
checkPixel(pixels[0], 0, 0);
|
||||||
|
|
||||||
|
// 2-points line
|
||||||
|
console.log('Check getLinePixels(0, 1, 0, 1)');
|
||||||
|
pixels = pskl.PixelUtils.getLinePixels(0, 1, 0, 1);
|
||||||
|
expect(pixels.length).toBe(2);
|
||||||
|
checkPixel(pixels[0], 0, 0);
|
||||||
|
checkPixel(pixels[1], 1, 1);
|
||||||
|
|
||||||
|
// same line as before, reversed order
|
||||||
|
console.log('Check getLinePixels(1, 0, 1, 0)');
|
||||||
|
pixels = pskl.PixelUtils.getLinePixels(1, 0, 1, 0);
|
||||||
|
expect(pixels.length).toBe(2);
|
||||||
|
// check pixels are returned in reverse order
|
||||||
|
checkPixel(pixels[0], 1, 1);
|
||||||
|
checkPixel(pixels[1], 0, 0);
|
||||||
|
|
||||||
|
// non trivial line
|
||||||
|
console.log('Check getLinePixels(0, 2, 0, 7)');
|
||||||
|
pixels = pskl.PixelUtils.getLinePixels(0, 2, 0, 7);
|
||||||
|
expect(pixels.length).toBe(8);
|
||||||
|
checkPixels(pixels, [0,0],[0,1],[1,2],[1,3],[1,4],[1,5],[2,6],[2,7]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calculates uniform line pixels", function() {
|
||||||
|
// single point
|
||||||
|
console.log('Check getUniformLinePixels(0, 0, 0, 0)');
|
||||||
|
var pixels = pskl.PixelUtils.getUniformLinePixels(0, 0, 0, 0);
|
||||||
|
expect(pixels.length).toBe(1);
|
||||||
|
checkPixel(pixels[0], 0, 0);
|
||||||
|
|
||||||
|
// 2-points line
|
||||||
|
console.log('Check getUniformLinePixels(0, 1, 0, 1)');
|
||||||
|
pixels = pskl.PixelUtils.getUniformLinePixels(0, 1, 0, 1);
|
||||||
|
expect(pixels.length).toBe(2);
|
||||||
|
checkPixel(pixels[0], 0, 0);
|
||||||
|
checkPixel(pixels[1], 1, 1);
|
||||||
|
|
||||||
|
// same line as before, reversed order
|
||||||
|
console.log('Check getUniformLinePixels(1, 0, 1, 0)');
|
||||||
|
pixels = pskl.PixelUtils.getUniformLinePixels(1, 0, 1, 0);
|
||||||
|
expect(pixels.length).toBe(2);
|
||||||
|
// check pixels are returned in reverse order
|
||||||
|
checkPixel(pixels[0], 1, 1);
|
||||||
|
checkPixel(pixels[1], 0, 0);
|
||||||
|
|
||||||
|
// computed step should be 2, min dist is 3 (y1 -y0 + 1)-> step should be applied
|
||||||
|
console.log('Check getUniformLinePixels(0, 5, 0, 2)');
|
||||||
|
pixels = pskl.PixelUtils.getUniformLinePixels(0, 5, 0, 2);
|
||||||
|
expect(pixels.length).toBe(6);
|
||||||
|
checkPixels(pixels, [0,0],[1,0],[2,1],[3,1],[4,2],[5,2]);
|
||||||
|
|
||||||
|
// computed step should be 3, min dist is 2 (y1 -y0 + 1)-> step > minDist -> straight line
|
||||||
|
console.log('Check getUniformLinePixels(0, 5, 0, 1)');
|
||||||
|
pixels = pskl.PixelUtils.getUniformLinePixels(0, 5, 0, 1);
|
||||||
|
expect(pixels.length).toBe(7);
|
||||||
|
checkPixels(pixels, [0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0]);
|
||||||
|
|
||||||
|
// non trivial line
|
||||||
|
console.log('Check getUniformLinePixels(0, 2, 0, 7)');
|
||||||
|
pixels = pskl.PixelUtils.getUniformLinePixels(0, 2, 0, 7);
|
||||||
|
expect(pixels.length).toBe(8);
|
||||||
|
checkPixels(pixels, [0,0],[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user