{"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\" target=\"_blank\" rel=\"noopener\">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=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include&lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Structure of a point in 2D space<br\/>struct Point<br\/>{<br\/>    int x, y;<br\/>};<br\/> <br\/>\/\/ A utility function to find square of distance<br\/>\/\/ from point &#039;p&#039; to poitn &#039;q&#039;<br\/>int distSq(Point p, Point q)<br\/>{<br\/>    return (p.x - q.x)*(p.x - q.x) +<br\/>           (p.y - q.y)*(p.y - q.y);<br\/>}<br\/> <br\/>\/\/ This function returns true if (p1, p2, p3, p4) form a<br\/>\/\/ square, otherwise false<br\/>bool isSquare(Point p1, Point p2, Point p3, Point p4)<br\/>{<br\/>    int d2 = distSq(p1, p2);  \/\/ from p1 to p2<br\/>    int d3 = distSq(p1, p3);  \/\/ from p1 to p3<br\/>    int d4 = distSq(p1, p4);  \/\/ from p1 to p4<br\/> <br\/>    \/\/ If lengths if (p1, p2) and (p1, p3) are same, then<br\/>    \/\/ following conditions must met to form a square.<br\/>    \/\/ 1) Square of length of (p1, p4) is same as twice<br\/>    \/\/    the square of (p1, p2)<br\/>    \/\/ 2) p4 is at same distance from p2 and p3<br\/>    if (d2 == d3 &amp;&amp; 2*d2 == d4)<br\/>    {<br\/>        int d = distSq(p2, p4);<br\/>        return (d == distSq(p3, p4) &amp;&amp; d == d2);<br\/>    }<br\/> <br\/>    \/\/ The below two cases are similar to above case<br\/>    if (d3 == d4 &amp;&amp; 2*d3 == d2)<br\/>    {<br\/>        int d = distSq(p2, p3);<br\/>        return (d == distSq(p2, p4) &amp;&amp; d == d3);<br\/>    }<br\/>    if (d2 == d4 &amp;&amp; 2*d2 == d3)<br\/>    {<br\/>        int d = distSq(p2, p3);<br\/>        return (d == distSq(p3, p4) &amp;&amp; d == d2);<br\/>    }<br\/> <br\/>    return false;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    Point p1 = {20, 10}, p2 = {10, 20},<br\/>          p3 = {20, 20}, p4 = {10, 10};<br\/>    isSquare(p1, p2, p3, p4)? cout &lt;&lt; &quot;Yes&quot;: cout &lt;&lt; &quot;No&quot;;<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<p>Output:<\/p>\n<pre>Yes<\/pre>\n[ad type=&#8221;banner&#8221;]\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}]}}