使用的PathIterator返回制约了区域内所有线段? [英] Using PathIterator to return all line segments that constrain an Area?

查看:292
本文介绍了使用的PathIterator返回制约了区域内所有线段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何将一个使用 的PathIterator 遍历制约的的 ?该仅由线的约束(但曲线的支持不会伤害)。

In Java, how would one employ PathIterator to iterate through the line segments that constrain an Area? The Area is bound only by lines (but curve support wouldn't hurt).

的方法,应该返回所有的线段的集合。

The method should return a collection of all the line segments.

推荐答案

这工作(在任何情况下,我认为),但它可能需要更彻底的测试:

This works (in all situations, I believe), but it might require more thorough testing:

Area area; // The value is set elsewhere in the code    
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 starts

for (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

这篇关于使用的PathIterator返回制约了区域内所有线段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆