Given a cartesian position, how can you map the angle from the origin into the range 0 .. 1?
I have tried:
sweep = atan(pos.y,pos.x) + PI) / (2.*PI);
(where sweep should be between 0 and 1)
This is GLSL, so the atan
function is happy with two parameters (y then x) and returns -PI ... PI
This gives 1 in the top-left quadrant, a nice gradient in the top-right going round to the bottom right quadrant and then 0 in the bottom left quadrant:
How do I get a nice single gradient sweep instead? I want the maximum sweep somewhere, and the minimum adjacent to it anti-clockwise.
Here's my GLSL shader code:
Vertex shader:
uniform mat4 MVP_MATRIX;
attribute vec2 VERTEX;
varying vec2 pos;
void main() {
gl_Position = MVP_MATRIX * vec4(VERTEX,-2,1.);
pos = gl_Position.xy;
}
Fragment shader:
uniform vec4 COLOUR;
varying vec2 pos;
void main() {
float PI = 3.14159265358979323846264;
float sweep = (atan(pos.y,pos.x) + PI) / (2.*PI);
gl_FragColor = vec4(COLOUR.rgb * sweep,COLOUR.a);
}