简体   繁体   中英

How can I create a bounding box using a set of points?

I have an ArrayList of 2D points. How can I create the rectangle using these points using java?

The ArrayList of points is:

[277.0, 275.0, 269.0, 271.0, 260.0, 268.0, 184.0, 277.0, 160.0, 288.0, 137.0, 303.0, 115.0, 319.0, 97.0, 337.0, 80.0, 357.0, 67.0, 375.0, 59.0, 394.0, 55.0, 413.0, 53.0, 433.0, 55.0, 456.0, 59.0, 482.0, 66.0, 507.0, 77.0, 534.0, 90.0, 558.0, 103.0, 578.0, 117.0, 594.0, 131.0, 607.0, 147.0, 616.0, 163.0, 622.0, 179.0, 624.0, 198.0, 623.0, 221.0, 618.0, 244.0, 608.0, 266.0, 595.0, 284.0, 581.0, 300.0, 565.0, 316.0, 548.0, 329.0, 530.0, 339.0, 511.0, 348.0, 493.0, 355.0, 473.0, 361.0, 454.0, 364.0, 436.0, 367.0, 420.0, 368.0, 403.0, 367.0, 388.0, 366.0, 371.0, 360.0, 338.0, 356.0, 323.0, 349.0, 309.0, 343.0, 298.0, 337.0, 289.0, 329.0, 283.0, 320.0, 280.0, 309.0, 281.0, 299.0, 282.0, 299.0, 282.0, 277.0, 275.0]

Find the minimum and maximum x and y coordinates in your list. Your bounding box will have the following corners:
(x min , y min ), (x min , y max ), (x max , y max ), (x max , y min )

You explained your problem in a bad way. I have to guess which type your points have. Nevertheless you can use the Rectangle2D class from the Java Api. For further information click here .

The general idea for a bounding box of some points is:

Calculate the max and the min over all points for each dimension and save them. Your bounding box consists out of the points which have those values for coordinates. For a 2D case this would be the following four points:

(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin).

For a snippet of code:

        // Constructs a new Rectangle whose upper-left corner is 
        // specified by the Point argument, and whose width and 
        // height are specified by the Dimension argument.
        Rectangle2D rect = new Rectangle(new Point(3, 4), new Dimension(200, 200));

        // For a Rectangle with double Coordinates
        Rectangle2D floatRect = new Rectangle2D.Double(3.0, 4.0, width, height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM