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:
committed by
juliandescottes
parent
00f0debf12
commit
f5a33dc39a
@@ -6,6 +6,9 @@
|
||||
return Math.max(Math.min(val, max), min);
|
||||
},
|
||||
|
||||
/**
|
||||
* Calculate the distance between {x0, y0} and {x1, y1}
|
||||
*/
|
||||
distance : function (x0, x1, y0, y1) {
|
||||
var dx = Math.abs(x1 - x0);
|
||||
var dy = Math.abs(y1 - y0);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
/**
|
||||
* Maximum step for uniform lines
|
||||
*/
|
||||
var MAX_LINE_STEP = 5;
|
||||
var MAX_LINE_STEP = 4;
|
||||
|
||||
ns.PixelUtils = {
|
||||
|
||||
@@ -293,24 +293,17 @@
|
||||
x1 = pskl.utils.normalize(x1, 0);
|
||||
y1 = pskl.utils.normalize(y1, 0);
|
||||
|
||||
var dx = Math.abs(x1 - x0);
|
||||
var dy = Math.abs(y1 - y0);
|
||||
var dx = Math.abs(x1 - x0) + 1;
|
||||
var dy = Math.abs(y1 - y0) + 1;
|
||||
|
||||
var sx = (x0 < x1) ? 1 : -1;
|
||||
var sy = (y0 < y1) ? 1 : -1;
|
||||
|
||||
var ratio = Math.max(dx, dy) / Math.min(dx, dy);
|
||||
// in pixel art, lines should use uniform number of pixels for each step
|
||||
var pixelStep = Math.round(ratio);
|
||||
// invalid step, bail out
|
||||
if (pixelStep === 0 || isNaN(pixelStep)) {
|
||||
return pixels;
|
||||
}
|
||||
var pixelStep = Math.round(ratio) || 0;
|
||||
|
||||
if (pixelStep > MAX_LINE_STEP && pixelStep < MAX_LINE_STEP * 2) {
|
||||
pixelStep = MAX_LINE_STEP;
|
||||
} else if (pixelStep >= MAX_LINE_STEP * 2) {
|
||||
// straight line
|
||||
if (pixelStep > Math.min(dx, dy)) {
|
||||
pixelStep = Infinity;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user