{"id":1106,"date":"2019-10-19T05:54:14","date_gmt":"2019-10-19T05:54:14","guid":{"rendered":"https:\/\/muthu.co\/?p=1106"},"modified":"2021-05-24T02:42:08","modified_gmt":"2021-05-24T02:42:08","slug":"using-the-law-of-cosines-and-vector-dot-product-formula-to-find-the-angle-between-three-points","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/using-the-law-of-cosines-and-vector-dot-product-formula-to-find-the-angle-between-three-points\/","title":{"rendered":"Using the law of cosines and vector dot product formula to find the angle between three points"},"content":{"rendered":"\n
For any 3 points A, B, and C on a cartesian plane. If we have to find the angle between these points, there are many ways we can do that. In this article I will talk about the two frequently used methods:<\/p>\n\n\n\n
For any given triangle ABC with sides AB, BC and AC, the angle formed by the lines AB and BC is given by the formula:<\/p>\n\n\n\n
Here is how we can derive that formula:<\/p>\n\n\n\n
The same can be extended for other angles as well.<\/p>\n\n\n\n
Now, if we were given 3 points on a cartesian plane, we can find the distance between any two points using the Euclidean distance formula:<\/p>\n\n\n\n
So, to find the angle between three points A(x1,y1),<\/em> B(x2,y2)<\/em> and C(x3,y3), <\/em>our formula becomes:<\/p>\n\n\n\n In Python we can represent the above formula using the code:<\/p>\n\n\n\n The three points can also be represented as a vector as shown below.<\/p>\n\n\n\n where,<\/p>\n\n\n\n The scalar product or the dot product of two vectors is represented by the formula:<\/p>\n\n\n\n where || * || is the magnitude of the vector and \u03b8 is the angle made by the two vectors.<\/p>\n\n\n\n From the above formula we can represent the angle using the formula:<\/p>\n\n\n\n In Python we can represent the above formula using the code:<\/p>\n\n\n\n The above is code from my post<\/a> where I talked about finding the collinearity of three points on a plane using the angle formed by the points.<\/p>\n\n\n\n References:<\/p>\n\n\n\n For any 3 points A, B, and C on a cartesian plane. If we have to find the angle between these points, there are many ways we can do that. In this article I will talk about the two frequently used methods: The Law of Cosines formula Vector Dot product formula Law of Cosines For […]<\/p>\n","protected":false},"author":1,"featured_media":1158,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[54],"class_list":["post-1106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mathematics","tag-mathematics"],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=1106"}],"version-history":[{"count":3,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"predecessor-version":[{"id":1859,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1106\/revisions\/1859"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/1158"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/a><\/figure><\/div>\n\n\n\n
import numpy as np\nimport math\n\ndef angle_between_three_points(pointA, pointB, pointC):\n \n x1x2s = math.pow((pointA[0] - pointB[0]),2)\n x1x3s = math.pow((pointA[0] - pointC[0]),2)\n x2x3s = math.pow((pointB[0] - pointC[0]),2)\n \n y1y2s = math.pow((pointA[1] - pointB[1]),2)\n y1y3s = math.pow((pointA[1] - pointC[1]),2)\n y2y3s = math.pow((pointB[1] - pointC[1]),2)\n\n cosine_angle = np.arccos((x1x2s + y1y2s + x2x3s + y2y3s - x1x3s - y1y3s)\/(2*math.sqrt(x1x2s + y1y2s)*math.sqrt(x2x3s + y2y3s)))\n\n return np.degrees(cosine_angle)\n\nA = np.array([2,4])\nB = np.array([8,7])\nC = np.array([9,1])\n\nprint(\"Angle between points:\", angle_between_three_points(A,B,C))\n# gives 72.89\n\nA = np.array([0,0])\nB = np.array([0,4])\nC = np.array([4,0])\n\n\nprint(\"Angle between points:\", angle_between_three_points(A,B,C))\n# gives 45.0<\/code><\/pre>\n\n\n\n
Vector Dot Product<\/h2>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
def angle_between_three_points(pointA, pointB, pointC):\n BA = pointA - pointB\n BC = pointC - pointB\n\n try:\n cosine_angle = np.dot(BA, BC) \/ (np.linalg.norm(BA) * np.linalg.norm(BC))\n angle = np.arccos(cosine_angle)\n except:\n print(\"exc\")\n raise Exception('invalid cosine')\n\n return np.degrees(angle)<\/code><\/pre>\n\n\n\n