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.

>> REGISTER NOW TO START POSTING

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

1. ## 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.GetPts(); j++) //loop that goes through all the points
{
tempX[j]=obj.GetXArray()[j]; //grab the points by value from the array
tempY[j]=obj.GetYArray()[j];

System.out.println("#" + j + " Old X: " + tempX[j] + " Old Y: " + tempY[j]);

tempX[j]=tempX[j]*cosF-tempY[j]*sinF;   //using online formula
tempY[j]=tempY[j]*cosF+tempX[j]*sinF;

System.out.println("#" + j + " New X: " + tempX[j] + " New Y: " + tempY[j]);
}

System.out.println();

obj.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::

xObjArr=0;
xObjArr=20;
xObjArr=-20;

yObjArr=-20;
yObjArr=0;
yObjArr=0;

(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???  Reply With Quote

3. ## Re: 2D Object makes my object smaller, Why?

```            tempX[j]=tempX[j]*cosF-tempY[j]*sinF;   //using online formula
tempY[j]=tempY[j]*cosF+tempX[j]*sinF;```
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)`  Reply With Quote

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

MassiveResponse (May 15th, 2010)

5. ## 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.  Reply With Quote