I know, this is more of a math problem, but maybe someone knows the solution.
I need to rotate a point defined by x, y and z coordinates around a ray defined by 2 points. I'm just too plain stupid for this. I don't even need the finished code, the formulas for x, y, z would do just fine.
My original Point3D and MathOps classes are far more complex. But for this problem here this should be enough.
class Point3D {
/** Creates a new Point3D in (0, 0, 0)
*/
public Point3D() {
this(0, 0, 0);
}
/** Creates a new Point3D with the given coordinates
*/
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/** returns the distance between this point and the point p1
*/
publicdouble getDistance(Point3D p1) {
double dx = p1.x - x;
double dy = p1.y - y;
double dz = p1.z - z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
publicdouble x;
publicdouble y;
publicdouble z;
}
class MathOps{
/** Turns the Point rotationPoint around the ray defined by offset and pRay. The rotation direction is CCW relative to the Ray's direction.
*/
publicstatic Point3D rotatePoint(Point3D offset, Point3D pRay, Point3D rotationPoint, double angle) {
double rayLength = offset.distance(pRay);
//creates a vector with the direction from offset to pRay
Point3D pVector = new Point3D(
(pRay.x - offset.x) / rayLength,
(pRay.y - offset.y) / rayLength,
(pRay.z - offset.z) / rayLength);
//creates a new point subtracting the offset from rotationPoint
Point3D tempPoint = new Point3D(
rotationPoint.x - offset.x,
rotationPoint.y - offset.y,
rotationPoint.z - offset.z);
//does the actual calculation
Point3D returnPoint = rotatePoint(pVector, tempPoint, angle);
//adds the offet to the result of the rotation
returnPoint.x += offset.x;
returnPoint.y += offset.y;
returnPoint.z += offset.z;
return returnPoint;
}
/** Rotates the Point pR around the ray through (0, 0, 0) with the direction given by pVector
* @param pVector direction of the ray
* @param pR Point to rotate
* @param angle rotation angle
* @return rotated point
*/
privatestatic Point3D rotatePoint(Point3D pVector, Point3D pR, angle) {
//
//
//I just don't know what to do here. Please help
//
//
}
//some methods which may be of use
publicstaticdouble getAngle(double x1, double y1, double x2, double y2) {
return getAngle(x2 - x1, y2 - y1);
}
publicstaticdouble getTwoPiAngle(double dx, double dy) {
double returnVal;
if (dy == 0)
returnVal = (dx < 0)? Math.PI : 0;
elseif (dx == 0)
returnVal = Math.PI * ((dy < 0)? 1.5 : 0.5);
else {
double atan = Math.atan(dy / dx);
if (dx > 0) {
returnVal = ((dy > 0)? atan : (2 * Math.PI + atan));
} else
returnVal = atan + Math.PI;
}
// if (Math.abs(returnVal - 2 * Math.PI < 0.0000000001)
// returnVal = 0;
return returnVal;
}
}
Note: This is NOT homework since I finished school 9 years ago. I tried some different approaches with a rotation matrix, the results were devastating.