Properties of Lines

 <  Day Day Up  >  

Now that we've defined the equation of a line, let's look at its properties a little more closely. One of the most important elements of a line is its slope , or steepness. Figure 1.12 shows a hill (a straight line) that rises steadily at a rate of 50m of vertical "rise" for each 100m of horizontal "run."

Figure 1.12. A hill represented by a straight line.

graphics/01fig12.gif

The steepness of this incline is measured by the ratio of its rise to its run, which in this case is 50/100, or 50%. This same hill can be mathematically represented by the linear equation ½ x y = c , as shown in Figure 1.13.

Figure 1.13. Line ½x “ y = c with slope ½.

graphics/01fig13.gif

Look at the coordinates of points P and Q. As you can see, the line rises at a rate of one vertical unit for every two horizontal units. This means that the line's steepness, or slope , is rise/run = ½. In general, subscript notation is used to name any two points P( x 1 , y 1 ) and Q( x 2 , y 2 ) on a nonvertical line to define slope, which we will denote as m .

graphics/01equ01.gif

It doesn't matter which two points you choose; the slope ratio is always the same for any two points on the same line. Look at the graph of ½ x y = 0, shown in Figure 1.14.

Figure 1.14. Line ½x “ y = 0 with slope ½.

graphics/01fig14.gif

Points P and Q and the origin all lie on the same line. If you use points P and Q to calculate the slope, you get m = (2 “1)/(4 “2) = ½. If you use point P and the origin, you get m = (1 “0)/(2 “0) = ½. Either way, the slope is still ½.

Example 1.5: The Slope Between Two Points

Find the slope between points (1,5) and (“2,0).

Solution

Slope = m = (y 2 “y 1 )/(x 2 “x 1 ) = (0 “5)/(“2 “1) = (“5)/(“3) = 5/3.

Let's take a look at how we would solve this same problem in code, using a function specifically designed to calculate the slope between 2D points:

 // purpose: calculate the slope of a line given 2 points // input:  P1  an array of 2 floats representing point 1 //           P2  an array of 2 floats representing point 2 // output: the slope between our 2 points float slopeBetweenPoints(float *P1, float *P2) {     return (P2[1]  P1[1]) / (P2[0]  P1[0]) ; } 

Example 1.6: The Slope of a Line

Now let's combine this idea of slope with the graph of a line, which was discussed in the preceding section.

Determine the slope of the following two lines, and graph both lines on the same Cartesian coordinate system:

  1. y = ½ x + 1

  2. “3 x + 6 y = “12

Solution

Find two pairs of coordinates for each line, and then use the slope formula.

  1. (0,1) and (4,3) both satisfy y = ½ x + 1.

Using the slope formula, you get m = (3 “1)/(4 “0) = 2/4 = ½.

  1. (0, “2) and (3, “1/2) both satisfy “3 x + 6 y = “12.

Using the slope formula, you get m = ([ “ ½] “ “2)/(3 “0) = ( graphics/01inl03.gif )/3 = ½.

Now that you have two points on each line, you can plot the two points and connect them with a line, as shown in Figure 1.15.

Figure 1.15. A graph of lines a and b.

graphics/01fig15.gif

Look at the graphs of these two equations. They both have slope ½, and they're parallel to each other. That makes sense, doesn't it? If they both rise at the same rate, they must be parallel . In fact, looking at the slope can tell you an awful lot about the graph of a line. If the slope is a negative number, the graph falls as it goes from left to right. If the slope is a positive number (as in Example 1.6), the graph rises as it goes from left to right. If the slope of a line is 0, the graph is a horizontal line. Last, if the denominator of the slope formula is 0, the slope is undefined. When the slope is undefined, the graph is a vertical line. These few rules will help you visualize the graph of a line based on its slope.

Notice that in Example 1.6 the two lines are parallel because their slopes are equal. Another interesting relationship between the two slopes occurs if the lines are perpendicular , or if they intersect at right angles. (Another fancy word for perpendicular is orthogonal , so you might come across that term in programming as well.) The interesting rule is that any time you multiply the slopes of two perpendicular lines, you always get “1. This means that the two slopes must be negative reciprocals of each other.

If two lines are perpendicular, m 1 m 2 = “1, or graphics/01inl01.gif or graphics/01inl02.gif .


A function in code which would encompass this formula would look like this:

 // purpose: to determine the slope of the line perpendicular to // ourselves // input:  slope  the slope of our line // output: the slope of a line perpendicular to us float perpSlope(float slope) {     return 1 / slope; } 

Let's take a look at a useful application of this concept. Say for instance that you are making a game in which it is important to know whether or not a collision occurs at a right angle. Knowing that the slope of a line that is perpendicular to another line is “1 over that slope, then if two lines are perpendicular, the product of their slopes will be “1.

 // purpose: to determine whether two lines are perpendicular // input:  slope1  the slope of the first line //         slope2  the slope of our second line // output: true if the lines are perpendicular, else false bool arePerp(float slope1, float slope2) {     if(slope1 * slope2 == -1)         return true;     return false; } 

Look back at Example 1.6 for a moment. You calculated the slope by finding two points on the line and then using the slope formula. If the equation is in standard form (A x + B y = C), there's a shortcut for finding the slope.

For any line in standard form, A x + B y = C, the slope m = “A/B.


The second equation from Example 1.6 is in standard form: “3 x + 6 y = “12. Because it's in standard form, you can use this new shortcut. The slope = “A/B = “(“3)/6 = ½. That's the same slope you found using two points on the line. Just be careful with the plus and minus signs; it's very easy to lose track of them.

Example 1.7: Finding the Slope of a Line

Find the slope of the line 2 x + y = 5.

Solution

Don't panic because there's no number in front of the y ; it just means that B=1. (If there is no y , B must be 0.)

slope = m = “A/B = “2/1 = “2

This shortcut works with linear equations in standard form, but there are two other forms for the equation of a line that you might find more useful.

Slope-intercept form:

y = mx + b

Point-slope form:

( y y 1 ) = m ( x x 1 ), where ( x 1 , y 1 ) is a point on the line.


Let's look at each one more closely.

The slope-intercept form is probably the most often recalled form of the three. It's used quite frequently in code because the y , or the output, is equal to the rest of the equation, which is the common format for equations in code. You already know that the m represents slope, but you might not have realized that the b represents the y-intercept , or the point where the line crosses the y-axis. This form is convenient for trying to visualize the graph, because you immediately know where it crosses the y-axis, and you also know how steep its incline is from the slope.

NOTE

The y-intercept is the point on the line where x = 0. Similarly, the x-intercept is the point on the line where y = 0.


Interestingly enough, the point-slope form of a linear equation has many benefits over the other two forms. What if you needed to generate an equation based on two points? For example, you might know your current location and the point you want to move to, but you need the equation of a line to take you there. The point-slope form provides an easier way to get that equation, because all you need is a point on the line and the slope. After you compute the slope, you have both pieces of information for the line.

Example 1.8: Finding the Equation of a Line

Your game character is currently at point (50,200) on the screen. The player clicks the point (150,400), indicating that he wants to go there. Find the equation to take him in a straight-line path to the desired location.

Solution

To generate an equation in point-slope form, all you need is the slope and one of the two points on the line:

Slope = m = (400 “200)/(150 “50) = 200/100 = 2

You could use either of the two points, because they're both on the line. Use the starting point, (50,200). Now you just have to plug those two items into the point-slope equation:

( y y 1 ) = m ( x x 1 )

( y “200) = 2( x “50)

What's interesting is that if you want your answer in slope-intercept form instead, all it takes is two quick algebraic steps:

( y “200) = 2( x “50)

y “200 = 2 x “100

y = 2 x +100

When it's time for your character to animate along that line, you'll want to use the slope-intercept form to find out where to place it on the y-axis for each x . Notice in this example that the point-slope form gives you an easy way to derive the line, and the slope-intercept form provides an easy way to use it.

Now let's combine this process with the idea of perpendicular lines that was discussed earlier.

Example 1.9: Finding the Equation of a Perpendicular Line

An object in your game is currently moving along the line y = 2/3 x + 20. When it gets to the point (30,40), the player presses the directional button, forcing the object to turn 90 ° to the left and continue along that line. Find the equation for the line of the new path.

Solution

Just like the last example, if you find a point on the line and the slope, you can quickly generate an equation in point-slope form. You already know a point on the line, because the object was at (30,40) when it turned, so all you really need to find is the perpendicular slope.

If the slope of the original line is 2/3, the negative reciprocal is “3/2. Therefore, the slope of the new line must be “3/2.

Now you just have to plug those two items into the point-slope equation:

( y y 1 ) = m ( x x 1 )

( y “40) = “3/2( x “30)

If you like, you can even put that answer back in slope-intercept form:

( y “40) = “3/2( x “30)

( y “40) = (“3/2) x + 45

y = (“3/2) x + 85

Example 1.9 is illustrated in Figure 1.16.

Figure 1.16. Illustration of Example 1.9.

graphics/01fig16.gif

NOTE

In Chapter 4, you'll find out how to perform this process using vector operations, which makes it easier to move to 3D.


In addition to quickly generating an equation, another benefit of the point-slope equation is that it can easily be extended to 3D. In 2D, a point and a slope can define a line. Looking back at Example 1.9, the starting point of the new line (30,40) and the slope “3/2 define the line. Another way to represent the line is with a slightly different notation that will make more sense when you get to the vector chapter (Chapter 4). The vector notation represents the point in the form < x , y > or <30,40> and the slope as < D x , D y > or <2, “3>. This allows you to define a 3D line as a 3D point < x , y , z > and a 3D "slope" as < D x , D y , D z >.

Example 1.10: Defining a Line in 3D

Your game character is currently at point (50,200,75) in your 3D world. You want him to move to the point (100,50,225). Find the line that will take him in a straight-line path to the desired location.

Solution

You already know a point on the line, <50,200,75>, so all you need to find is D x , D y , and D z :

D x = 100 “50 = 50

D y = 50 “200 = “150

D z = 225 “75 = 150

Therefore, the line can be defined by the point <50,200,75> and the change in position <50, “150,150>.

Self-Assessment

Compute the slope between the following pairs of points:

1.

(0,10) and (5,0)

2.

(3,5) and (1,9)

3.

(2, “1) and (6,1)

4.

(“2, “5) and (1,4)

5.

(“3,5) and (“4, graphics/01inl05.gif )

6.

(9,8) and (9, “7)

7.

(4,2) and (“2,1)

8.

(3,7) and (“8,7)

9.

How are the lines from questions 1 and 2 related ?

10.

Describe what the line in question 6 looks like.

11.

Describe what the line in question 8 looks like.


Find the slope of the following lines:

12.

2 x + 3 y = 10

13.

x “ 5 y = 0

14.

2 y = 8

15.

x + y = “7


Find the equation of a line connecting the following pairs of points:

16.

(0,10) and (5,0)

17.

(3,5) and (1,9)

18.

(2, “1) and (6,1)

19.

(“2, “5) and (1,4)

20.

(2,0, “1) and (3,4,5)

21.

(“3,1,5) and (0,8, “2)


 <  Day Day Up  >  


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net