当前位置:网站首页>The effective square of the test (one question of the day 7/29)
The effective square of the test (one question of the day 7/29)
2022-08-03 19:05:00 【Lanzhou Qianfan】
Effective squares for brushing questions (Daily Question 7/29)
I am using complete computational brute force for this question.
The subject address is as follows link
The subject requirements are as follows
Given the coordinates p1, p2, p3 and p4 of four points in 2D space, returns true if the four points form a square.The coordinates pi of a point are represented as [xi, yi] .The inputs are not given in any order.A valid square has four equal sides and four equal angles (90-degree angles).Source: LeetCode
This is a basic requirement.Actually!I didn't use the prompt, because I used a completely pure mathematical calculation and used the characteristics of coordinates.More violent, the code is fast and large, but the efficiency is high.
Below is my code
class Solution {public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {boolean result = judge_finally(p1,p2,p3,p4);return result;}public static boolean judge_finally(int[]p1,int[]p2,int[]p3,int[]p4) {double v_1_2 = judge_rhomb(p1, p2);//Some possible distances are calculated heredouble v2_3 = judge_rhomb(p2, p3);double v3_4 = judge_rhomb(p3, p4);double v1_4 = judge_rhomb(p1, p4);double v_2_4 = judge_rhomb(p2, p4);double v_1_3 = judge_rhomb(p1, p3);//Here lists the possible diagonals, and makes a judgment of the diagonals, including the coincidence of the midpoints of the four-point diagonals, and confirms the quadrilateral//Equal distance and diagonal vertical constraint.This progressively constrains it to a square//Pay attention to the constraint of the zero point coordinate, so a constraint must be made, here the diagonal distance is constrained to not be 0if (v_1_3 == v_2_4 && v_1_3 != 0&&judge_vertical(p1, p3, p2, p4)&judge_if_line(p1,p3,p2,p4)) {return true;}if (v1_4 == v2_3 && v1_4 != 0&&judge_vertical(p1, p4, p2, p3)&judge_if_line(p1,p4,p2,p3)) {return true;}if (v_1_2 == v3_4 && v_1_2 != 0&&judge_vertical(p1, p2, p3, p4)&judge_if_line(p1,p2,p3,p4)) {return true;}return false;}public static double judge_rhomb(int[]a,int[]b)//Calculate the length of the line formed by the two coordinates{double x1 = ( Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));return x1;}public static boolean judge_vertical(int[]a,int[]b,int[]c,int[]d)//Judgment right angle{int v[] = {a[0]-b[0],a[1]-b[1]};int v1[] = {c[0]-d[0],c[1]-d[1]};return v[0]*v1[0]+v[1]*v1[1]==0;}public static boolean judge_if_line(int[]a,int[]b,int[]c,int[]d)//Judge whether four points can form a parallelogram//If four points can form a parallelogram, the midpoints of the diagonals coincide{boolean bool_line_ = a[0] + b[0] == c[0] + d[0];boolean bool_line__ = a[1]+b[1]==c[1]+d[1];if (bool_line_&&bool_line__){return true;}return false;}}It boils down to the method, a very basic method call judgment.The vertical here uses the characteristics of the vector.x1x2+y1y2=0, so you can judge whether it is vertical or not.To judge whether the four coordinate points constitute a parallelogram, it is necessary to judge whether the midpoints of the diagonals of the possible cases coincide, so here it is only necessary to calculate whether the abscissa and ordinate coordinates are equal respectively.The diagonals of a rhombus are perpendicular to each other, and we can judge it as a square as long as the diagonals are equal.But be careful to do these given coordinate constraints.So I gave the diagonal length not 0.Because if the horizontal and vertical coordinates of the four points are all 0, the above requirements can actually be satisfied.
Fully diagonal constraints.
边栏推荐
猜你喜欢
随机推荐
不要小看 WebSocket!长连接、有状态、双向、全双工都是王炸技能
MySQL读写分离的三种实现方案
X86函数调用模型分析
MVC vs MVP
[Notes] Introduction to machine learning
微信小程序分享功能
阿里巴巴政委体系-第五章、阿里政委体系建设
Compose原理-compose中是如何实现事件分法的
docker mysql 容器中执行mysql脚本文件并解决乱码
dd命令:用于读取、转换并输出数据
【QT】入门心法
选出表中的中位数记录[构造左右边界 || 问题转换]
LeetCode 622. 设计循环队列
Postgresql中的pg_memory_barrier_impl和C的volatile
2022/08/02------Ugly number
力扣刷题之合并两个有序数组
JumpServer开源堡垒机完成龙芯架构兼容性认证
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
MYSQL误删数据恢复
学弟:我适不适合转行做软件测试?
![选出表中的中位数记录[构造左右边界 || 问题转换]](/img/02/8d8e515c994c8a1a364f1e299d73f7.png)








