Search code examples
javatrigonometrylabview

re/im to polar in java like in labview


I usually programmed on labview programming language but now I want to rewrite some functions from labview to java especially re/im to polar and polar to re/im. I didn't find any information about this. So how can I write them on java? Thanks!


Solution

  • It is not so difficult. As you can see here https://zone.ni.com/reference/en-XX/help/371361R-01/glang/re_im_to_polar/ it is just simple trigonometry functions like arctan2, cos and sin

    public float[] ReImToPolar(float x, float y)
        {
            float[] arr = new float[2];
            arr[0] = (float)Math.sqrt((x * x + y * y));  // r
            arr[1] = (float)Math.atan2(y, x);  // theta
            return arr;
        }
    
    public float[] PolarToReIm(float r, float theta)
        {
            float[] arr = new float[2];
            arr[0] = r * (float)Math.cos(theta);  // x
            arr[1] = r * (float)Math.sin(theta);  // y
            return arr;
        }