You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

笛卡尔坐标系Point类isUnder与isRight方法合规实现问题咨询

问题根因

你当前的实现存在两处逻辑错误:

  • isUnder方法直接返回当前对象调用isAbove(other)的结果,完全搞反了上下判断的逻辑
  • isRight方法直接对isLeft(other)取反,会导致x坐标相等时也返回true,不符合右方判断的预期

解决方案

我们可以利用「A点在B点下方等价于B点在A点上方」「A点在B点右方等价于B点在A点左方」的逻辑,完全符合你要求的复用现有方法、不访问私有属性、不额外添加运算的限制。
修改后的两个方法如下:

/** check if one point is under another.
 * @parm other the point to compare to this point.
 * @return true if the point is under the point we compare to.
 */
public boolean isUnder(Point other){
    return other.isAbove(this);
}

/** check if one point is right to another.
 * @parm other the point to compare to this point.
 * @return true if the point is right to the point we compare to.
 */
public boolean isRight(Point other){
    return other.isLeft(this);
}

边界场景验证

  • 两个点y坐标相等时:other.isAbove(this)返回false,isUnder返回false,符合预期
  • 两个点x坐标相等时:other.isLeft(this)返回false,isRight返回false,符合预期
  • 所有正常高低、左右判断的场景均和原有逻辑兼容

内容的提问来源于stack exchange,提问作者Tim Asher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 02:45:04