How to rotate an object defined by x y z points around the x y or z axis

假如想象 提交于 2019-12-10 14:39:09

问题


I'm working with a program where I visualize molecules. The molecules are represented by multiple x, y, and z values for each atom. What I would like to be able to do, is some math on each coordinate and have the resulting molecule be rotated on one of the axis. I've tried using these formulas:

y' = y*cos q - z*sin q

z' = y*sin q + z*cos q

x' = x

and the corresponding ones for Y and Z axis. In rotations of 180 degrees, the resulting molecule looks fine, but appears to be the enatiomer (mirror image). Any other angle (other than 360) skews the molecules apart from each other.

What I was hoping for was that all atoms would rotate "together". If it matters I'm working in Java.

Thank you for your help

edited code

if (axis == 'X') {
        // y' = y*cos q - z*sin q
        // z' = y*sin q + z*cos q
        // x' = x
        y = y * Math.cos(radians) - z * Math.sin(radians);
        z = y * Math.sin(radians) + z * Math.cos(radians);
    }
    else if(axis == 'Y'){
        //z' = z*cos q - x*sin q
        //x' = z*sin q + x*cos q
        //y' = y
        z = z * Math.cos(radians) - x * Math.sin(radians);
        x = z * Math.sin(radians) + x * Math.cos(radians);
    }
    else if(axis == 'Z'){
        //x' = x*cos q - y*sin q
        //y' = x*sin q + y*cos q 
        //z' = z
        x = x * Math.cos(radians) - y * Math.sin(radians);
        y = x * Math.sin(radians) + y * Math.cos(radians);
    }

edit 2. Found the error. I was calculating the first coordinate value correctly, and assigning it. When I attempt to calculate the second coordinate, I have changed the first coordinate and so get the wrong value for the second one. I'm sorry for the troubles

来源:https://stackoverflow.com/questions/16380147/how-to-rotate-an-object-defined-by-x-y-z-points-around-the-x-y-or-z-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!