15.理論知識:直線

15.Theory: Lines

This post is a bit of background on lines, which we’ll be using in the next post to finish offour bus scenario.

這篇帖子是關於直線的一些背景知識,我們將在下篇帖子裏用其來完成我們的巴士劇本

Mathematics typically only deals with lines that are infinite in length. However, in programming we more often deal with lines that are finite, stretching from one endpoint to another. There are several different ways to represent infinite and finite lines, which we’ll look at in turn.

數學中只是典型地將直線當做無限長度來處理。然而,在編程中我們通常處理的是從一點延伸到另一點的有限直線。這兒有一些不同的方式來表示有限和無限直線,我們將輪流介紹。

Infinite line: y = mx + c

無限直線

You may well have come across the equation “y = m \times x + c” before. You can look at it in two ways. One is that it is a way to calculate y, given a value for x (and thus is a way to draw the line). The other is that it is a relation that holds true for points on the line, and is false otherwise. So let’s say you havey = 3 \times x + 2. If you plug inx = 1, y = 5 then you will find the equation is true: the point (1, 5) is on the line. But if you plug inx = 1, y = 6 then you will find the equation is false: the point (1, 6) is not on the line.

你以前很可能接觸過方程式“y = m \times x + c”。你可以用兩種觀點看待它。其一在於它是一種根據給定的x值計算y值的方式(因而這是一種繪製直線的方式)。其二在於它表示一種關係,如果點在直線上爲真,否則爲假。因此假設你有y = 3 \times x + 2。如果你帶入x = 1, y = 5於是你會發現等式爲真:點(1,5)在直線上。但是如果你帶入x = 1, y = 6於是你會發現等式爲假:點(1,6)不在直線上。

This form is usually used for straight lines on graphs because it expresses the relation between x and y. However, it has one major weakness: you can’t express a vertical line in this form, which only exists at one value of x. This makes it unsuitable for programming purposes, because you don’t want to have to program a special case for when a line happens to be vertical.

這種形式經常使用於圖表中的直線因爲它表達了x和y的關係。然而,它有一個主要的弱點:在這樣形式下你不能表達一條垂線,而它只存在一個x值。這使得它不適合用於編程,因爲你不會希望爲一條直線碰巧是直線的特殊情形編寫程序。

Infinite line: point and direction

無限直線:點和方向

Another way to express a line is as a combination of a point on the line (as a vector), and a direction. For example, a point on the line might be (0, 2), and the direction might be (1, 3). In mathematics it is often useful to combine these two into an expression which represents all points on the line. This is done by introducing a variable, which I’ll call scalar, to represent a form of distance along the line:

另一種表示直線的方式是將其看做直線上的點(作爲向量)和方向的集合體。比如,直線上的一個點可能是(0,2),方向可能是(1,3)。在數學上將這兩者組合爲一個代表直線上所有點的表達式常常是有用的。這通過引入一個變量來實現,我將其稱爲scalar,用來表示沿着直線的距離。

\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} 0 \\ 2 \end{pmatrix} + \text{scalar} \times \begin{pmatrix} 1 \\ 3 \end{pmatrix}

The idea here is that all points on the line have a value for the scalar variable. So for example, (1.5, 6.5) is on the line: use\text{scalar}=1.5 and you get:

這兒的原理是直線上得所有點都對應一個scalar變量的值,(1.5,6.5)在直線上:使用\text{scalar}=1.5同時你將得到:

\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} 0 \\ 2 \end{pmatrix} + 1.5 \times \begin{pmatrix} 1 \\ 3 \end{pmatrix}

\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} 0 \\ 2 \end{pmatrix} + \begin{pmatrix} 1.5 \\ 4.5 \end{pmatrix}

\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} 1.5 \\ 6.5 \end{pmatrix}

However, there’s no such value of  scalar for the point (2, 9), because it’s not on the line. Two slightly unexpected features of this representation are that it doesn’t matter which point on the line you pick for the expression, and you can multiply the direction vector by any constant without it changing the line. So the above line is equivalent to either of the following:

然而,這兒對於點(2,9)則沒有這樣的scalar值,因爲它不在直線上。這種表示方式有兩個稍微意想不到的特徵是:你爲表達式中挑選哪個起點並沒有關係,以及你可以用任意常數乘以方向向量而不會改變直線。於是上面的直線等同於下面這種形式:

\begin{pmatrix} 1 \\ 5 \end{pmatrix} + \text{scalarB} \times \begin{pmatrix} -1 \\ -3 \end{pmatrix}

\begin{pmatrix} -10 \\ -28 \end{pmatrix} + \text{scalarC} \times \begin{pmatrix} 6 \\ 18 \end{pmatrix}

Finite line: two endpoints

有限直線:兩個端點

The simplest way to represent a finite line is to simply write down its two endpoints. So you might have a line from (1, 3) to (6, 7). That’s a good, simple, unambiguous definition of the line. However, to do any calculations involving the line, you usually need to convert it to another representation, such as the last on our list:

表示有限直線的最簡單的方式便是簡單地寫下它的兩個端點。於是你可能獲得一條從(1,3)到(6,7)的直線。那很好,很簡單,很符合直線的定義。然而,爲了處理涉及到直線的一些計算,你往往需要將其轉換爲另一種形式,正如我們最後所列出的:

Finite line: point and direction

有限直線:點和方向

You can take the two endpoints of a line and turn it into a form that looks identical to our earlier point and direction form. So using our (1, 3) to (6, 7) example, you get:

你可以獲取直線的兩個端點並將其轉換爲我們早前看到的點和方向的形式。於是使用(1,3)到(6,7)的例子,你可得到:

\begin{pmatrix} 1 \\ 3 \end{pmatrix} + \text{scalar} \times \begin{pmatrix} 5 \\ 4 \end{pmatrix}

The difference now is that the line extends from \text{scalar}=0 (the start-point) to\text{scalar}=1 (the end-point). Any values for the scalar variable outside the 0–1 range would be on the line if it’s infinite, but are not when it’s finite.

現在的區別在於該直線從\text{scalar}=0(起點)延伸至\text{scalar}=1(終點)。如果它是無限的scalar變量則可以是任何超過0-1範圍的值,但是當它是有限時則不可以。

End of the Line

總結直線

Today’s post was a lot of theory without an actual application, but we’ll be using it in our next post when we’ll look at finding out if two finite lines intersect.

今天的帖子有很多理論知識而沒有實際的應用程序,但是我們將在下一篇帖子裏使用,我們將看一下如何判斷兩條有限直線是否相交。

發佈了0 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章