Skip to content

Commit f03d3b5

Browse files
authored
Merge pull request #8096 from Abhayaj247/fix/atan-strands
Fix: allow single-arg atan() outside strands; add unit test
2 parents 1a86985 + b2e4e34 commit f03d3b5

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,8 +1615,10 @@ function shadergenerator(p5, fn) {
16151615
'asin': { args: ['genType'], returnType: 'genType', isp5Function: true },
16161616
'asinh': { args: ['genType'], returnType: 'genType', isp5Function: false },
16171617
'atan': [
1618-
{ args: ['genType'], returnType: 'genType', isp5Function: false },
1619-
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: false }
1618+
// Single-argument atan is a normal p5 function and should work outside strands
1619+
{ args: ['genType'], returnType: 'genType', isp5Function: true},
1620+
// Two-argument atan(y, x) is GLSL-only and remains strands-only
1621+
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: false},
16201622
],
16211623
'atanh': { args: ['genType'], returnType: 'genType', isp5Function: false },
16221624
'cos': { args: ['genType'], returnType: 'genType', isp5Function: true },

test/unit/math/atan.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import trigonometry from '../../../src/math/trigonometry.js';
2+
import { assert } from 'chai';
3+
4+
suite('atan', function() {
5+
const mockP5 = {
6+
RADIANS: 'radians',
7+
DEGREES: 'degrees',
8+
_validateParameters: () => {}
9+
};
10+
const mockP5Prototype = {};
11+
12+
beforeEach(function() {
13+
mockP5Prototype._angleMode = mockP5.RADIANS;
14+
mockP5Prototype.angleMode = function(mode) {
15+
this._angleMode = mode;
16+
};
17+
trigonometry(mockP5, mockP5Prototype);
18+
});
19+
20+
test('should return the correct value for atan(0.5) in radians', function() {
21+
mockP5Prototype.angleMode(mockP5.RADIANS);
22+
const expected = 0.4636476090008061; // pre-calculated value
23+
const actual = mockP5Prototype.atan(0.5);
24+
assert.closeTo(actual, expected, 1e-10);
25+
});
26+
27+
test('should return the correct value for atan(0.5) in degrees', function() {
28+
mockP5Prototype.angleMode(mockP5.DEGREES);
29+
const expected = 26.56505117707799; // pre-calculated value
30+
const actual = mockP5Prototype.atan(0.5);
31+
assert.closeTo(actual, expected, 1e-10);
32+
});
33+
});

test/unit/spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var spec = {
5555
// to omit some for speed if they should only be run manually.
5656
'webgl',
5757
'typography',
58-
'shape_modes'
58+
'shape_modes',
5959
]
6060
};
6161
document.write(

0 commit comments

Comments
 (0)