算法查找给定圆的最小边界矩形未正确评估

时间:2021-12-20 03:35:25

I'm trying to find the smallest bounding rectangle of a number of circles given the x and y co-ordinates of their centre and the magnitude of the radii.

我试图找到多个圆的最小边界矩形,给定它们的中心的x和y坐标以及半径的大小。

The test input is:

测试输入是:

(1, 1, 2), (2, 2, 0.5), (-1, -3, 2), (5, 2, 1)

Here's my code:

这是我的代码:

package com.CocoMac.main;

import java.util.ArrayList;
import java.util.Scanner;

public class Challenge330Easy {
    static double maxx;
    static double maxy;
    static double minx;
    static double miny;

    static ArrayList <double[]> circles = new ArrayList<double[]>();

    static String in = "1,1,2\n2,2,0.5\n-1,-3,2\n5,2,1";
    static String s;

    static Scanner inScan = new Scanner(in);

    public static void main(String[] args) {
        for(int i = 0; inScan.hasNextLine(); i++) {
            s = inScan.nextLine();
            circles.add(i, toDouble(s.split(",")));

        }

        for(int i = 0; i < circles.size(); i++) {
                if(maxx < circles.get(i)[0] + circles.get(i)[2]);
                    maxx = circles.get(i)[0] + circles.get(i)[2];

                if(minx > circles.get(i)[0] - circles.get(i)[2]);
                    minx = circles.get(i)[0] - circles.get(i)[2];

                if(maxy < circles.get(i)[1] + circles.get(i)[2]);
                    maxy = circles.get(i)[1] + circles.get(i)[2];

                if(miny > circles.get(i)[1] - circles.get(i)[2]);
                    miny = circles.get(i)[1] - circles.get(i)[2];

        }

        System.out.print("(" + minx + ", " + miny + ")" + ",");
        System.out.print("(" + minx + ", " + maxy + ")" + ",");
        System.out.print("(" + maxx + ", " + maxy + ")" + ",");
        System.out.print("(" + maxx + ", " + miny + ")");

    }

    public static double[] toDouble(String[] input) {
        double[] output = new double[input.length];
        for(int i = 0; i < input.length; i++) {
            output[i] = Double.parseDouble(input[i]);

        }

        return output;

    }

}

The expected output is:

预期的输出是:

(-3.000, -5.000), (-3.000, 3.000), (6.000, 3.000), (6.000, -5.000)

But instead, the output is:

但相反,输出是:

(4.0, 1.0),(4.0, 3.0),(6.0, 3.0),(6.0, 1.0)

I've tried everything I can think of. For whatever reason, the final input is the maximum values found from the last circle iterated through (5,2,1) instead of the largest possible from all circles, which makes me think the conditional statements used when I'm setting the maximum and minimum values of x and y may be always evaluating to true, but I have no idea how.

我已经尝试了所有我能想到的东西。无论出于何种原因,最终输入是从迭代通过(5,2,1)的最后一个圆圈中找到的最大值,而不是从所有圆圈中找到的最大值,这使我认为在设置最大值时使用的条件语句和x和y的最小值可能总是评估为true,但我不知道如何。

If you could, clue me in on what I'm missing here or point me in the direction of what I need to learn. Any help is appreciated!

如果可以,请告诉我我在这里缺少的东西,或指出我需要学习的方向。任何帮助表示赞赏!

1 个解决方案

#1


1  

One obvious problem:

一个明显的问题:

if(maxx < circles.get(i)[0] + circles.get(i)[2]);
    maxx = circles.get(i)[0] + circles.get(i)[2];

You put a semicolon after the if condition. That terminates the statement. This translates to ...

你在if条件之后加了一个分号。这终止了声明。这转化为......

if(maxx < circles.get(i)[0] + circles.get(i)[2])
    ;  // Do nothing
maxx = circles.get(i)[0] + circles.get(i)[2];

#1


1  

One obvious problem:

一个明显的问题:

if(maxx < circles.get(i)[0] + circles.get(i)[2]);
    maxx = circles.get(i)[0] + circles.get(i)[2];

You put a semicolon after the if condition. That terminates the statement. This translates to ...

你在if条件之后加了一个分号。这终止了声明。这转化为......

if(maxx < circles.get(i)[0] + circles.get(i)[2])
    ;  // Do nothing
maxx = circles.get(i)[0] + circles.get(i)[2];