Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2D Object makes my object smaller, Why?

  1. #1
    Junior Member
    Join Date
    May 2010
    Thanked 0 Times in 0 Posts

    Default 2D Object makes my object smaller, Why?

    Title should read: 2D Object Rotation makes my object smaller, why?

    The problem is that the triangle become smaller and smaller after each rotation for some reason and i dont understand why, im using a formula ive found for the rotation, and its the same on many sites, so i dont know why this is happening. Heres the function that does the rotation.

    public void rotate(double x)
            double tempX[]=new double[OBJECT_COMPLEXITY];   //points for drawing objects
            double tempY[]=new double[OBJECT_COMPLEXITY];
            double cosF=Math.cos(x);    //x is Math.toRadians(10), negated when going right
            double sinF=Math.sin(x);
            for(int j=0; j<obj[0].GetPts(); j++) //loop that goes through all the points
                tempX[j]=obj[0].GetXArray()[j]; //grab the points by value from the array
                System.out.println("#" + j + " Old X: " + tempX[j] + " Old Y: " + tempY[j]);
                tempX[j]=tempX[j]*cosF-tempY[j]*sinF;   //using online formula
                System.out.println("#" + j + " New X: " + tempX[j] + " New Y: " + tempY[j]);
            obj[0].SetArrays(tempX, tempY); //send the arrays back to the object

    Now what i did is turn the triangle 10 degrees to the left, then 10 degrees to the right (yes i converted to radians)

    The original array of points::



    (This is just a triangle)

    And now here is the report generated by the rotations, here is the report after rotating to the left:

    #0 Old X: 0.0 Old Y: -20.0
    #0 New X: -3.4729635533386065 New Y: -19.093081268103244
    #1 Old X: 20.0 Old Y: 0.0
    #1 New X: 19.69615506024416 New Y: -3.4202014332566866
    #2 Old X: -20.0 Old Y: 0.0
    #2 New X: -19.69615506024416 New Y: 3.4202014332566866

    Here it is when rotating back to the right (it should be noted the size still decreases when turning constantly only in one direction)

    #0 Old X: -3.4729635533386065 Old Y: -19.093081268103244
    #0 New X: -0.10472266500395477 New Y: -18.821199361658596
    #1 Old X: 19.69615506024416 Old Y: -3.4202014332566866
    #1 New X: 19.99083795399793 New Y: 0.1031316924119956
    #2 Old X: -19.69615506024416 Old Y: 3.4202014332566866
    #2 New X: -19.99083795399793 New Y: -0.1031316924119956

    Obviously something is wrong, i would be okay with minor imprecisions but this is not even close to the original points.

    Can anyone see anything wrong with this, is perhaps the formula wrong???
    Last edited by MassiveResponse; May 15th, 2010 at 02:44 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: 2D Object makes my object smaller, Why?

    Closely inspect your formula:
                tempX[j]=tempX[j]*cosF-tempY[j]*sinF;   //using online formula
    The formula is technically correct but should be implemented differently. The formulas are
    x'=x*cos(f) - y*sin(f)
    y'= y*cos(f)+x*sin(f)
    However, your code does the following when calculating the new Y value.
    y'=y*cos(f) * x' * sin(f)

  3. The Following User Says Thank You to copeg For This Useful Post:

    MassiveResponse (May 15th, 2010)

  4. #3
    Junior Member
    Join Date
    May 2010
    Thanked 0 Times in 0 Posts

    Default Re: 2D Object makes my object smaller, Why?

    Ahhh, thanks, that did the trick. Ive never incorrectly implemented a formula before, ill be more careful next time.

Similar Threads

  1. write object to sql db
    By wolfgar in forum JDBC & Databases
    Replies: 3
    Last Post: May 18th, 2010, 10:03 AM
  2. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 04:34 PM
  3. Class and Object
    By jyothishey in forum Object Oriented Programming
    Replies: 2
    Last Post: January 25th, 2010, 08:48 AM
  4. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  5. Object Injection
    By Json in forum Java Programming Tutorials
    Replies: 1
    Last Post: December 4th, 2009, 04:08 AM