Contents

vDSP_polar

Converts single-precision rectangular coordinates to polar coordinates, using the specified stride.

Declaration

extern void vDSP_polar(const float *__A, vDSP_Stride __IA, float *__C, vDSP_Stride __IC, vDSP_Length __N);

Parameters

  • __A:

    The input vector that contains adjacent pairs of coordinates.

  • __IA:

    The distance between the adjacent coordinate pairs of elements in the input vector. This value must be an even number.

  • __C:

    On output, the output vector that contains adjacent pairs of coordinates.

  • __IC:

    The distance between the adjacent coordinate pairs of elements in the output vector. This value must be an even number.

  • __N:

    The number of ordered pairs that the function processes.

Discussion

The function uses the following calculation to convert rectangular coordinates to polar coordinates:

for (n = 0; n < N; ++n)
{
    x = A[n*IA+0];
    y = A[n*IA+1];
    C[n*IC+0] = sqrt(x**2 + y**2);
    C[n*IC+1] = atan2(y, x);
}

Vector A defines the input Cartesian (x, y) pairs. The function writes polar (rho, theta) pairs, where rho is the radius and theta is the angle in the range [-pi, pi] to output vector C. N specifies the number of coordinate pairs in A and C.

Coordinate pairs are adjacent elements in the array, regardless of stride; stride is the distance from one coordinate pair to the next.

The following code shows how to convert a single pair of rectangular coordinates to their polar equivalent.

    let x = Float(5.0)
    let y = Float(5.0)

    let rectangularCoordinates = [x, y]

    var polarCoordinates: [Float] = [0, 0]

    let stride = vDSP_Stride(2)
    let n = vDSP_Length(1)

    vDSP_polar(rectangularCoordinates, stride,
                &polarCoordinates, stride,
                n)

    let radius = polarCoordinates[0]
    let angle = Measurement(value: Double(polarCoordinates[1]),
                            unit: UnitAngle.radians)
        .converted(to: UnitAngle.degrees)
        .value

On return, radius is 7.07, and angle is 45.

This function performs the inverse operation of vDSP_rect, which converts polar to rectangular coordinates.

See Also

Converting rectangular coordinates to polar coordinates