{"id":25963,"date":"2017-10-25T22:00:46","date_gmt":"2017-10-25T16:30:46","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25963"},"modified":"2017-10-25T22:00:46","modified_gmt":"2017-10-25T16:30:46","slug":"check-given-four-points-form-square","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-given-four-points-form-square\/","title":{"rendered":"How to check if given four points form a square"},"content":{"rendered":"<p>Given coordinates of four points in a plane, find if the four points form a square or not.<span id=\"more-142567\"><\/span><br \/>\nTo check for square, we need to check for following.<br \/>\na) All fours sides formed by points are same.<br \/>\nb) The angle between any two sides is 90 degree. (This condition is required as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Quadrilateral\">Quadrilateral <\/a>also has same sides.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25971\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/square.png\" alt=\"How to check if given four points form a square\" width=\"312\" height=\"225\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/square.png 312w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/square-300x216.png 300w\" sizes=\"(max-width: 312px) 100vw, 312px\" \/><\/p>\n<p>The idea is to pick any point and calculate its distance from rest of the points. Let the picked picked point be \u2018p\u2019. To form a square, distance of two points must be same from \u2018p\u2019, let this distance be d. The distance from one point must be different from that d and must be equal to \u221a2 times d. Let this point with different distance be \u2018q\u2019.<br \/>\nThe above condition is not good enough as the the point with different distance can be on the other side. We also need to check that q is at same distance from 2 other points and this distance is same as d.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Below is C++ implementation of above idea.<\/p>\n<div>\n<div id=\"highlighter_613668\" class=\"syntaxhighlighter nogutter cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp comments\">\/\/ A C++ program to check if four given points form a square or not.<\/code><\/div>\n<div class=\"line number1 index0 alt2\"><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%23include%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Structure%20of%20a%20point%20in%202D%20space%0Astruct%20Point%0A%7B%0A%20%20%20%20int%20x%2C%20y%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20square%20of%20distance%0A%2F%2F%20from%20point%20\u2019p\u2019%20to%20poitn%20\u2019q\u2019%0Aint%20distSq(Point%20p%2C%20Point%20q)%0A%7B%0A%20%20%20%20return%20(p.x%20-%20q.x)*(p.x%20-%20q.x)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20(p.y%20-%20q.y)*(p.y%20-%20q.y)%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20(p1%2C%20p2%2C%20p3%2C%20p4)%20form%20a%0A%2F%2F%20square%2C%20otherwise%20false%0Abool%20isSquare(Point%20p1%2C%20Point%20p2%2C%20Point%20p3%2C%20Point%20p4)%0A%7B%0A%20%20%20%20int%20d2%20%3D%20distSq(p1%2C%20p2)%3B%20%20%2F%2F%20from%20p1%20to%20p2%0A%20%20%20%20int%20d3%20%3D%20distSq(p1%2C%20p3)%3B%20%20%2F%2F%20from%20p1%20to%20p3%0A%20%20%20%20int%20d4%20%3D%20distSq(p1%2C%20p4)%3B%20%20%2F%2F%20from%20p1%20to%20p4%0A%20%0A%20%20%20%20%2F%2F%20If%20lengths%20if%20(p1%2C%20p2)%20and%20(p1%2C%20p3)%20are%20same%2C%20then%0A%20%20%20%20%2F%2F%20following%20conditions%20must%20met%20to%20form%20a%20square.%0A%20%20%20%20%2F%2F%201)%20Square%20of%20length%20of%20(p1%2C%20p4)%20is%20same%20as%20twice%0A%20%20%20%20%2F%2F%20%20%20%20the%20square%20of%20(p1%2C%20p2)%0A%20%20%20%20%2F%2F%202)%20p4%20is%20at%20same%20distance%20from%20p2%20and%20p3%0A%20%20%20%20if%20(d2%20%3D%3D%20d3%20%26%26%202*d2%20%3D%3D%20d4)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p4)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p3%2C%20p4)%20%26%26%20d%20%3D%3D%20d2)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20below%20two%20cases%20are%20similar%20to%20above%20case%0A%20%20%20%20if%20(d3%20%3D%3D%20d4%20%26%26%202*d3%20%3D%3D%20d2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p3)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p2%2C%20p4)%20%26%26%20d%20%3D%3D%20d3)%3B%0A%20%20%20%20%7D%0A%20%20%20%20if%20(d2%20%3D%3D%20d4%20%26%26%202*d2%20%3D%3D%20d3)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20d%20%3D%20distSq(p2%2C%20p3)%3B%0A%20%20%20%20%20%20%20%20return%20(d%20%3D%3D%20distSq(p3%2C%20p4)%20%26%26%20d%20%3D%3D%20d2)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20Point%20p1%20%3D%20%7B20%2C%2010%7D%2C%20p2%20%3D%20%7B10%2C%2020%7D%2C%0A%20%20%20%20%20%20%20%20%20%20p3%20%3D%20%7B20%2C%2020%7D%2C%20p4%20%3D%20%7B10%2C%2010%7D%3B%0A%20%20%20%20isSquare(p1%2C%20p2%2C%20p3%2C%20p4)%3F%20cout%20%3C%3C%20%22Yes%22%3A%20cout%20%3C%3C%20%22No%22%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<\/div>\n<\/div>\n<p>Output:<\/p>\n<pre>Yes<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>How to check if given four points form a square-Geometric Algorithm Given coordinates of four points in a plane, find if the four points form square or not.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73905],"tags":[76353,76358,76356,76354,76359,76357,76355,76360],"class_list":["post-25963","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-geometric-algorithms","tag-check-if-four-points-form-a-rectangle","tag-check-if-four-points-form-a-square-java","tag-check-if-given-four-points-form-a-square-geeksforgeeks","tag-check-if-given-four-points-form-a-square-java","tag-check-if-given-four-points-form-a-square-maths","tag-how-to-check-if-given-four-points-form-a-square-c","tag-identify-whether-four-sides-can-form-a-square-a-rectangle-or-neither","tag-show-that-the-4-points-form-a-square"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25963"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25963\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}