我们将得到一个带有四个点的正方形,即a、b、c、d,如图所示−
我们需要从这些点来检查它们是否形成一个正方形。为了检查这一点,它应满足以下条件−
点a和点c之间的距离,以及点b和点d之间的距离即“x”应该相等。
点a和点b之间的距离,点b和点c之间的距离,点c和点d之间的距离,点d和点a之间的距离即“z”应该相等。
我们将使用公式找到两点之间的距离 -
$$\mathrm{d=\sqrt{(x_{2}-x_{1})^2(y_{2}-y_{1})^2}}$$
点1将是(x1,y1),点2将是(x2,y2)。
让我们开始吧!
展示给你一些实例instance-1 的中文翻译为:实例-1给定四个输入点为 -
p1(3,7), p2(4,3), p3(7,8), p4(1,9)
将其放入距离公式中并检查是否满足平方条件,结果将为 -
给定四个点不构成一个正方形。
实例-2给定四个输入点为 -
p1(20,20), p2(20,10), p3(10,10), p4(10,20)
将其放入距离公式中并检查是否满足平方条件,结果将为 -
给定四个点形成一个正方形。
算法步骤-1 − 声明并初始化变量。
步骤-2 − 找到圆的中心1和中心2之间的距离。
第三步 - 检查五个距离条件。
步骤-4 − 打印结果。
多种方法我们以不同的方式提供了解决方案。
通过使用静态输入
通过使用用户定义的方法
让我们逐个查看程序及其输出。
方法一:使用静态输入在这种方法中,将分配点值。然后根据算法,我们将找出给定的四个点是否形成一个正方形。
example的中文翻译为:示例public class main{ //main method public static void main(string[] args){ //declaring variables int x1=3, x2=4, x3=7, x4=1; int y1=7, y2=3, y3=8, y4=9; double d1, d2, d3, d4, d5, d6; //applyinng logic d1 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); d2 = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2); d3 = (x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3); d4 = (x1 - x4) * (x1 - x4) + (y1 - y4) * (y1 - y4); d5 = (x4 - x2) * (x4 - x2) + (y4 - y2) * (y4 - y2); d6 = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1); if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0 || d5 == 0 || d6 == 0){ system.out.println(given four points do not form a square); } else if (d1 == d2 && d2 == d3 && d3 == d4 && d5 == d6){ //prints if four points form square system.out.println(given four points form a square); } else { //prints if four points do not form square system.out.println(given four points do not form a square); } }}
输出given four points do not form a square
方法二:使用用户定义的方法在这种方法中,将分配点值。然后通过传递给定的值调用一个用户定义的方法,并根据算法判断给定的四个点是否形成一个正方形。
example的中文翻译为:示例public class main{ //main method public static void main(string[] args){ //creating objects of point point p1 = new point(20, 20); point p2 = new point( 20, 10 ); point p3 = new point(10, 10 ); point p4 = new point( 10, 20 ); //calling user defined method if(issquare(p1, p2, p3, p4)==true){ //print if four points form a square system.out.println(given four points form a square); } else{ //print if points does not form a square system.out.println(given four points do not form a square); } } // declaring point class static class point{ int x, y; public point(int x, int y){ this.x = x; this.y = y; } }; //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); } //user defined method static boolean issquare(point p1, point p2, point p3, point p4){ int d1 = distsq(p1, p2); int d2 = distsq(p2, p3); int d3 = distsq(p3, p4); int d4 = distsq(p4, p1); int d5 = distsq(p1, p3); int d6 = distsq(p2, p4); if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0 || d5 == 0 || d6 == 0) return false; if (d1 == d2 && d2 == d3 && d3 == d4 && d5 == d6){ //it returns true if (p1, p2, p3, p4) form a square return true; } //it returns false if (p1, p2, p3, p4) do not form a square return false; }}
输出given four points form a square
在这篇文章中,我们使用java编程语言探讨了不同的方法来检查一条线是否触碰、相交或位于圆外。
以上就是如何在java中确认给定的四个点是否形成一个正方形?的详细内容。
