How to group points

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

This a program which divides a rectangle into grids and have the individual point's co-ordinates printed.

  public static void main(String[] args) {      Map<Integer, double[]> hmap = new HashMap<>();      int id = 0;     for (double height = 9.0 ; height >= 6.0; height -=0.6) {          for (double width = 3.0 ; width <= 6.0; width+=0.6) {              hmap.put(id++, new double[] {width, height}) ;         }     }      //output      for(Integer iD : hmap.keySet()) {          System.out.printf("%2d  %3.2f %s %3.2f \n",iD,hmap.get(iD)[0],"-",hmap.get(iD)[1]);     } }

I am now planning to group them in the sense I need 1 -> (3,9),(3.6,9),(3.6,8.4),(3,8.4) instead of 1 -> (3,9).So basically I want to group the individual point which form the grids.Any help is appreciated.

回答1:

I think there's probably several ways to solve this. The way I did it was to notice that your grid cell numbers form a grid themselves. Numbers 1 to 5 are the first row, numbers 6 to 10 are the second, etc. That means I can find the row and column of any cell like this:

      int col = (cell-1) % 5;       int row = (cell-1) / 5;

Once I do that, finding the coordinates is pretty easy, just algebra. Your top row starts at 9 and decreases by 0.6 for each row. The bottom of that cell is always 0.6 less than the top. This gives the y coordinates. A similar calculation can be made for the x coordinates.

To find the coordinates, just call the static method below with its cell number. I had to subtract one from the cell parameter because the modulus counting starts at 0, and your counting starts at 1.

public class GridCoord {    public static void main(String[] args) {       for( int i = 1; i <= 25; i++ ) {          System.out.println( Arrays.toString( gridCoordinates( i ) ) );       }    }    public static double[] gridCoordinates( int cell ) {       int col = (cell-1) % 5;       int row = (cell-1) / 5;       double y1 = 9.0 - row * 0.6;       double y2 = y1 - 0.6;       double x1 = 3.0 + col * 0.6;       double x2 = x1 + 0.6;       return new double[] {x1, x2, y1, y2};    } }

Output:

run: [3.0, 3.6, 9.0, 8.4] [3.6, 4.2, 9.0, 8.4] [4.2, 4.8, 9.0, 8.4] [4.8, 5.3999999999999995, 9.0, 8.4] [5.4, 6.0, 9.0, 8.4] [3.0, 3.6, 8.4, 7.800000000000001] [3.6, 4.2, 8.4, 7.800000000000001] [4.2, 4.8, 8.4, 7.800000000000001] [4.8, 5.3999999999999995, 8.4, 7.800000000000001] [5.4, 6.0, 8.4, 7.800000000000001] [3.0, 3.6, 7.8, 7.2] [3.6, 4.2, 7.8, 7.2] [4.2, 4.8, 7.8, 7.2] [4.8, 5.3999999999999995, 7.8, 7.2] [5.4, 6.0, 7.8, 7.2] [3.0, 3.6, 7.2, 6.6000000000000005] [3.6, 4.2, 7.2, 6.6000000000000005] [4.2, 4.8, 7.2, 6.6000000000000005] [4.8, 5.3999999999999995, 7.2, 6.6000000000000005] [5.4, 6.0, 7.2, 6.6000000000000005] [3.0, 3.6, 6.6, 6.0] [3.6, 4.2, 6.6, 6.0] [4.2, 4.8, 6.6, 6.0] [4.8, 5.3999999999999995, 6.6, 6.0] [5.4, 6.0, 6.6, 6.0] BUILD SUCCESSFUL (total time: 1 second)


回答2:

import java.util.HashMap; import java.util.Map;  /**  * A solution base on using a custom data structure representing a Cell.  * Each cell is defined by 4 points, each point represented by a data   * structure Point2D holding coordinates (2 double values).  * You can make use of javafx.geometry.Point2D if you have access to javafx.  *Alternatively make your own simple Point2D class as in the following code:  */ class Cells{      public static void main(String[] args) {          //map to hold cell number and cell         Map<Integer, Cell> hmap = new HashMap<>();           double maxHeight=9.0, minHeight = 6, maxWidth= 6.0, minWidth = 3.0, step =0.6;         int cellNumber = 1;          //iterate on height (row by row top to bottom)         for (double height = maxHeight ; height > (minHeight+step); height -= step) {              //iterate over columns, left to right             for (double width = minWidth ; width < (maxWidth -step) ; width+=step) {                  Point2D tl = new Point2D(width, height);                 Point2D tr = new Point2D(width+step, height);                 Point2D br = new Point2D(width+step, height-step);                 Point2D bl = new Point2D(width, height-step);                 Cell cell = new Cell(tl, tr, br, bl);                   //add cell to map                 hmap.put(cellNumber++, cell) ;             }          }          //output         for(Integer cellNum : hmap.keySet()) {              System.out.printf("Cell:%2d  %s\n",cellNum, hmap.get(cellNum));         }     } }  //Represents a cell by its four point class Cell{      //Top left, top right, bottom right, bottom left points     private Point2D tl, tr, br,  bl;     Cell(Point2D tl, Point2D tr, Point2D br, Point2D bl){          this.tl=tl; this.tr=tr; this.br=br; this.bl=bl;     }      @Override     public String toString() {          return (tl+","+tr+","+br+","+ bl);     } }  //a container to hold 2 coordinates. Alternatively import //import javafx.geometry.Point2D class Point2D {      private double x,y;      Point2D(double x, double y){         this.x = x; this.y = y;     }     @Override     public String toString() {          return (String.format("[%3.2f %s %3.2f]", x,"-",y ));     } }

Output :


Cell: 1 [3.00 - 9.00],[3.60 - 9.00],[3.60 - 8.40],[3.00 - 8.40]
Cell: 2 [3.60 - 9.00],[4.20 - 9.00],[4.20 - 8.40],[3.60 - 8.40]
Cell: 3 [4.20 - 9.00],[4.80 - 9.00],[4.80 - 8.40],[4.20 - 8.40]
Cell: 4 [4.80 - 9.00],[5.40 - 9.00],[5.40 - 8.40],[4.80 - 8.40]
Cell: 5 [5.40 - 9.00],[6.00 - 9.00],[6.00 - 8.40],[5.40 - 8.40]
Cell: 6 [3.00 - 8.40],[3.60 - 8.40],[3.60 - 7.80],[3.00 - 7.80]
Cell: 7 [3.60 - 8.40],[4.20 - 8.40],[4.20 - 7.80],[3.60 - 7.80]
Cell: 8 [4.20 - 8.40],[4.80 - 8.40],[4.80 - 7.80],[4.20 - 7.80]
Cell: 9 [4.80 - 8.40],[5.40 - 8.40],[5.40 - 7.80],[4.80 - 7.80]
Cell:10 [5.40 - 8.40],[6.00 - 8.40],[6.00 - 7.80],[5.40 - 7.80]
Cell:11 [3.00 - 7.80],[3.60 - 7.80],[3.60 - 7.20],[3.00 - 7.20]
Cell:12 [3.60 - 7.80],[4.20 - 7.80],[4.20 - 7.20],[3.60 - 7.20]
Cell:13 [4.20 - 7.80],[4.80 - 7.80],[4.80 - 7.20],[4.20 - 7.20]
Cell:14 [4.80 - 7.80],[5.40 - 7.80],[5.40 - 7.20],[4.80 - 7.20]
Cell:15 [5.40 - 7.80],[6.00 - 7.80],[6.00 - 7.20],[5.40 - 7.20]
Cell:16 [3.00 - 7.20],[3.60 - 7.20],[3.60 - 6.60],[3.00 - 6.60]
Cell:17 [3.60 - 7.20],[4.20 - 7.20],[4.20 - 6.60],[3.60 - 6.60]
Cell:18 [4.20 - 7.20],[4.80 - 7.20],[4.80 - 6.60],[4.20 - 6.60]
Cell:19 [4.80 - 7.20],[5.40 - 7.20],[5.40 - 6.60],[4.80 - 6.60]
Cell:20 [5.40 - 7.20],[6.00 - 7.20],[6.00 - 6.60],[5.40 - 6.60]
Cell:21 [3.00 - 6.60],[3.60 - 6.60],[3.60 - 6.00],[3.00 - 6.00]
Cell:22 [3.60 - 6.60],[4.20 - 6.60],[4.20 - 6.00],[3.60 - 6.00]
Cell:23 [4.20 - 6.60],[4.80 - 6.60],[4.80 - 6.00],[4.20 - 6.00]
Cell:24 [4.80 - 6.60],[5.40 - 6.60],[5.40 - 6.00],[4.80 - 6.00]
Cell:25 [5.40 - 6.60],[6.00 - 6.60],[6.00 - 6.00],[5.40 - 6.00]



转载请标明出处:How to group points
文章来源: How to group points
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!