Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualmanderin87

Posted 30 November 2012 - 01:12 PM

Ive rechecked everything... The rotation code was the problem.... I made two lists to keep track of the points... the original points and the rotated points. Both were pointing to the same object, i thought they were seperate, but they were not... so I just made both lists create a new point when adding to the list. The rotation is now correct.

Here is the polygon constructor

public Polygon(Point... points) {
  if(points.length < 3) {
   throw new IllegalArgumentException("polygons must contain at least 3 points.");
  }

  for(Point p : points) {
   mRotatedPoints.add(new Point(p));
   mOriginalPoints.add(new Point(p));
  }
}

And the rotation code.

public void rotate(float x0, float y0, float degree) {
  for(int i = 0; i < mOriginalPoints.size(); i++) {
   Point point = mOriginalPoints.get(i);
  
   float x = (float) (x0 + (point.x - x0) * Math.cos(Utilities.toRadians(degree)) -
				  (point.y - y0) * Math.sin(Utilities.toRadians(degree)));
   float y = (float) (y0 + (point.x - x0) * Math.sin(Utilities.toRadians(degree)) +
			(point.y - y0) * Math.cos(Utilities.toRadians(degree)));

   Point p = mRotatedPoints.get(i);
   p.x = x;
   p.y = y;
  }

thanks everyone for the help and forgive my idiocy for not noticing they were they same instead of being seperate.

#1manderin87

Posted 30 November 2012 - 01:11 PM

Ive rechecked everything... The rotation code was the proble.... I made two lists to keep track of the points... the original points and the rotated points. Both were pointing to the same object, i thought they were seperate, but they were not... so I just made both lists create a new point when adding to the list. The rotation is now correct.

Here is the polygon constructor

public Polygon(Point... points) {
  if(points.length < 3) {
   throw new IllegalArgumentException("polygons must contain at least 3 points.");
  }
 
  for(Point p : points) {
   mRotatedPoints.add(new Point(p));
   mOriginalPoints.add(new Point(p));
  }
}

And the rotation code.

public void rotate(float x0, float y0, float degree) {
  for(int i = 0; i < mOriginalPoints.size(); i++) {
   Point point = mOriginalPoints.get(i);
  
   float x = (float) (x0 + (point.x - x0) * Math.cos(Utilities.toRadians(degree)) -
				  (point.y - y0) * Math.sin(Utilities.toRadians(degree)));
   float y = (float) (y0 + (point.x - x0) * Math.sin(Utilities.toRadians(degree)) +
		    (point.y - y0) * Math.cos(Utilities.toRadians(degree)));
 
   Point p = mRotatedPoints.get(i);
   p.x = x;
   p.y = y;
  }

thanks everyone for the help and forgive my idiocy for not noticing they were they same instead of being seperate.

PARTNERS