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

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
S
SegmentFault 最新的问题
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
U
Unit 42
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - Franky
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research

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