将极角映射到0..1

Wil*_*ill 2 math trigonometry glsl

给定笛卡尔位置,如何将原点的角度映射到范围0 .. 1?

我试过了:

sweep = atan(pos.y,pos.x) + PI) / (2.*PI);
Run Code Online (Sandbox Code Playgroud)

(扫描应在0到1之间)

这是GLSL,因此atan函数对两个参数(y然后是x)感到满意并返回-PI ... PI

这在左上象限中给出1,右上角的一个漂亮的渐变绕到右下象限,然后在左下象限中为0:

严重映射的阿坦

我如何获得一个漂亮的单梯度扫描呢?我想在某个地方进行最大扫描,并且逆时针方向与其相邻.

这是我的GLSL着色器代码:

顶点着色器:

uniform mat4 MVP_MATRIX;
attribute vec2 VERTEX;
varying vec2 pos;
void main() {
    gl_Position = MVP_MATRIX * vec4(VERTEX,-2,1.);
    pos = gl_Position.xy;
}
Run Code Online (Sandbox Code Playgroud)

片段着色器:

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);
}
Run Code Online (Sandbox Code Playgroud)

int*_*jay 6

大多数编程语言都有一个双参数版本atan,通常称为.atan2这通常会给出范围内的结果(-PI,PI).要将其转换为值0-1,您可以使用:

(atan2(y,x) + PI) / (2*PI)
Run Code Online (Sandbox Code Playgroud)

由于你的语言atan函数有两个参数,它可能与它相同atan2.