Skip to content

Commit 5f77e9c

Browse files
author
Mr. X
committed
complete addData kernel with lines
1 parent caa0429 commit 5f77e9c

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/kernels/addData.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@
88
* @param {Number} yScaleFactor
99
* @param {Number} xOffset
1010
* @param {Number} yOffset
11+
* @param {Number} lineThickness
12+
* @param {Number} lineColor
13+
* @param {String} progressiveAxis
1114
*/
12-
function getAddDataKernel(gpu, dimensions, brushSize, brushColor, xScaleFactor, yScaleFactor, xOffset, yOffset) {
13-
return gpu.createKernel(function(graphPixels, value, dataIndex, lastData) {
14-
const X = this.thread.x / this.constants.xScaleFactor - this.output.x / (100 / this.constants.yOffset);
15-
const Y = this.thread.y / this.constants.yScaleFactor - this.output.y / (100 / this.constants.xOffset);
15+
function getAddDataKernel(gpu, dimensions, brushSize, brushColor, xScaleFactor, yScaleFactor, xOffset, yOffset, lineThickness, lineColor, progressiveAxis) {
16+
return gpu.createKernel(function(graphPixels, value, dataIndex, lastData, numProgress) {
17+
const x = this.thread.x + numProgress * Math.abs(this.constants.progressiveAxis - 1),
18+
y = this.thread.y + numProgress * this.constants.progressiveAxis;
19+
20+
const outX = this.output.x, outY = this.output.y;
21+
22+
const X = x / this.constants.xScaleFactor - (outX * (this.constants.yOffset / 100)) / this.constants.xScaleFactor;
23+
const Y = y / this.constants.yScaleFactor - (outY * (this.constants.xOffset / 100)) / this.constants.yScaleFactor;
1624

1725
const xDist = X - dataIndex;
1826
const yDist = Y - value;
1927

2028
const dist = Math.sqrt(xDist*xDist + yDist*yDist);
21-
let lineEqn = 0;
22-
23-
if (value - lastData != 0) {
24-
lineEqn = (Y - value) / (value - lastData) - (X - dataIndex) / this.constants.xScaleFactor;
25-
}
26-
else lineEqn = 10;
2729

28-
// return [lineEqn / 100, 0, 0]
30+
let lineEqn = X * (value - lastData) - Y - dataIndex * (value - lastData) + value;
31+
let lineDist = Math.abs(lineEqn) / Math.sqrt((value - lastData)*(value - lastData) + 1)
2932

30-
if (dist < this.constants.brushSize) return this.constants.brushColor;
31-
else if (Math.abs(lineEqn) == 0) return this.constants.brushColor;
33+
if (dist <= this.constants.brushSize) return this.constants.brushColor;
34+
else if (
35+
lineDist <= this.constants.lineThickness &&
36+
X <= dataIndex &&
37+
X >= dataIndex - 1 &&
38+
Y <= Math.max(value, lastData) &&
39+
Y >= Math.min(value, lastData)
40+
) return this.constants.lineColor;
3241
else return graphPixels[this.thread.y][this.thread.x];
3342
},
3443
{
@@ -37,18 +46,24 @@ function getAddDataKernel(gpu, dimensions, brushSize, brushColor, xScaleFactor,
3746
constants: {
3847
brushSize,
3948
brushColor,
49+
lineThickness,
50+
lineColor,
4051
xScaleFactor,
4152
yScaleFactor,
4253
xOffset,
43-
yOffset
54+
yOffset,
55+
progressiveAxis: progressiveAxis == 'y' ? 1 : 0
4456
},
4557
constantTypes: {
4658
brushColor: 'Array(3)',
4759
brushSize: 'Float',
60+
lineThickness: 'Float',
61+
lineColor: 'Array(3)',
4862
xScaleFactor: 'Float',
4963
yScaleFactor: 'Float',
5064
xOffset: 'Float',
51-
yOffset: 'Float'
65+
yOffset: 'Float',
66+
progressiveAxis: 'Integer'
5267
}
5368
})
5469
}

0 commit comments

Comments
 (0)