使用PathIterator返回约束一个Area的所有线段?

使用PathIterator返回约束一个Area的所有线段?,第1张

使用PathIterator返回约束一个Area的所有线段

这是可行的(我相信在所有情况下),但可能需要进行更彻底的测试:

Area area; // The value is set elsewhere in the pre    ArrayList<double[]> areaPoints = new ArrayList<double[]>();ArrayList<Line2D.Double> areaSegments = new ArrayList<Line2D.Double>();double[] coords = new double[6];for (PathIterator pi = area.getPathIterator(null); !pi.isDone(); pi.next()) {    // The type will be SEG_LINETO, SEG_MOVETO, or SEG_CLOSE    // Because the Area is composed of straight lines    int type = pi.currentSegment(coords);    // We record a double array of {segment type, x coord, y coord}    double[] pathIteratorCoords = {type, coords[0], coords[1]};    areaPoints.add(pathIteratorCoords);}double[] start = new double[3]; // To record where each polygon startsfor (int i = 0; i < areaPoints.size(); i++) {    // If we're not on the last point, return a line from this point to the next    double[] currentElement = areaPoints.get(i);    // We need a default value in case we've reached the end of the ArrayList    double[] nextElement = {-1, -1, -1};    if (i < areaPoints.size() - 1) {        nextElement = areaPoints.get(i + 1);    }    // Make the lines    if (currentElement[0] == PathIterator.SEG_MOVETO) {        start = currentElement; // Record where the polygon started to close it later    }    if (nextElement[0] == PathIterator.SEG_LINETO) {        areaSegments.add(     new Line2D.Double(         currentElement[1], currentElement[2],         nextElement[1], nextElement[2]     ) );    } else if (nextElement[0] == PathIterator.SEG_CLOSE) {        areaSegments.add(     new Line2D.Double(         currentElement[1], currentElement[2],         start[1], start[2]     ) );    }}// areaSegments now contains all the line segments


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5429433.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-11
下一篇2022-12-11

发表评论

登录后才能评论

评论列表(0条)

    保存