{"id":43,"date":"2013-01-14T15:20:49","date_gmt":"2013-01-14T23:20:49","guid":{"rendered":"http:\/\/willclausen.com\/?p=43"},"modified":"2013-01-14T15:29:32","modified_gmt":"2013-01-14T23:29:32","slug":"solution-four-points-determine-a-square-jan-2-2013","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=43","title":{"rendered":"Solution: Four Points Determine a Square, Jan. 2, 2013"},"content":{"rendered":"<p>Here is my solution to the Programming Praxis exercise from Jan. 2, 2013. The solution is written in C++.<\/p>\n<p>The <a href=\"http:\/\/programmingpraxis.com\/2013\/01\/02\/four-points-determine-a-square\/\" target=\"_blank\">task<\/a>:<\/p>\n<p style=\"padding-left: 30px;\">Your task is to write a function that determines if four input points determine a square.<\/p>\n<p>My solution:<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n\/\/ Author: William Clausen\r\n\/\/\r\n\/\/ Date: 1\/2\/13\r\n\/\/\r\n\/\/ Programming Praxis Problem for January 2, 2013: Four points\r\n\/\/ determine a square. Write a progam to determine if four points\r\n\/\/ make up a square in the x-y plane.\r\n\r\n#include &lt;utility&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n\r\nusing namespace std;\r\n\r\ndouble dist(pair &lt;double, double&gt; point1, pair &lt;double, double&gt; point2);\r\n\r\nbool isSquare(pair &lt;double, double&gt; coordinates&#x5B;4]);\r\n\r\n\/\/ Four points determine a sqaure if picking any pair of points\r\n\/\/ are the same distance apart...and two of the three distances are\r\n\/\/ equal\r\nbool isSquare(pair&lt;double, double&gt; coordinates&#x5B;4])\r\n{\r\n\t\/\/ First find the distances\r\n\tdouble dist12 = dist(coordinates&#x5B;1], coordinates&#x5B;2]);\r\n\tdouble dist34 = dist(coordinates&#x5B;3], coordinates&#x5B;4]);\r\n\r\n\tif (dist12 != dist34) return false;\r\n\r\n\tdouble dist13 = dist(coordinates&#x5B;1], coordinates&#x5B;3]);\r\n\tdouble dist24 = dist(coordinates&#x5B;2], coordinates&#x5B;4]);\r\n\r\n\tif (dist13 != dist24) return false;\r\n\r\n\tdouble dist14 = dist(coordinates&#x5B;1], coordinates&#x5B;4]);\r\n\tdouble dist23 = dist(coordinates&#x5B;2], coordinates&#x5B;3]);\r\n\r\n\tif (dist14 != dist23) return false;\r\n\r\n\t\/\/ If the above checks pass, find the max distance and make sure\r\n \/\/ the two smaller distances are equal.\r\n\tdouble maxDist = max(dist12, max(dist13, dist14));\r\n\r\n\tif (maxDist == dist12) {\r\n\t\treturn dist13 == dist14;\r\n\t} else if (maxDist == dist13) {\r\n\t\treturn dist12 == dist14;\r\n\t} else {\r\n\t\treturn dist12 == dist13;\r\n\t}\r\n}\r\n\r\n\/\/ Helper function to determine the distance between to points in an\r\n\/\/ x-y plane.\r\ndouble dist(pair&lt;double, double&gt; point1, pair&lt;double, double&gt; point2)\r\n{\r\n\t\/\/ The distance between 2 points can be found using the pythagorean\r\n\t\/\/ theorem.\r\n\tdouble xDiff = point2.first - point1.first;\r\n\tdouble yDiff = point2.second - point1.second;\r\n\r\n\tdouble diffSquared = pow(xDiff, 2) + pow(yDiff, 2);\r\n\r\n\treturn sqrt(diffSquared);\r\n}\r\n\r\n\/\/ This code produced the correct output for tests provided in the\r\n\/\/ problem statement.\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is my solution to the Programming Praxis exercise from Jan. 2, 2013. The solution is written in C++. The task: Your task is to write a function that determines if four input points determine a square. My solution: \/\/ Author: William Clausen \/\/ \/\/ Date: 1\/2\/13 \/\/ \/\/ Programming Praxis Problem for January 2, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-programming-praxis"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":4,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":47,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/47"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}