当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Postgresql-xl全局快照与GTM代码走读(支线)
[Dataset][VOC] Rat dataset voc format 3001 sheets
awk语法-02-运算、数组、格式化输出
【C语言学习笔记(六)】分支与跳转(if、else、continue、break、switch)
阿里二面:多线程间的通信方式有几种?举例说明
力扣刷题之移动零
mysql跨库关联查询(dblink)
【夜莺监控方案】08-监控msyql集群(prometheuse+n9e+mysqld_exporter)
系统太多,多账号互通如何实现?
6000 字+,帮你搞懂互联网架构演变历程!
使用安全浏览器将网页保存为pdf的方法步骤
力扣刷题之分数加减运算(每日一题7/27)
Power button brush the topic of merging two orderly array
Cobalt Strike (CS) 逆向初探
2022年7月国产数据库大事记
【C语言学习笔记(七)】C语言重定向输入与输出
BinomialTree 二叉树
X86 function call model analysis
ROS仿真环境搭建
红日安全内网渗透靶场-VulnStack-1









