The Artima Developer Community
Sponsored Link

Java Answers Forum
Rotating a point

1 reply on 1 page. Most recent reply: Jan 25, 2007 10:41 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Rotating a point Posted: Jan 25, 2007 8:09 AM
Reply to this message Reply
Advertisement
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
     */
    public double 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);
    }
 
    public double x;
    public double y;
    public double 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.
     */
    public static 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
     */
    private static 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
    public static double getAngle(double x1, double y1, double x2, double y2) {
        return getAngle(x2 - x1, y2 - y1);
    }
 
    public static double getTwoPiAngle(double dx, double dy) {
        double returnVal;
        if (dy == 0)
            returnVal = (dx < 0)? Math.PI : 0;
        else if (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.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Rotating a point Posted: Jan 25, 2007 10:41 AM
Reply to this message Reply
Just found an error.
The last method should be called getAngle and not getTwoPiAngle.

Flat View: This topic has 1 reply on 1 page
Topic: Rotating a point Previous Topic   Next Topic Topic: Calling non-static variable externally

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use