Java 我的程序不是';t正确分析正确的值

Java 我的程序不是';t正确分析正确的值,java,Java,正如您所看到的,极端值是不正确的(集合的顶部应该是3.0),并且没有裁剪正确的值。我做错了什么 编辑: 好的,所以基本上我的程序是假设把“点”(两个双倍值)加到一个“云”上,然后设定极值(从我的云的顶部到底部最远和最短的距离,就像它是一个图形一样),然后进行遍历和裁剪(设定两个点,去掉所有不在给定两点绘制的正方形中的点)。我希望这会有所帮助。在“添加点”中,您首先计算极限值,然后添加该点,因此您的最后一点不包括在该极限值中 公共布尔添加点(点p){ 极端(); 返回点。添加(p); } b、 t

正如您所看到的,极端值是不正确的(集合的顶部应该是3.0),并且没有裁剪正确的值。我做错了什么

编辑:
好的,所以基本上我的程序是假设把“点”(两个双倍值)加到一个“云”上,然后设定极值(从我的云的顶部到底部最远和最短的距离,就像它是一个图形一样),然后进行遍历和裁剪(设定两个点,去掉所有不在给定两点绘制的正方形中的点)。我希望这会有所帮助。

在“添加点”中,您首先计算极限值,然后添加该点,因此您的最后一点不包括在该极限值中

公共布尔添加点(点p){ 极端(); 返回点。添加(p); }

b、 t.w为什么不在添加的点上检查极值,而在整个集合上运行它。 唯一的变化可能是由添加的最后一点引起的。 您在O(n^2)而不是O(n)中构建云


Roni

与其让我们通过阅读代码来解析和弄清楚您的程序应该做什么,也许您可以在向我们抛出所有代码之前先描述一下您的问题?您所说的“您在O(n^2)而不是O(n)中构建云”是什么意思例如,如果你有10个点,那么对于每个加法,你调用所有点上的极限和极限运行,到目前为止,你有1+2+3+…10=55,而如果你只检查最后一个点,那么你有1,1,1,1,1..1,1=10。如果你有1000点,那么如果你只检查最后添加的点,那么动作数量的差异是550000对1000。这是性能上的一个主要差异。这些是复杂度,行动的线性数量和行动的平方数量。
import java.util.ArrayList;
import java.util.Arrays;

public class Cloud {
    private ArrayList<Point> points;
    private double left;
    private double right;
    private double top;
    private double bottom;

    private final double epsilon = 10e-6;
    /**
     * 
     * @param p a Point
     * @return whether p in the cloud
     */
    public boolean hasPoint(Point p) {
        return points.contains(p);
    }

    /**
     * Constructor
     * @param maxSize: points array size
     */
    public Cloud(){
        points = new ArrayList<Point>();
        this.left = 0.0;
        this.right = 0.0;
        this.top = 0.0;
        this.bottom = 0.0;
    }

    /**
     * 
     * @param p
     * if (size < maxSize) add p to points, increment size
     * @return the boolean value returned by the ArrayList add method
     *         
     */
    public boolean addPoint(Point p){
        extremes();
        return points.add(p);
    }

    /**
     *  Use the method toString of ArrayList
     */
    public String toString(){
        return points.toString();
        }

    /*
     * return an array of the double extremes instance variables: 
     *     left, right, top, bottom
     */
    public double[] getExtremes(){
        double[] ext = new double[4];
        ext[0] = left;
        ext[1] = right;
        ext[2] = top;
        ext[3] = bottom;

        return ext;
    }


    /**
     * Compute four double values: 
     *   left: the x coordinate of a left-most Point 
     *   right: the x coordinate of a right-most Point
     *   top: the y coordinate of a highest Point
     *   bottom: the y coordinate of a lowest Point  
     *   
     *   and put them in the appropriate instance variables
     */
    private void extremes(){
        for (int i=0; i<points.size(); i++){
            Point p = points.get(i);
            if(p.getX() < left){
                left = p.getX();
            }
            if(p.getX() > right){
                right = p.getX();
            }
            if(p.getY() < bottom){
                bottom = p.getY();
            }
            if(p.getY() > top){
                top = p.getY();
            }
        }
    }

    /**
     * 
     * @param p1 
     * @param p2
     * 
     * all Points outside the rectangle, line or point spanned
     * by p1 and p2 are removed
     * 
     * After removal, the extreme values left, right, top and bottom
     * are updated using the extremes method; then using an assert the
     * extremes of the Cloud are checked using the extremes of the two 
     * Points p1, and p2
     *  
     */
    public void crop(Point p1, Point p2){
        if(p1 == p2){
            Point temp = p1;
            points.clear();
            points.add(temp);
        }
        if(p1.getX() == p2.getX()){
            double temp = p1.getX();
            for(int i=0; i<points.size(); i++){
                if(points.get(i).getX() != temp){
                    points.remove(i);
                }
            }
        }
        if(p1.getY() == p2.getY()){
            double temp = p1.getY();
            for(int i=0; i<points.size(); i++){
                if(points.get(i).getY() != temp){
                    points.remove(i);
                }
            }
        }
        else{
            double tempLeft = p1.getX();
            double tempRight = p1.getX();
            double tempTop = p1.getY();
            double tempBottom = p1.getY();
            if(p2.getX() < tempLeft){
                tempLeft = p2.getX();
            }
            if(p2.getX() > tempRight){
                tempRight = p2.getX();
            }
            if(p2.getY() > tempTop){
                tempTop = p2.getY();
            }
            if(p2.getY() < tempBottom){
                tempBottom = p2.getY();
            }
            for(int i=0; i<points.size(); i++){
                if(points.get(i).getX() < tempLeft){
                    points.remove(i);
                }
                if(points.get(i).getX() > tempRight){
                    points.remove(i);
                }
                if(points.get(i).getY() < tempBottom){
                    points.remove(i);
                }
                if(points.get(i).getY() > tempTop){
                    points.remove(i);
                }
            }
            }
    }
    /*
     * equality check for doubles 
     */
    private boolean dblEq(double a, double b){
        return Math.abs(a-b) < epsilon;
    }


    /**
     * @param args: not used
     */
    public static void main(String[] args) {
        // TODO test all cloud methods
        Cloud set = new Cloud();
        System.out.println("initial set: " + set);
        for(int i=0; i<5; i++)
            for (int j=0; j<i; j++){
                set.addPoint(new Point(i-j*0.5,j));
            }
        System.out.println("set after addPoints: " + set);        
        double[] ext = set.getExtremes();
        if(ext != null) {
        System.out.println("extremes: " + Arrays.toString(ext));
        System.out.println("left of set: " + ext[0]);
        System.out.println("right of set: " + ext[1]);
        System.out.println("top of set: " + ext[2]);
        System.out.println("bottom of set: " + ext[3]);

        set.crop(new Point(3,0), new Point(2,2));
        System.out.println("set after crop 1: " + set);
        assert set.dblEq(set.left,2.0)   && set.dblEq(set.right,3.0) && 
               set.dblEq(set.bottom,0.0) && set.dblEq(set.top,2.0);

        set.crop(new Point(3,2),new Point(2,2));
        System.out.println("set after crop 2: " + set);
        assert set.dblEq(set.left,2.0)   && set.dblEq(set.right,3.0) && 
               set.dblEq(set.bottom,2.0) && set.dblEq(set.top,2.0);
        }
    }

}
initial set: []

set after addPoints: [(1.0,0.0), (2.0,0.0), (1.5,1.0), (3.0,0.0), (2.5,1.0), (2.0,2.0), (4.0,0.0), (3.5,1.0), (3.0,2.0), (2.5,3.0)]

extremes: [0.0, 4.0, 2.0, 0.0]

left of set: 0.0

right of set: 4.0

top of set: 2.0

bottom of set: 0.0

set after crop 1: [(2.0,0.0), (3.0,0.0), (2.5,1.0), (2.0,2.0), (3.5,1.0), (3.0,2.0)]

set after crop 2: [(3.0,0.0), (2.0,2.0), (3.0,2.0)]