ABOUT ME

-

  • 구면 좌표계
    개발/Etc 2025. 1. 8. 18:39

     

    출처: 위키백과

     

    • θ - theta : 세로 각도 (수직 각도, 수평선에서 위로 몇 도)
    • ϕ - phi : 가로 각도 (수평 각도, 원점 기준 좌우)

     

    • x = rsin(θ)cos(ϕ)
    • y = rsin(θ)sin(ϕ)
    • z = rcos(θ)

     

    function degreeToRadian(degree) {
        return degree * (Math.PI / 180)
    }

    function radianToDegree(radian) {
        return radian * (180 / Math.PI)
    }

    function getRadius(x, y, z) {
        return Math.sqrt(x ** 2 + y ** 2 + z ** 2)
    }

    function getTheta(x, y, z) {
        const radius = getRadius(x, y, z);
        return Math.acos(z / radius)
    }

    function getPhi(x, y) {
        return Math.atan2(y, x)
    }

    function getCoordinates(r, theta, phi) {
        const x = r * Math.sin(theta) * Math.cos(phi)
        const y = r * Math.sin(theta) * Math.sin(phi)
        const z = r * Math.cos(theta)
        return { x: x, y: y, z: z }
    }

     

    // *** Example.
    const currentValue = {
        x: -4.33,
        y: 0,
        z: -2.5,
        theta: null,
        phi: null,
        radius: null
    }

    currentValue.radius = getRadius(currentValue.x, currentValue.y, currentValue.z)
    currentValue.theta = radianToDegree(getTheta(currentValue.x, currentValue.y, currentValue.z))
    currentValue.phi = radianToDegree(getPhi(currentValue.x, currentValue.y))

    console.log('currentValue: ', currentValue)

    const targetValue = {
        x: null,
        y: null,
        z: null,
        theta: 120,
        phi: 240,
        radius: currentValue.radius
    }
    const rTheta = degreeToRadian(targetValue.theta)
    const rPhi = degreeToRadian(targetValue.phi)

    const coordinates = getCoordinates(targetValue.radius, rTheta, rPhi)
    targetValue.x = coordinates.x
    targetValue.y = coordinates.y
    targetValue.z = coordinates.z

    console.log('targetValue: ', targetValue)

     

     

    '개발 > Etc' 카테고리의 다른 글

    도커 명령어  (1) 2024.12.22
    SVN 명령어  (3) 2024.09.26
    Git 명령어  (0) 2024.09.26
    리눅스 명령어  (1) 2024.09.10
    Linux CANable을 이용한 CAN 통신 설정  (0) 2024.08.29
Designed by Tistory.