Polygon shape shrinks as it rotates

I am struggling with a spin of 6 points. These points rotate around a central point in the middle. But what happens is that the shape region shrinks and becomes smaller and smaller.

The form is drawn on JPanel using PaintComponent. This means that the canvas only supports integer positioning, although I can store positions in doubles.

I use Point2D.Double to store the position of the points. I Turn all the points 1 deagre on each function call

I think my understanding of rotation is not enough, I can rotate 360 โ€‹โ€‹deegre in one call or 180, this works fine. But 45 deegres or 90 will completely rotate my points in a line (picture below).

This problem bothered me for a while, but, as always, I'm sure there is a simple solution.

Here is the rotation function

@Override public synchronized void rotatePoints(int move_x, int move_y) { // TODO Auto-generated method stub super.rotatePoints(move_x, move_y); BottomPanel.appendText("Area of Polygon is: " + UtilClass.calculateAreaOfPolygon(points)+ "\n"); double degrees=1.0; double radians = degrees * (double)(Math.PI / 180.0); //GET THE CENTER POINT C Point2D.Double center = UtilClass.getCenterOfPolygon(points); //ITERATE THROUGH THE POINTS Iterator<PointClass> itr = points.iterator(); while(itr.hasNext()) { //GET THE POINT PointClass point_class = itr.next(); //point_class = points.get(3); //FIRST TRANSLATE THE DIFFERENCE double x1 = point_class.point.x - center.x; double y1 = point_class.point.y - center.y; //APPLY ROTATION MATRIX x1 = (x1 * Math.cos(radians)) - (y1 * Math.sin(radians)); y1 = (x1 * Math.sin(radians)) + (y1 * Math.cos(radians)); //TRANSLATE BACK point_class.point.setLocation(x1 + center.x, y1 + center.y); //ADD THE DEEGRES TO POINT CLASS point_class.angle += Math.toDegrees(radians); } } 

Here is the code to restore the central location of a given polygon

 public synchronized static Point2D.Double getCenterOfPolygon(List<PointClass> points) { //GETTING THE CENTER OF A COMPLEX POLYGON double combined_x = 0; double combined_y = 0; Iterator<PointClass> itr = points.iterator(); while(itr.hasNext()) { PointClass point_class = itr.next(); //ADD TO THE combined_x += point_class.point.x; combined_y += point_class.point.y; } double center_x = combined_x / (double)points.size(); double center_y = combined_y / (double)points.size(); return new Point2D.Double(center_x, center_y); } 

Here is an image of a shape that rotates all its points 1 degree clockwise

Shape

After each rotation, I print the polygon area here is the result

  • Polygon Area: 6290
  • Polygon Area: 6288
  • Polygon Area: 6286
  • Polygon Area: 6284
  • Polygon Area: 6283
  • Polygon Area: 6281

Here is an image of a shape that rotates all its points 90 degrees clockwise after one call. He does not want to do this.

I would be happy for any suggestions or advice.

Shape

+4
source share
1 answer

The error is here:

 //APPLY ROTATION MATRIX x1 = (x1 * Math.cos(radians)) - (y1 * Math.sin(radians)); y1 = (x1 * Math.sin(radians)) + (y1 * Math.cos(radians)); 

x1 updated too early, so y1 calculated based on the new value of x1 instead of the old value.

You can change it to something like this:

 //APPLY ROTATION MATRIX double temp; temp = (x1 * Math.cos(radians)) - (y1 * Math.sin(radians)); y1 = (x1 * Math.sin(radians)) + (y1 * Math.cos(radians)); x1 = temp; 
+5
source

Source: https://habr.com/ru/post/1483682/


All Articles