当前位置: 首页 > news >正文

Java 检查给定的四个点是否形成正方形(Check if given four points form a square)

给定平面上四个点的坐标,判断这四个点是否形成正方形。 

例子:

输入: p1 = { 20, 10 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }

输出:是

解释: 

输入: p1 = { 20, 20 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }

输出: 否 

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 

要判断正方形是否为正方形,我们需要检查以下几点。
a ) 由点构成的四条边是否相等。
b ) 任意两条边之间的夹角为 90 度。(此条件是必需的,因为菱形的边也相同。)
c ) 检查两条对角线的距离是否相等

思路是选取任意一点,计算它与其他点的距离。设选取的点为 p。为了构成正方形,两个点到 p 的距离必须相等,设该距离为 d。其中一个点到 p 的距离必须不同于 d,并且必须等于 √2 乘以 d(或者距离平方等于 2 * d ²)。设这个距离不同的点为 q。 

上述条件不够好,因为距离不同的点可能位于另一侧。我们还需要检查 q 与另外两点的距离是否相同,并且该距离是否与 d 相同。

在下面的代码中,为了简单起见,我们将距离计算为 (x1-x2)² + (y1-y2)²。我们不会对其进行平方根运算!

示例代码:

// A Java program to check if four given points form a
// square or not.

class GFG {

    // Structure of a point in 2D space
    static class Point {
        int x, y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    };

    // A utility function to find square of distance
    // from point 'p' to point 'q'
    static int distSq(Point p, Point q)
    {
        return (p.x - q.x) * (p.x - q.x)
            + (p.y - q.y) * (p.y - q.y);
    }

    // This function returns true if (p1, p2, p3, p4) form a
    // square, otherwise false
    static boolean isSquare(Point p1, Point p2, Point p3,
                            Point p4)
    {
        int d2 = distSq(p1, p2); // from p1 to p2
        int d3 = distSq(p1, p3); // from p1 to p3
        int d4 = distSq(p1, p4); // from p1 to p4

        if (d2 == 0 || d3 == 0 || d4 == 0)
            return false;

        // If lengths if (p1, p2) and (p1, p3) are same,
        // then following conditions must met to form a
        // square. 1) Square of length of (p1, p4) is same
        // as twice the square of (p1, p2) 2) Square of
        // length of (p2, p3) is same as twice the square of
        // (p2, p4)

        if (d2 == d3 && 2 * d2 == d4
            && 2 * distSq(p2, p4) == distSq(p2, p3)) {
            return true;
        }

        // The below two cases are similar to above case
        if (d3 == d4 && 2 * d3 == d2
            && 2 * distSq(p3, p2) == distSq(p3, p4)) {
            return true;
        }
        if (d2 == d4 && 2 * d2 == d3
            && 2 * distSq(p2, p3) == distSq(p2, p4)) {
            return true;
        }

        return false;
    }

    // Driver code
    public static void main(String[] args)
    {
        Point p1 = new Point(20, 10),
              p2 = new Point(10, 20),
              p3 = new Point(20, 20),
              p4 = new Point(10, 10);
        System.out.println(isSquare(p1, p2, p3, p4) == true
                               ? "Yes"
                               : "No");
    }
}

输出:

Yes

时间复杂度: O(1),所有操作均在 O(1) 常数时间内完成。

辅助空间: O(1),无需额外空间。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

http://www.dtcms.com/a/338780.html

相关文章:

  • Springboot 3 上传图片,并返回路径让前端显示图片
  • 《UE5_C++多人TPS完整教程》学习笔记43 ——《P44 奔跑混合空间(Running Blending Space)》
  • 监督分类——最小距离分类、最大似然分类、支持向量机
  • Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
  • BROADCHIP广芯电子在各类电子产品的方案与应用
  • 3、栈和队列
  • Ansible 配置并行 - 项目管理笔记
  • 零知开源——基于STM32F407VET6与GY-271三轴地磁传感器的高精度电子罗盘设计与实现
  • TensorFlow 面试题及详细答案 120道(11-20)-- 操作与数据处理
  • Auto-CoT:大型语言模型的自动化思维链提示技术
  • OpenCV快速入门(C++版)
  • 常见的 Bash 命令及简单脚本
  • 从 0 到 1 开发校园二手交易系统:飞算 JavaAI 全流程实战
  • 无畏契约手游上线!手机远控模拟器畅玩、抢先注册稀有ID!
  • Windows/Centos 7下搭建Apache服务器
  • MySQL-分库分表(Mycat)
  • 第一章 认识单片机
  • 出现了常规系统错误: Unable to push signed certificate to host 192.168.1.2
  • 从数据表到退磁:Ansys Maxwell中N48磁体磁化指南
  • LINUX 软件编程 -- 线程
  • 决策树的学习(二)
  • MCP(模型上下文协议):是否是 AI 基础设施中缺失的标准?
  • jsPDF 不同屏幕尺寸 生成的pdf不一致,怎么解决
  • Ansible 中的文件包含与导入机制
  • java17学习笔记-Deprecate the Applet API for Removal
  • C语言基础:(十八)C语言内存函数
  • 连接远程服务器上的 jupyter notebook,解放本地电脑
  • 计算机毕设推荐:痴呆症预测可视化系统Hadoop+Spark+Vue技术栈详解
  • 生成式AI的能力边界与职业重构:从“百科实习生“到人机协作增强器
  • 人工智能学派简介