Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';“我的”怎么了;包括「;算法?_Java_Algorithm_Geometry - Fatal编程技术网

Java 什么';“我的”怎么了;包括「;算法?

Java 什么';“我的”怎么了;包括「;算法?,java,algorithm,geometry,Java,Algorithm,Geometry,建模非常接近Java AWT的类,我有我的RectanglePOJO: public class Rectangle { // The Coordinate of the upper-left corner of the Rectangle. private Coordinate upperLeft; // upperLeft.getXVal() and upperLeft.getYVal() // The width of the Rectangle. p

建模非常接近Java AWT的类,我有我的
Rectangle
POJO:

public class Rectangle {
    // The Coordinate of the upper-left corner of the Rectangle.
    private Coordinate upperLeft;   // upperLeft.getXVal() and upperLeft.getYVal()

    // The width of the Rectangle.
    private BigDecimal width;

    // The height of the Rectangle.
    private BigDecimal height;

    // Determine if we wholly contains otherRectangle. (no touching sides).
    @Override
    public boolean contains(Rectangle otherRectangle) {
        BigDecimal x = otherRectangle.getUpperLeft().getXVal();
        BigDecimal y = otherRectangle.getUpperLeft().getYVal();
        BigDecimal w = otherRectangle.getWidth();
        BigDecimal h = otherRectangle.getHeight();
        BigDecimal x0 = getUpperLeft().getXVal();
        BigDecimal y0 = getUpperLeft().getYVal();

        if(isSingularity() || w.doubleValue() <= 0.0 || h.doubleValue() <= 0.0)
            return false;

        return (
            x.doubleValue() >= x0.doubleValue() &&
            y.doubleValue() >= y0.doubleValue() &&
            (x.doubleValue() + w.doubleValue()) <= (x0.doubleValue() + getWidth().doubleValue()) &&
            (y.doubleValue() + h.doubleValue()) <= (y0.doubleValue() + getHeight().doubleValue())
        );
    }
}
答案是错误的

注意,我写这篇文章时有以下假设:

  • upperLeft
    坐标字段就是矩形的左上角;这意味着:
  • 获取右上角坐标的伪代码为
    (upperLeft.x+width,upperLeft.y)
  • 获取左下角坐标的伪代码是
    (upperLeft.x,upperLeft.y-高度)
  • 获取右下角坐标的伪代码是
    (upperLeft.x+宽度,upperLeft.y-高度)
现在,我相信我的回报值有点不对劲:

    return (
        x.doubleValue() >= x0.doubleValue() &&
        y.doubleValue() >= y0.doubleValue() &&
        (x.doubleValue() + w.doubleValue()) <= (x0.doubleValue() + getWidth().doubleValue()) &&
        (y.doubleValue() + h.doubleValue()) <= (y0.doubleValue() + getHeight().doubleValue())
    );
返回(
x、 doubleValue()>=x0.doubleValue()&&
y、 doubleValue()>=y0.doubleValue()&&

(x.doubleValue()+w.doubleValue())您似乎混淆了两个不同的坐标系。您的代码使用的坐标系是Y轴自上而下指向的坐标系(这在计算机图形学中经常使用)。同时,您的意见是指Y轴从下到上指向的标准数学坐标系

这就是为什么您的代码不能按预期的方式工作


你需要决定使用哪一个坐标系,然后修正代码或改变你计算大脑坐标的方式。

你的
y
不等式混淆了。因为你使用左上角作为起点,你需要检查负方向的包容

上图绘制了
r1
(绿色)和
r2
(粉色)。要修复代码,请进行以下调整

// y must be less than y0
y.doubleValue() <= y0.doubleValue()

// y - h must be greater than y0 - h0
(y.doubleValue() - h.doubleValue()) >= (y0.doubleValue() - getHeight().doubleValue())
//y必须小于y0
y、 doubleValue()=(y0.doubleValue()-getHeight().doubleValue())

尝试将返回语句拆分为多个值,看看哪一个是错误的。为什么会被否决?它显示了一个,是原始的,易处理的,显然是一个编程问题。@TicketMonster我添加了一个图表来帮助理解,请检查它是否有帮助。如果r1的左上角是(0,4),那么@NPE如何处理,则必须加上宽度并减去高度才能得到其他坐标。这意味着它的3个其他坐标将是(0,0)、(6,0)和(6,4)。如果对r2也这样做,那么它的坐标是(1,1)、(1,2)、(2,2)和(2,1)…@TicketMonster,如果左上角是(0,4),为什么要减去高度,得到其他坐标?在正常的数学环境中,我会同意,但在Java-2D-context中,原点位于左上角,因此y轴是反向的,r1不包含r2。大家都很困惑!如果左上角是(0,4),那么它代表最顶端(y轴方向)和矩形上最左边(x轴方向)的坐标。所以你加上宽度得到右上角和右下角的x值,减去高度得到左下角和右下角的y值。因为它是上角,所以它表示“矩形中没有东西在我的左边,矩形中没有东西比我高”@TicketMonster:您混淆了两个不同的坐标系。请参阅更新的答案。
// y must be less than y0
y.doubleValue() <= y0.doubleValue()

// y - h must be greater than y0 - h0
(y.doubleValue() - h.doubleValue()) >= (y0.doubleValue() - getHeight().doubleValue())