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 “” 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 have. If you plug in then you will find the equation is true: the point (1, 5) is on the line. But if you plug in then you will find the equation is false: the point (1, 6) is not on the line.
你以前很可能接触过方程式“”。你可以用两种观点看待它。其一在于它是一种根据给定的x值计算y值的方式(因而这是一种绘制直线的方式)。其二在于它表示一种关系,如果点在直线上为真,否则为假。因此假设你有。如果你带入于是你会发现等式为真:点(1,5)在直线上。但是如果你带入于是你会发现等式为假:点(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,用来表示沿着直线的距离。
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 and you get:
这儿的原理是直线上得所有点都对应一个scalar变量的值,(1.5,6.5)在直线上:使用同时你将得到:
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值,因为它不在直线上。这种表示方式有两个稍微意想不到的特征是:你为表达式中挑选哪个起点并没有关系,以及你可以用任意常数乘以方向向量而不会改变直线。于是上面的直线等同于下面这种形式:
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)的例子,你可得到:
The difference now is that the line extends from (the start-point) to (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.
现在的区别在于该直线从(起点)延伸至(终点)。如果它是无限的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.
今天的帖子有很多理论知识而没有实际的应用程序,但是我们将在下一篇帖子里使用,我们将看一下如何判断两条有限直线是否相交。