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

我有要画线的坐标列表。

实际上问题是这些坐标不按顺序排列。

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

线的坐标为startx,starty,endx,endy。

下面是线坐标列表。

3350 1500 3200 1500

1450 1750 1450 2200

1450 2200 2100 2200

2400 2200 2550 2200

2550 2200 2550 2350

2550 2350 2850 2350

2850 2350 2850 2700

2850 2700 3350 2700

3650 2700 3750 2700

3750 2700 3750 2600

3750 2600 5250 2600

5250 2600 5250 2350

5250 2350 5000 2350

4700 2350 4350 2350

4350 2350 4350 1600

4350 1600 3650 1600

3650 1600 3650 1500

3200 1500 3200 1750

3200 1750 1450 1750

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

3200 1500 3200 1750

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);

            }

        }

    }

}

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


MMMHUHU
浏览 85回答 1
1回答

MM们

基于以下假设进行更新:问题中给出的第一行坐标表示连接线集中第一条线的坐标找到第一组连接线后,问题中给出的剩余坐标线中第一条未使用的线将被视为下一组连接线中第一条线的坐标,依此类推,直到问题中给出的坐标列表为止问题排气。所需的逻辑在Main.java.&nbsp;我还创建Point.java并Line.java测试了逻辑。如果您遇到任何问题,请随时告诉我。Point.javapublic class Point {&nbsp; &nbsp; int x,y;&nbsp; &nbsp; public Point(int x, int y) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; }&nbsp; &nbsp; public void setX(int x) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; &nbsp; public void setY(int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Point [x=" + x + ", y=" + y + "]";&nbsp; &nbsp; }&nbsp; &nbsp;}Line.javapublic class Line {&nbsp; &nbsp; int x1,y1,x2,y2;&nbsp; &nbsp; Point start,end;&nbsp; &nbsp; boolean used;&nbsp; &nbsp; public Line(int x1, int y1, int x2, int y2) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.x1 = x1;&nbsp; &nbsp; &nbsp; &nbsp; this.y1 = y1;&nbsp; &nbsp; &nbsp; &nbsp; this.x2 = x2;&nbsp; &nbsp; &nbsp; &nbsp; this.y2 = y2;&nbsp; &nbsp; }&nbsp; &nbsp; public Line(Point start, Point end) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.start = start;&nbsp; &nbsp; &nbsp; &nbsp; this.end = end;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX1() {&nbsp; &nbsp; &nbsp; &nbsp; return x1;&nbsp; &nbsp; }&nbsp; &nbsp; public void setX1(int x1) {&nbsp; &nbsp; &nbsp; &nbsp; this.x1 = x1;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY1() {&nbsp; &nbsp; &nbsp; &nbsp; return y1;&nbsp; &nbsp; }&nbsp; &nbsp; public void setY1(int y1) {&nbsp; &nbsp; &nbsp; &nbsp; this.y1 = y1;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX2() {&nbsp; &nbsp; &nbsp; &nbsp; return x2;&nbsp; &nbsp; }&nbsp; &nbsp; public void setX2(int x2) {&nbsp; &nbsp; &nbsp; &nbsp; this.x2 = x2;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY2() {&nbsp; &nbsp; &nbsp; &nbsp; return y2;&nbsp; &nbsp; }&nbsp; &nbsp; public void setY2(int y2) {&nbsp; &nbsp; &nbsp; &nbsp; this.y2 = y2;&nbsp; &nbsp; }&nbsp; &nbsp; public Point getStart() {&nbsp; &nbsp; &nbsp; &nbsp; return start;&nbsp; &nbsp; }&nbsp; &nbsp; public void setStart(Point start) {&nbsp; &nbsp; &nbsp; &nbsp; this.start = start;&nbsp; &nbsp; }&nbsp; &nbsp; public Point getEnd() {&nbsp; &nbsp; &nbsp; &nbsp; return end;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEnd(Point end) {&nbsp; &nbsp; &nbsp; &nbsp; this.end = end;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isUsed() {&nbsp; &nbsp; &nbsp; &nbsp; return used;&nbsp; &nbsp; }&nbsp; &nbsp; public void setUsed(boolean used) {&nbsp; &nbsp; &nbsp; &nbsp; this.used = used;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Line [x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2 + "]";&nbsp; &nbsp; }&nbsp; &nbsp;}Main.javaimport java.util.ArrayList;import java.util.List;class Main {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; List<Line> givenLines = new ArrayList<Line>();&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3350, 1500, 3200, 1500));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(1450, 1750, 1450, 2200));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(1450, 2200, 2100, 2200));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(2400, 2200, 2550, 2200));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(2550, 2200, 2550, 2350));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(2550, 2350, 2850, 2350));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(2850, 2350, 2850, 2700));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(2850, 2700, 3350, 2700));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3650, 2700, 3750, 2700));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3750, 2700, 3750, 2600));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3750, 2600, 5250, 2600));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(5250, 2600, 5250, 2350));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(5250, 2350, 5000, 2350));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(4700, 2350, 4350, 2350));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(4350, 2350, 4350, 1600));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(4350, 1600, 3650, 1600));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3650, 1600, 3650, 1500));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3200, 1500, 3200, 1750));&nbsp; &nbsp; &nbsp; &nbsp; givenLines.add(new Line(3200, 1750, 1450, 1750));&nbsp; &nbsp; &nbsp; &nbsp; int linesIndex, usedCounter=0;&nbsp; &nbsp; &nbsp; &nbsp; List<List<Line>> listOfConnectedLines = new ArrayList<List<Line>>();&nbsp; &nbsp; &nbsp; &nbsp; //The start (first) line, in the list of given lines, to be processed to find the first set of connected lines&nbsp; &nbsp; &nbsp; &nbsp; Line startLineforTheNextSetOfConnectedLines=givenLines.get(0);&nbsp; &nbsp; &nbsp; &nbsp; startLineforTheNextSetOfConnectedLines.setUsed(true);&nbsp; &nbsp; &nbsp; &nbsp; usedCounter = 1;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Process the list of given lines until all the lines have been used to form the connected lines&nbsp; &nbsp; &nbsp; &nbsp; while (usedCounter < givenLines.size()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linesIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Line> connectedLines = new ArrayList<Line>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connectedLines.add(linesIndex, startLineforTheNextSetOfConnectedLines);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Line nextLine=null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Starting with startLineforTheNextSetOfConnectedLines, the variable lastArrangedLine will hold the next lines qualifying to become the connected line&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Line lastArrangedLine=startLineforTheNextSetOfConnectedLines;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Create the list of connected lines starting with startLineforTheNextSetOfConnectedLines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < givenLines.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < givenLines.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextLine=givenLines.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!nextLine.isUsed() && lastArrangedLine.getX2() == nextLine.getX1()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && lastArrangedLine.getY2() == nextLine.getY1()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextLine.setUsed(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usedCounter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connectedLines.add(++linesIndex, nextLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastArrangedLine = nextLine;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add the list of connected lines (found from the above nested for loops) to the list of connected lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfConnectedLines.add(connectedLines);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Find the start (first) line for the next set of connected lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < givenLines.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!givenLines.get(i).isUsed()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startLineforTheNextSetOfConnectedLines=givenLines.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startLineforTheNextSetOfConnectedLines.setUsed(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usedCounter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Display the lists of connected lines&nbsp; &nbsp; &nbsp; &nbsp; for(List<Line> connectedLines:listOfConnectedLines)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(connectedLines);&nbsp; &nbsp; }}给定行列表的输出:[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]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java