当前位置:网站首页>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.
边栏推荐
- 阿里资深专家打造从零开始学架构,含阿里内部技术栈PPT、PFD实战
- 2022年最新的Android面试大厂必考174题(附带详细答案)
- ScrollView嵌套RV,滑动有阻力不顺滑怎么办?
- Execute the mysql script file in the docker mysql container and solve the garbled characters
- Cobalt Strike (CS) 逆向初探
- PreFixSum前缀和
- 图像超分——Real-ESRGAN快速上手
- MYSQL误删数据恢复
- docker mysql 容器中执行mysql脚本文件并解决乱码
- 软件测试回归案例,什么是回归测试?
猜你喜欢
随机推荐
深度学习常用公式与命令总结(更新中)
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
LineSegmentTree线段树
懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w
How does MySQL permanently support Chinese input once and for all?
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?
MySQL如何 drop 大表
机器学习的方法总结
The ecological environmental protection management system based on mobile GIS
YAML中多行字符串的配置方法:|+、 |、 |-、 >+、 >、 >-的区别
BinomialTree 二叉树
Force is brushed buckle problem for the sum of two Numbers
基于移动GIS的环保生态管理系统
国产虚拟化云宏CNware WinStack安装体验-5 开启集群HA
201709-3 CCF jason查询 (满分题解)
Postgresql源码(64)查询执行——子模块Executor(2)执行前的数据结构和执行过程
Mkke:为什么无法从Oracle 11g或12c升级到Oracle 23c?
online 方式创建索引触发trigger怎么办?
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
Compose原理-compose中是如何实现事件分法的