0
votes

Here is a code segment in java intended to rotate the vertices with coordinates A(10,10),B(20,10),C(20,20),D(10,20) of a square by an angle about the center point of the square. The side of the square is 10 points. The angle of rotation is 90 degree. Ideally after rotation A must become B, B must become C, C must become D and D becomes A.

private Point getRotation(Point start, int side, int rotation){

    int x = start.getX();
    int y = start.getY();

    int pivot_x = x + (side/2);
    int pivot_y = y + (side/2);

    float angle = (float)Math.toRadians(rotation);

    int xR = (int)(pivot_x + (x -pivot_x)*Math.cos(angle) - (y - pivot_y)*Math.sin(angle));
    int yR = (int)(pivot_y + (x -pivot_x)*Math.sin(angle) + (y - pivot_y)*Math.cos(angle));

    return new Point(xR,yR);        
}

public static void main(String[] args) {
    Square s = new Square();
    Point rotatedPoint1= s.getRotation(new Point(10,10), 10, 90);
    System.out.println("{"+rotatedPoint1.getX()+","+rotatedPoint1.getY()+"}");

    Point rotatedPoint2= s.getRotation(new Point(20,10), 10, 90);
    System.out.println("{"+rotatedPoint2.getX()+","+rotatedPoint2.getY()+"}");

    Point rotatedPoint3= s.getRotation(new Point(20,20), 10, 90);
    System.out.println("{"+rotatedPoint3.getX()+","+rotatedPoint3.getY()+"}");

    Point rotatedPoint4= s.getRotation(new Point(10,20), 10, 90);
    System.out.println("{"+rotatedPoint4.getX()+","+rotatedPoint4.getY()+"}");
}

The result that is achieved are not correct

  • point A(10,10) rotated to (20,10) ---- correct

  • point B(20,10) rotated to (30,10) ---- INCORRECT

  • point C(20,20) rotated to (30,20) ---- INCORRECT

  • point D(10,20) rotated to (20,20) ---- INCORRECT

The formula applied is

if (h,k) are the points about which the point (x,y) needs to be rotated by an angle THETA, then the Coordinates after rotation (xR, yR) are

  • xR = h + (x-h)cos(THETA) - (y-k)sin(THETA)
  • yR = k + (x-h)sin(THETA) + (y-k)cos(THETA)

Where is the problem?

1
What are you trying to achieve with side? The formula you give is for rotating a point about another point by a given angle; but your method takes another parameter of side.chiastic-security
Is this intended for practicing? In any "real world" application, this should be done with docs.oracle.com/javase/8/docs/api/java/awt/geom/… (But note that in any case, the int coordinates of the points might cause some rouning errors. Consider using a Point2D.Double)Marco13
Oh, I see. Your getRotation method wants to be static. That was a bit confusing.chiastic-security
@Marco13 You are correct that this can be done using awt. Since i have not worked with AWT or SWT therefore a little apprehensive about introducing AWT to the project. Indeed the rouding off errors were lingering on my mind since a day or two which for the time being i was planning to ignore.But seeing the simplicity of AffineTranforms we might consider now.RaghaveShukla
Hi @Marco13 actually your concern regarding rounding errors is really true, the rotation of vertices is causing a problem. The length between two sides is changing noticeably. even after using Point2D.Double. This is happening because we are required to cast the double_x, double_y to float_x, float_y so that this can be used by the iText pdf library which requires float values for x,y to create a line. Any solutionsRaghaveShukla

1 Answers

3
votes

The problem is your calculation of the square's centroid.

It's supposed to be the same point for all four vertices. However, you calculate as (x+5,y+5) based on each new pair when you call the function. That is:

  • Call for (10,10), pivot is (15,15)
  • Call for (20,10), pivot is (25,15)
  • Call for (20,20), pivot is (25,25)
  • Call for (10,20), pivot is (15,25)

And you should have rotated them all around the same pivot (15,15).

So you should calculate the pivot before calling the getRotation() method, and pass the pre-calculated pivot as parameter instead of passing the length of the side.