Java 我有坐标表,我的要求是安排它

Java 我有坐标表,我的要求是安排它,java,geometry,drawing,coordinates,Java,Geometry,Drawing,Coordinates,我有一张坐标表要画线 事实上,问题是这些坐标不是按顺序排列的 终点线坐标是另一条线的终点线坐标。 如果任何一条线的端点坐标与另一条线的起点坐标不匹配,则创建连接线列表 线的坐标为startx、starty、endx、endy 下面是直线坐标列表 3350 1500 3200 1500 1450 1750 1450 2200 1450 2200 2100 2200 2400220025502200 2550220025502350 2550235028502350 2850 2350 2850 2

我有一张坐标表要画线

事实上,问题是这些坐标不是按顺序排列的

终点线坐标是另一条线的终点线坐标。 如果任何一条线的端点坐标与另一条线的起点坐标不匹配,则创建连接线列表

线的坐标为startx、starty、endx、endy

下面是直线坐标列表

3350 1500 3200 1500

1450 1750 1450 2200

1450 2200 2100 2200

2400220025502200

2550220025502350

2550235028502350

2850 2350 2850 2700

2850270033502700

3650270037502700

3750270037502600

3750260052502600

5250 2600 5250 2350

5250 2350 5000 2350

4700 2350 4350 2350

4350235043501600

4350 1600 3650 1600

3650 1600 3650 1500

3200150032001750

3200 1750 1450 1750

最后两条线的坐标实际上位于第二和第三位置

3200150032001750

3200 1750 1450 1750

我的要求是创建所有相互连接的线束

List<DeviceElement> outerListWire= new ArrayList<DeviceElement>(schematicImporter.listOfWires);
List<DeviceElement> innerListWire = new ArrayList<DeviceElement>(schematicImporter.listOfWires);
    List<DeviceElement> listWireTemp = new ArrayList<DeviceElement>();
for (int j = 0; j < outerListWire.size(); j++) {
    Wire wire1 = (Wire) outerListWire.get(j);
    for (int i = 0; i < innerListWire.size(); i++) {
        Wire wire2 = (Wire) innerListWire.get(i);

        if (wire1.getEndPoint().getX() == wire2.getStartPoint().getX() && wire1.getEndPoint().getY() == wire2.getStartPoint().getY() ) {
            if (!listWireTemp.contains(wire1)) {
                listWireTemp.add(wire1);
                System.out
                .println("wire1 = " + wire1.getStartPoint().toString() + " = " + wire1.getEndPoint().toString());
                innerListWire.remove(wire1);
            }

            if (!listWireTemp.contains(wire2)) {
                listWireTemp.add(wire2);
                System.out
                .println("wire2 = " + wire2.getStartPoint().toString() + " = " + wire2.getEndPoint().toString());
                innerListWire.remove(wire2);
            }
        }
    }
}
List outerListWire=new ArrayList(schematicporter.listofwire);
List innerListWire=新的ArrayList(schematicImporter.listOfWires);
List listWireTemp=new ArrayList();
对于(int j=0;j

我已经尝试了上面的代码,但坐标列表仍然没有按顺序排列。

根据以下假设进行了更新:

[Line [x1=3350, y1=1500, x2=3200, y2=1500], Line [x1=3200, y1=1500, x2=3200, y2=1750], Line [x1=3200, y1=1750, x2=1450, y2=1750], Line [x1=1450, y1=1750, x2=1450, y2=2200], Line [x1=1450, y1=2200, x2=2100, y2=2200]]
[Line [x1=2400, y1=2200, x2=2550, y2=2200], Line [x1=2550, y1=2200, x2=2550, y2=2350], Line [x1=2550, y1=2350, x2=2850, y2=2350], Line [x1=2850, y1=2350, x2=2850, y2=2700], Line [x1=2850, y1=2700, x2=3350, y2=2700]]
[Line [x1=3650, y1=2700, x2=3750, y2=2700], Line [x1=3750, y1=2700, x2=3750, y2=2600], Line [x1=3750, y1=2600, x2=5250, y2=2600], Line [x1=5250, y1=2600, x2=5250, y2=2350], Line [x1=5250, y1=2350, x2=5000, y2=2350]]
[Line [x1=4700, y1=2350, x2=4350, y2=2350], Line [x1=4350, y1=2350, x2=4350, y2=1600], Line [x1=4350, y1=1600, x2=3650, y2=1600], Line [x1=3650, y1=1600, x2=3650, y2=1500]]
  • 问题中给出的第一行坐标表示连接线集中第一行的坐标
  • 在找到第一组连接线后,问题中给出的剩余坐标线的第一条未使用的线将被视为下一组连接线中第一条线的坐标,依此类推,直到问题中给出的坐标列表消失
  • 所需的逻辑位于
    Main.java
    中。我还创建了
    Point.java
    Line.java
    来测试逻辑。如果您遇到任何问题,请随时告诉我

    Point.java

    public class Point {
        int x,y;
    
        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
        @Override
        public String toString() {
            return "Point [x=" + x + ", y=" + y + "]";
        }   
    }
    
    public class Line {
        int x1,y1,x2,y2;
        Point start,end;
        boolean used;
        public Line(int x1, int y1, int x2, int y2) {
            super();
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
        public Line(Point start, Point end) {
            super();
            this.start = start;
            this.end = end;
        }
        public int getX1() {
            return x1;
        }
        public void setX1(int x1) {
            this.x1 = x1;
        }
        public int getY1() {
            return y1;
        }
        public void setY1(int y1) {
            this.y1 = y1;
        }
        public int getX2() {
            return x2;
        }
        public void setX2(int x2) {
            this.x2 = x2;
        }
        public int getY2() {
            return y2;
        }
        public void setY2(int y2) {
            this.y2 = y2;
        }
        public Point getStart() {
            return start;
        }
        public void setStart(Point start) {
            this.start = start;
        }
        public Point getEnd() {
            return end;
        }
        public void setEnd(Point end) {
            this.end = end;
        }
        public boolean isUsed() {
            return used;
        }
        public void setUsed(boolean used) {
            this.used = used;
        }
        @Override
        public String toString() {
            return "Line [x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2 + "]";
        }   
    }
    
    import java.util.ArrayList;
    import java.util.List;
    
    class Main {
        public static void main(String args[]) {
            List<Line> givenLines = new ArrayList<Line>();
            givenLines.add(new Line(3350, 1500, 3200, 1500));
            givenLines.add(new Line(1450, 1750, 1450, 2200));
            givenLines.add(new Line(1450, 2200, 2100, 2200));
            givenLines.add(new Line(2400, 2200, 2550, 2200));
            givenLines.add(new Line(2550, 2200, 2550, 2350));
            givenLines.add(new Line(2550, 2350, 2850, 2350));
            givenLines.add(new Line(2850, 2350, 2850, 2700));
            givenLines.add(new Line(2850, 2700, 3350, 2700));
            givenLines.add(new Line(3650, 2700, 3750, 2700));
            givenLines.add(new Line(3750, 2700, 3750, 2600));
            givenLines.add(new Line(3750, 2600, 5250, 2600));
            givenLines.add(new Line(5250, 2600, 5250, 2350));
            givenLines.add(new Line(5250, 2350, 5000, 2350));
            givenLines.add(new Line(4700, 2350, 4350, 2350));
            givenLines.add(new Line(4350, 2350, 4350, 1600));
            givenLines.add(new Line(4350, 1600, 3650, 1600));
            givenLines.add(new Line(3650, 1600, 3650, 1500));
            givenLines.add(new Line(3200, 1500, 3200, 1750));
            givenLines.add(new Line(3200, 1750, 1450, 1750));
    
            int linesIndex, usedCounter=0;
            List<List<Line>> listOfConnectedLines = new ArrayList<List<Line>>();
    
            //The start (first) line, in the list of given lines, to be processed to find the first set of connected lines
            Line startLineforTheNextSetOfConnectedLines=givenLines.get(0);
            startLineforTheNextSetOfConnectedLines.setUsed(true);
            usedCounter = 1;        
    
            //Process the list of given lines until all the lines have been used to form the connected lines
            while (usedCounter < givenLines.size()) {
    
                linesIndex = 0;         
                List<Line> connectedLines = new ArrayList<Line>();
                connectedLines.add(linesIndex, startLineforTheNextSetOfConnectedLines);     
                Line nextLine=null;
    
                //Starting with startLineforTheNextSetOfConnectedLines, the variable lastArrangedLine will hold the next lines qualifying to become the connected line 
                Line lastArrangedLine=startLineforTheNextSetOfConnectedLines;
    
                //Create the list of connected lines starting with startLineforTheNextSetOfConnectedLines
                for (int i = 0; i < givenLines.size(); i++) {
                    for (int j = 0; j < givenLines.size(); j++) {
                        nextLine=givenLines.get(j);
                        if (!nextLine.isUsed() && lastArrangedLine.getX2() == nextLine.getX1()
                                && lastArrangedLine.getY2() == nextLine.getY1()) {
                            nextLine.setUsed(true);
                            usedCounter++;
                            connectedLines.add(++linesIndex, nextLine);
                            lastArrangedLine = nextLine;
                            break;
                        }
                    }
                }
    
                //Add the list of connected lines (found from the above nested for loops) to the list of connected lines
                listOfConnectedLines.add(connectedLines);
    
                //Find the start (first) line for the next set of connected lines
                for (int i = 0; i < givenLines.size(); i++) {
                    if(!givenLines.get(i).isUsed()) {
                        startLineforTheNextSetOfConnectedLines=givenLines.get(i);
                        startLineforTheNextSetOfConnectedLines.setUsed(true);
                        usedCounter++;                  
                        break;
                    }
                }
            }
    
            //Display the lists of connected lines
            for(List<Line> connectedLines:listOfConnectedLines)
                System.out.println(connectedLines);
        }
    }
    
    Line.java

    public class Point {
        int x,y;
    
        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
        @Override
        public String toString() {
            return "Point [x=" + x + ", y=" + y + "]";
        }   
    }
    
    public class Line {
        int x1,y1,x2,y2;
        Point start,end;
        boolean used;
        public Line(int x1, int y1, int x2, int y2) {
            super();
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
        public Line(Point start, Point end) {
            super();
            this.start = start;
            this.end = end;
        }
        public int getX1() {
            return x1;
        }
        public void setX1(int x1) {
            this.x1 = x1;
        }
        public int getY1() {
            return y1;
        }
        public void setY1(int y1) {
            this.y1 = y1;
        }
        public int getX2() {
            return x2;
        }
        public void setX2(int x2) {
            this.x2 = x2;
        }
        public int getY2() {
            return y2;
        }
        public void setY2(int y2) {
            this.y2 = y2;
        }
        public Point getStart() {
            return start;
        }
        public void setStart(Point start) {
            this.start = start;
        }
        public Point getEnd() {
            return end;
        }
        public void setEnd(Point end) {
            this.end = end;
        }
        public boolean isUsed() {
            return used;
        }
        public void setUsed(boolean used) {
            this.used = used;
        }
        @Override
        public String toString() {
            return "Line [x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2 + "]";
        }   
    }
    
    import java.util.ArrayList;
    import java.util.List;
    
    class Main {
        public static void main(String args[]) {
            List<Line> givenLines = new ArrayList<Line>();
            givenLines.add(new Line(3350, 1500, 3200, 1500));
            givenLines.add(new Line(1450, 1750, 1450, 2200));
            givenLines.add(new Line(1450, 2200, 2100, 2200));
            givenLines.add(new Line(2400, 2200, 2550, 2200));
            givenLines.add(new Line(2550, 2200, 2550, 2350));
            givenLines.add(new Line(2550, 2350, 2850, 2350));
            givenLines.add(new Line(2850, 2350, 2850, 2700));
            givenLines.add(new Line(2850, 2700, 3350, 2700));
            givenLines.add(new Line(3650, 2700, 3750, 2700));
            givenLines.add(new Line(3750, 2700, 3750, 2600));
            givenLines.add(new Line(3750, 2600, 5250, 2600));
            givenLines.add(new Line(5250, 2600, 5250, 2350));
            givenLines.add(new Line(5250, 2350, 5000, 2350));
            givenLines.add(new Line(4700, 2350, 4350, 2350));
            givenLines.add(new Line(4350, 2350, 4350, 1600));
            givenLines.add(new Line(4350, 1600, 3650, 1600));
            givenLines.add(new Line(3650, 1600, 3650, 1500));
            givenLines.add(new Line(3200, 1500, 3200, 1750));
            givenLines.add(new Line(3200, 1750, 1450, 1750));
    
            int linesIndex, usedCounter=0;
            List<List<Line>> listOfConnectedLines = new ArrayList<List<Line>>();
    
            //The start (first) line, in the list of given lines, to be processed to find the first set of connected lines
            Line startLineforTheNextSetOfConnectedLines=givenLines.get(0);
            startLineforTheNextSetOfConnectedLines.setUsed(true);
            usedCounter = 1;        
    
            //Process the list of given lines until all the lines have been used to form the connected lines
            while (usedCounter < givenLines.size()) {
    
                linesIndex = 0;         
                List<Line> connectedLines = new ArrayList<Line>();
                connectedLines.add(linesIndex, startLineforTheNextSetOfConnectedLines);     
                Line nextLine=null;
    
                //Starting with startLineforTheNextSetOfConnectedLines, the variable lastArrangedLine will hold the next lines qualifying to become the connected line 
                Line lastArrangedLine=startLineforTheNextSetOfConnectedLines;
    
                //Create the list of connected lines starting with startLineforTheNextSetOfConnectedLines
                for (int i = 0; i < givenLines.size(); i++) {
                    for (int j = 0; j < givenLines.size(); j++) {
                        nextLine=givenLines.get(j);
                        if (!nextLine.isUsed() && lastArrangedLine.getX2() == nextLine.getX1()
                                && lastArrangedLine.getY2() == nextLine.getY1()) {
                            nextLine.setUsed(true);
                            usedCounter++;
                            connectedLines.add(++linesIndex, nextLine);
                            lastArrangedLine = nextLine;
                            break;
                        }
                    }
                }
    
                //Add the list of connected lines (found from the above nested for loops) to the list of connected lines
                listOfConnectedLines.add(connectedLines);
    
                //Find the start (first) line for the next set of connected lines
                for (int i = 0; i < givenLines.size(); i++) {
                    if(!givenLines.get(i).isUsed()) {
                        startLineforTheNextSetOfConnectedLines=givenLines.get(i);
                        startLineforTheNextSetOfConnectedLines.setUsed(true);
                        usedCounter++;                  
                        break;
                    }
                }
            }
    
            //Display the lists of connected lines
            for(List<Line> connectedLines:listOfConnectedLines)
                System.out.println(connectedLines);
        }
    }
    
    Main.java

    public class Point {
        int x,y;
    
        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
        @Override
        public String toString() {
            return "Point [x=" + x + ", y=" + y + "]";
        }   
    }
    
    public class Line {
        int x1,y1,x2,y2;
        Point start,end;
        boolean used;
        public Line(int x1, int y1, int x2, int y2) {
            super();
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
        public Line(Point start, Point end) {
            super();
            this.start = start;
            this.end = end;
        }
        public int getX1() {
            return x1;
        }
        public void setX1(int x1) {
            this.x1 = x1;
        }
        public int getY1() {
            return y1;
        }
        public void setY1(int y1) {
            this.y1 = y1;
        }
        public int getX2() {
            return x2;
        }
        public void setX2(int x2) {
            this.x2 = x2;
        }
        public int getY2() {
            return y2;
        }
        public void setY2(int y2) {
            this.y2 = y2;
        }
        public Point getStart() {
            return start;
        }
        public void setStart(Point start) {
            this.start = start;
        }
        public Point getEnd() {
            return end;
        }
        public void setEnd(Point end) {
            this.end = end;
        }
        public boolean isUsed() {
            return used;
        }
        public void setUsed(boolean used) {
            this.used = used;
        }
        @Override
        public String toString() {
            return "Line [x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2 + "]";
        }   
    }
    
    import java.util.ArrayList;
    import java.util.List;
    
    class Main {
        public static void main(String args[]) {
            List<Line> givenLines = new ArrayList<Line>();
            givenLines.add(new Line(3350, 1500, 3200, 1500));
            givenLines.add(new Line(1450, 1750, 1450, 2200));
            givenLines.add(new Line(1450, 2200, 2100, 2200));
            givenLines.add(new Line(2400, 2200, 2550, 2200));
            givenLines.add(new Line(2550, 2200, 2550, 2350));
            givenLines.add(new Line(2550, 2350, 2850, 2350));
            givenLines.add(new Line(2850, 2350, 2850, 2700));
            givenLines.add(new Line(2850, 2700, 3350, 2700));
            givenLines.add(new Line(3650, 2700, 3750, 2700));
            givenLines.add(new Line(3750, 2700, 3750, 2600));
            givenLines.add(new Line(3750, 2600, 5250, 2600));
            givenLines.add(new Line(5250, 2600, 5250, 2350));
            givenLines.add(new Line(5250, 2350, 5000, 2350));
            givenLines.add(new Line(4700, 2350, 4350, 2350));
            givenLines.add(new Line(4350, 2350, 4350, 1600));
            givenLines.add(new Line(4350, 1600, 3650, 1600));
            givenLines.add(new Line(3650, 1600, 3650, 1500));
            givenLines.add(new Line(3200, 1500, 3200, 1750));
            givenLines.add(new Line(3200, 1750, 1450, 1750));
    
            int linesIndex, usedCounter=0;
            List<List<Line>> listOfConnectedLines = new ArrayList<List<Line>>();
    
            //The start (first) line, in the list of given lines, to be processed to find the first set of connected lines
            Line startLineforTheNextSetOfConnectedLines=givenLines.get(0);
            startLineforTheNextSetOfConnectedLines.setUsed(true);
            usedCounter = 1;        
    
            //Process the list of given lines until all the lines have been used to form the connected lines
            while (usedCounter < givenLines.size()) {
    
                linesIndex = 0;         
                List<Line> connectedLines = new ArrayList<Line>();
                connectedLines.add(linesIndex, startLineforTheNextSetOfConnectedLines);     
                Line nextLine=null;
    
                //Starting with startLineforTheNextSetOfConnectedLines, the variable lastArrangedLine will hold the next lines qualifying to become the connected line 
                Line lastArrangedLine=startLineforTheNextSetOfConnectedLines;
    
                //Create the list of connected lines starting with startLineforTheNextSetOfConnectedLines
                for (int i = 0; i < givenLines.size(); i++) {
                    for (int j = 0; j < givenLines.size(); j++) {
                        nextLine=givenLines.get(j);
                        if (!nextLine.isUsed() && lastArrangedLine.getX2() == nextLine.getX1()
                                && lastArrangedLine.getY2() == nextLine.getY1()) {
                            nextLine.setUsed(true);
                            usedCounter++;
                            connectedLines.add(++linesIndex, nextLine);
                            lastArrangedLine = nextLine;
                            break;
                        }
                    }
                }
    
                //Add the list of connected lines (found from the above nested for loops) to the list of connected lines
                listOfConnectedLines.add(connectedLines);
    
                //Find the start (first) line for the next set of connected lines
                for (int i = 0; i < givenLines.size(); i++) {
                    if(!givenLines.get(i).isUsed()) {
                        startLineforTheNextSetOfConnectedLines=givenLines.get(i);
                        startLineforTheNextSetOfConnectedLines.setUsed(true);
                        usedCounter++;                  
                        break;
                    }
                }
            }
    
            //Display the lists of connected lines
            for(List<Line> connectedLines:listOfConnectedLines)
                System.out.println(connectedLines);
        }
    }
    

    您好,您能举例说明一下每行的坐标是如何表示的吗?@mettleap我有一个更新问题。请查收。@ArvindKumarAvinash,我本想说同样的话…@BhavinS。ew是否可以假定第一行上的坐标是起点?是的,每次此代码仅给出连接线的第一个列表时,第一行上的坐标都是起点。目标是找到所有连接的线路。