Search code examples
c++3d2dcartesiancylindrical

Code for converting Cartesian (x,y,z) to Cylindrical (ρ,θ,z) coordinates 2D/3D


Is there any code in C++ to converts from Cartesian (x,y,z) to Cylindrical (ρ,θ,z) coordinates in 2-dimensions and 3-dimensions!!

Thanks


Solution

  • If you are asking about a standard library function that will do this conversion for you, I do not believe there are any. However, there are some simple equations that relate the two. These equations are:

    x = p cos ( theta )
    y = p sin ( theta )
    z = z
    

    where

    p = sqrt( x^2 + y^2 )
    

    Standard C++ has the sin and cos functions. It also has asin and acos for the arc sin and the arc cos functions. These functions work in radians. It should be simple for you to write your own code to do this.