The Math Forum

Ask Dr. Math - Questions and Answers from our Archives
Associated Topics || Dr. Math Home || Search Dr. Math

Ball Bouncing off a Line Segment

Date: 05/04/2001 at 08:53:23
From: Robert Dick
Subject: Lines and points

I don't know if this is really a line intersection question or if I'm 
just overcomplicating the issue, but if you take an arbitrary line on 
a 2D plane, e.g. x1y1 - x2y2, then take a point that moves about the 
plane, say pxpy, can you tell if this point has crossed the line at 
any time?

I'm writing a computer simulation that shows a ball deflecting off a 
line segment, and I just can't get to the math behind it all. Another 
thing is that when the ball crosses the line, it may not land exactly 
on it. Can I compensate for this?

Help is greatly appreciated.


Date: 05/04/2001 at 12:48:02
From: Doctor Peterson
Subject: Re: Lines and points

Hi, Robert.

The best way to work with this kind of situation is with vectors, so 
you will want to find a good book that covers vectors in order to be 
able to solve such problems more easily. Many computer graphics books 
include a section on this topic.

Let's say you have a line defined by points A (x1,y1) and B (x2,y2), 
and a point P (px,py) that moves around. You can tell whether it has 
crossed the line by taking the cross product of the vectors AB and AP 
each time you move it. If the sign of this product changes (or if it 
is zero, meaning that P is exactly on line AB), you know P has crossed 

The vectors AB and AP are (x2-x1,y2-y1) and (px-x1,py-y1) 
respectively, and their cross product is

    (x2-x1)(py-y1) - (y2-y1)(px-x1)

(Technically, the cross product is a vector in three dimensions, but 
this gives you its length, which is what you need to use here.)

This can also be expressed in terms of determinants. The following 
page from our FAQ includes formulas for the signed area of a triangle, 
which is positive if the vertices are listed in a counterclockwise 
direction and negative otherwise (see if you can see how that's 
relevant here), and for determining whether two line segments 
intersect (which is what you want if it is important whether point P 
crosses the SEGMENT AB rather than just the line AB):

  Analytic Geometry Formulas - Ask Dr. Math FAQ   
  (click on Two dimensions: Lines).

- Doctor Peterson, The Math Forum   
Associated Topics:
College Euclidean Geometry
College Linear Algebra
High School Euclidean/Plane Geometry
High School Linear Algebra

Search the Dr. Math Library:

Find items containing (put spaces between keywords):
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

Math Forum Home || Math Library || Quick Reference || Math Forum Search

Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.