首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取包围框内的坐标文件

问提取包围框内的坐标文件
EN

Code Review用户
提问于 2016-05-16 03:43:07
回答 1查看 370关注 0票数 2

我正在扫描一个CSV文件,看看哪些值适合在给定的框内。CSV文件具有X和Y坐标,函数的参数是框的周长(即顶部、底部、左侧、右侧)。我不想使用任何外部库;其部分目的是学习如何手动加速代码。

我对Java相当陌生,所以任何输入都是有帮助的。如果可能的话,我试着把这个降低到100毫秒,但是现在它大约需要1000毫秒。

代码语言:javascript
复制
Scanner dataStream = new Scanner(fileMain);
while (dataStream.hasNext()){
    String data = dataStream.next();
    String[] values = data.split(",", 3);
    if(Double.parseDouble(values[0]) > lowX && Double.parseDouble(values[0]) < highX){ 
            if(Double.parseDouble(values[1]) < highY && Double.parseDouble(values[1]) > lowY){
                System.out.println(data);

    }

dataStream.close();

数据的格式如下:

代码语言:javascript
复制
90.92, 102.3
40.28, 82.32
13.02, 80
72, 104.82
EN

回答 1

Code Review用户

发布于 2016-05-16 07:55:35

首先,请注意,您始终可以使用Point2D.Double来表示平面上的点。

接下来,我将您的程序分成两种方法:第一种用于修剪不在指定边界框内的点,第二种用于从标准输入中读取这些点,并将它们放入第一种。

对于编码约定,在if语句之前我将有一个空行。另外,您应该用一个空格包围if语句的条件。所以,而不是

代码语言:javascript
复制
String[] values = data.split(",", 3);
if(Double.parseDouble(values[0])){ ...

你应该写

代码语言:javascript
复制
String[] values = data.split(",", 3);

if (Double.parseDouble(values[0])) { ...

总之,我有这样的想法:

代码语言:javascript
复制
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class CoordinateUtilities {

    public static List<Point2D.Double> 
        getPointsWithinBoundingBox(final List<Point2D.Double> pointList,
                                   final double lowX,
                                   final double highX,
                                   final double lowY,
                                   final double highY) {
        Objects.requireNonNull(pointList, "The list of points is null.");
        checkXBounds(lowX, highX);
        checkYBounds(lowY, highY);
        final List<Point2D.Double> ret = new ArrayList<>(pointList.size());

        for (final Point2D.Double point : pointList) {
            final double x = point.x;
            final double y = point.y;

            if (lowX <= x && x <= highX && lowY <= y && y <= highY) {
                ret.add(point);
            }
        }

        return ret;
    }

    public static void main(final String... args) {
        final Scanner scanner = new Scanner(System.in);
        final double lowX  = 50.0; 
        final double highX = 100.0;
        final double lowY  = 30.0;
        final double highY = 60.0;

        final List<Point2D.Double> allPoints = new ArrayList<>();

        while (scanner.hasNextLine()) {
            final String line = scanner.nextLine();

            if (line.trim().equals("quit")) {
                break;
            }

            final Point2D.Double point = parsePoint(line);

            if (point != null) {
                allPoints.add(point);
            }
        }

        final List<Point2D.Double> boundedPoints = 
                getPointsWithinBoundingBox(allPoints, lowX, highX, lowY, highY);

        boundedPoints.forEach(System.out::println);
    }

    private static Point2D.Double parsePoint(final String line) {
        final String[] tokens = line.trim().split("\\s*,\\s*");

        if (tokens.length < 2) {
            return null;
        }

        try {
            final double x = Double.parseDouble(tokens[0]);
            final double y = Double.parseDouble(tokens[1]);
            return new Point2D.Double(x, y);
        } catch (final NumberFormatException ex) {
            return null;
        }
    }

    private static void checkXBounds(final double lowX, final double highX) {
        if (Double.isNaN(lowX)) {
            throw new IllegalArgumentException(
                    "The lower bound of the x-coordinate is NaN.");
        }

        if (Double.isNaN(highX)) {
            throw new IllegalArgumentException(
                    "The upper bound of the x-coordinate is NaN.");
        }

        if (lowX > highX) {
            throw new IllegalArgumentException(
                    "The lower bound of the x-coordinate (" + lowX + "is " +
                    "larger than the upper bound of the x-coordinate (" +
                    highX + ").");
        }
    }

    private static void checkYBounds(final double lowY, final double highY) {
        if (Double.isNaN(lowY)) {
            throw new IllegalArgumentException(
                    "The lower bound of the y-coordinate is NaN.");
        }

        if (Double.isNaN(highY)) {
            throw new IllegalArgumentException(
                    "The upper bound of the y-coordinate is NaN.");
        }

        if (lowY > highY) {
            throw new IllegalArgumentException(
                    "The lower bound of the y-coordinate (" + lowY + "is " +
                    "larger than the upper bound of the y-coordinate (" +
                    highY + ").");
        }
    }
}

希望这能帮上忙。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/128450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档