In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how Python calculates the distance from point to line and the angle of intersection between lines". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "Python how to calculate the distance from point to line and the angle of intersection between lines".
Preface
In the project, there will be requirements for the calculation of the distance from a straight line, the coordinate calculation of the intersection of two lines, and the calculation of the angle between the two lines.
Calculation of the distance from a point to a straight line
Because it is the easiest to get the coordinates of the points in the project, it is most clear to use the vector method for all the mathematical calculations. The distance from the point to the straight line is deduced by vector method.
Import numpy as nparray_longi = np.array ([x2-x1, y2-y1]) array_trans = np.array ([x2-line_start_x] Y2-line_start_y]) # calculate an example from a point to a straight line with a vector array_temp = (float (array_trans. Dot (array_longi)) / array_longi.dot (array_longi)) array_temp = array_longi.dot (array_temp) distance = np.sqrt ((array_trans-array_temp). Dot (array_trans-array_temp)) # coordinate calculation of the intersection of two and two lines
General equation method:
The general equation of the line is F (x) = ax + by + c = 0. Suppose that the two points of the straight line are (x0rect y0) and (x1, y1), then we can get a = y0-y1 x0y1 b = x1-x0Powerc = x0y1-line.
So the two lines can be represented as
F0 (x) = a0roomx + b0roomy + c0 = 0
F1 (x) = a1roomx + b1roomy + C1 = 0
Then the point of intersection of the two lines should satisfy
A0roomx + b0roomy + c0 = a1roomx + b1roomy + c1
From this, we can deduce
X = (b0*c1-b1*c0) / D
Y = (a1*c0-a0*c1) / D
D = a0*b1-a1*b0, (when D is 0, the two lines are parallel)
The two are actually the cross product application of the system of even equations.
F0 (x) = a0roomx + b0roomy + c0 = 0
F1 (x) = a1roomx + b1roomy + C1 = 0
I j k
A0 b0 c0
A1 b1 c1
Class Point: def _ _ init__ (self, x, y): self.x = x self.y = yclass Line: def _ _ init__ (self, p1) P2): self.p1 = p1 self.p2 = p2def GetLinePara (line): line.a = line.p1.y-line.p2.y line.b = line.p2.x-line.p1.x line.c = line.p1.x * line.p2.y-ine.p2.x * line.p1.y def GetCrossPoint (L1 L2): GetLinePara (L1) GetLinePara (L2) d = l1.a * l2.c-l2.a * l1.b p = Point () p.x = (l1.b * l2.c-l2.b * l1.c) * 1.0 / dp.y = (l1.c * l2.a-l2.c * l1.a) * 1.0 / dp1 = Point (1,1) p2 = Point (3,3) line1 = Line (p1, p2) p3 = Point (2) 3.1p = Point (3.1,2) line2 = Line (p3, p4) Pc = GetCrossPoint (line1, line2) print (Pc.x, Pc.y) calculation of the angle between three or two lines
The vector method is used to calculate the angle between two straight lines. The general idea is similar to finding the distance from a point to a straight line, but also using the cosine theorem.
Import numpy as npdef GetCrossAngle (L1, L2): arr_0 = np.array ([(l1.p2.x-l1.p1.x), (l1.p2.y-l1.p1.y)]) arr_1 = np.array ([(l2.p2.x-l2.p1.x)) (l2.p2.y-l2.p1.y)]) cos_value = (float (arr_0.dot (arr_1)) / (np.sqrt (arr_0.dot (arr_0)) * np.sqrt (arr_1.dot (arr_1) return np.arccos (cos_value) * (180 / np.pi) angle = GetCrossAngle (line1, line2) # Thank you for your reading The above is the content of "how Python calculates the distance from point to line and the angle of intersection between lines". After the study of this article, I believe you have a deeper understanding of how Python calculates the distance from point to line and the angle of intersection between lines, and the specific use still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.