Get center of a square based on the 4 corners

16,887

Solution 1

I think the code is much too complicate for what it does. If you know the figure is a square, all you need to do to find the centre is compute the midpoint between any two opposite corners.

mid[0] = (points[0][0] + points[2][0]) / 2;
mid[1] = (points[0][1] + points[2][1]) / 2;

Solution 2

Because it's a square, you can find the center by taking the average of the x coordinates of the corners, and then take the average of the y coordinates of the corners. This will give you the x and y coordinates of the center of the square. I believe this also works for rectangles.

Solution 3

I aggre with @NPE and you should probably just use this simple snippet but as for your code per se I think that you don't take into account that you have 4 points not 2:

double a = Math.sqrt( (points[0][0] - points[1][0]) * (points[0][0] - points[1][0]) 
                    + (points[0][1] - points[1][1]) * (points[0][1] - points[1][1]) );

where are the other 2 points here?

Edit:

to comment a bit what exactly you are doing here. First you are calculating the length of 1 size:

double a = Math.sqrt( (points[0][0] - points[1][0]) * (points[0][0] - points[1][0]) + (points[0][1] - points[1][1]) * (points[0][1] - points[1][1]) );

Then you take half of this size (a/=2;) and using this as the large size of triangle (hypotenuse) you calculate the length of the opposite of you angle size:

double c = a / Math.sin(Math.toRadians(45));

Then using this length you calculate the opposite and near size respectively of this tiny triangle you have created that has as hypotenuse the later length size.

So, not to be lost some comment why this is not going to work anyway:

  1. c should be first multiplied with sqrt(2) in order to represent the correct length of the diagonal in your square.
  2. x coordinate should be subtracted not added to the points[1][0]
  3. This notion is only functional with 45 degrees, otherwise it need more complex calculations.
Share:
16,887

Related videos on Youtube

Julian Veerkamp
Author by

Julian Veerkamp

Just a student from Berlin.

Updated on September 20, 2022

Comments

  • Julian Veerkamp
    Julian Veerkamp over 1 year

    I have a problem with my code, which should give me the center of a square. The square is saved as it's corners in an double[][] array.

    static double[] getMid(double[][] points){
        double[] mid = new double[2];
        double a = Math.sqrt( (points[0][0] - points[1][0]) * (points[0][0] - points[1][0]) 
                            + (points[0][1] - points[1][1]) * (points[0][1] - points[1][1]) );
        a/=2;
        double c = a / Math.sin(Math.toRadians(45));
    
        mid[0] = Math.sin(Math.toRadians(45)) * c + points[1][0];
        mid[1] = Math.cos(Math.toRadians(45)) * c + points[1][1];
    
        StdDraw.point(mid[0], mid[1]);
    
        return mid;
    }
    

    My initial idea was to calculate the distance of the center and the corner and then calculate the centerpoint with the distance and the angle. This works fine when the square is in a normal position, but once it's rotated the center is way off.

    The dots represent the calculated centers.

    The dots represent the calculated centers.

  • Julian Veerkamp
    Julian Veerkamp over 9 years
    Sometimes things get more complicated the more you think about them... Thanks, this works fine.
  • Julian Veerkamp
    Julian Veerkamp over 9 years
    I only calculated the length of one side.