惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
G
Google Developers Blog
L
LangChain Blog
腾讯CDC
大猫的无限游戏
大猫的无限游戏
U
Unit 42
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
The GitHub Blog
The GitHub Blog
博客园_首页
GbyAI
GbyAI

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Point to Line Distance in 2D Plane
2020-02-23 · via jdhao's digital space

Suppose that we have a straight line formed by point B and C, and we have another point A. What is the distance from point A to line BC?

There are various ways to calculate this distance. Using vector calculus is one of the easiest ways to do it.

From elementary math, we know that distance from A to line BC is the distance between point A and D, where D is a point in line BC and line AD is perpendicular to line BC.

So the problem really boils down to finding the distance between between point A and D. That is, we need to find the value of following equations:

\[\begin{equation} \Vert \overrightarrow{AD} \Vert = \sqrt{(D_x - A_x)^2 + (D_y - A_y)^2} \end{equation}\]

Since point D is in line BC, so vector \(\overrightarrow{BD}\) and vector \(\overrightarrow{DC}\) is parallel to each other, i.e., \(\overrightarrow{BD} = t\cdot \overrightarrow{DC}\). We also know that \(\overrightarrow{AD}\) is perpendicular to \(\overrightarrow{BC}\). From these two conditions, we have the following two equations:

\[\begin{gather} \frac{D_x - B_x}{C_x - D_x} = \frac{D_y - B_y}{C_y - D_y} \label{eq2} \\ (D_x - A_x)(C_x - B_x) + (D_y - A_y)(C_y - B_y) = 0 \label{eq3} \end{gather}\]

Simplifying \(\eqref{eq2}\), we get:

\[\begin{equation} (D_x - B_x)(C_y - D_y) - (D_y - B_y)(C_x - D_x) = 0 \label{eq4} \end{equation}\]

Transforming \(\eqref{eq4}\) a bit, and we get:

\[\begin{equation} \begin{split} [(D_x - A_x) + (A_x - B_x)][(A_y - D_y) + (C_y - A_y)] -\\ [(D_y-A_y) + (A_y-B_y)][(A_x-D_x) + (C_x - A_x)] = 0 \end{split}\label{eq5} \end{equation}\]

Now, to simplify calculation, we use the following substitution:

\[\begin{align} D_x - A_x &= m \label{eq6} \\ D_y - A_y &= n \label{eq7} \end{align}\]

Substitute m and n into equation \(\eqref{eq3}\) and \(\eqref{eq5}\), and simplifying a bit, we now get:

\[\begin{gather} m(C_x-B_x) + n(C_y-B_y) = 0 \label{eq8} \\ m(C_y-B_y) - n(C_x - B_x) = (A_y - B_y)(C_x-A_x) - (A_x - B_x)(C_y-A_y) \label{eq9} \end{gather}\]

From equation \(\eqref{eq8}\) and \(\eqref{eq9}\), it it now easy to derive the value of \(m\) and \(n\):

\[\begin{align} m &= - \frac{(C_y - B_y)[A_y(B_x - C_x) - B_y(A_x - C_x) + C_y(A_x - B_x)]}{(C_x - B_x)^2 + (C_y - B_y)^2}\\ n &= \frac{(C_x - B_x)[A_y(B_x - C_x) - B_y(A_x - C_x) + C_y(A_x - B_x)]}{(C_x - B_x)^2 + (C_y - B_y)^2}\\ \end{align}\]

So the square sum of \(m\) and \(n\) is:

\[\begin{equation} m^2 + n^2 = \frac{[A_y(B_x - C_x) - B_y(A_x - C_x), + C_y(A_x - B_x)]^2}{(C_x - B_x)^2 + (C_y - B_y)^2} \end{equation}\]

The norm of vector \(\overrightarrow{AD}\) is:

\[\begin{equation} \Vert \overrightarrow{AD} \Vert = \frac{\lVert A_y(B_x - C_x) - B_y(A_x - C_x), + C_y(A_x - B_x)\rVert}{\sqrt{(C_x - B_x)^2 + (C_y - B_y)^2}} \end{equation}\]

In the above equation, the denominator is the norm of vector \(\overrightarrow{BC}\), the nominator is the norm of cross product between \(\overrightarrow{BC}\) and \(\overrightarrow{BA}\).

So the distance from point A to line BC can also be written as

\[\begin{equation} \lVert \overrightarrow{AD} \rVert = \frac{\lVert \overrightarrow{BC} \times \overrightarrow{BA} \rVert}{\lVert \overrightarrow{BC} \rVert} \end{equation}\]

References

  • https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
  • https://stackoverflow.com/q/39840030/6064933
  • https://math.stackexchange.com/questions/330269/the-distance-from-a-point-to-a-line-segment