WebReference.com - Part 3 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (3/5) | WebReference

WebReference.com - Part 3 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (3/5)

To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Beginning Java 2 SDK 1.4 Edition

Creating a Point from Two Lines

We could add this method to the Line class. The diagram below illustrates how the mathematics works out.

You can ignore the mathematics if you want to, as it is not the most important aspect of the example. If you are willing to take the code in the new constructor on trust, then skip to the Try It Out section below. On the other hand you shouldn't find it too difficult if you can still remember what you did in high school.

One way to get the intersection of two lines is to use equations like those shown. These are called parametric equations because they use a parameter value (s or t) as the variable for determining points on each line. The parameters s and t vary between 0 and 1 to give points on the lines between the defined start and end points. When a parameter s or t is 0 the equations give the coordinates of the start point of each line, and when the parameter value is 1 you get the end point of the line.

Where the lines intersect, the equations for the lines must produce the same (x, y) values, so, at this point, the right-hand sides of the equations for x for the two lines must be equal, and the same goes for the equations for y. This will give you two equations in s and t, and with a bit of algebraic juggling you can eliminate s to get the equation shown for t. You can then replace t in the equations defining line 1 to get x and y for the intersection point.

Try It Out--Calculating the Intersection of Two Lines

We can use these results to write the additional method we need for the Line class. Add the following code to the definition in Line.java:

// Return a point as the intersection of two lines -- called from a Line object
Point intersects(final Line line1) {
  Point localPoint = new Point(0, 0);
  double num = (this.end.y--this.start.y)*(this.start.x--line1.start.x) -
               (this.end.x--this.start.x)*(this.start.y--line1.start.y);
  double denom = (this.end.y--this.start.y)*(line1.end.x--line1.start.x) -
                 (this.end.x--this.start.x)*(line1.end.y--line1.start.y);
  localPoint.x = line1.start.x + (line1.end.x--line1.start.x)*num/denom;
  localPoint.y = line1.start.y + (line1.end.y--line1.start.y)*num/denom;
  return localPoint;
}

Since the Line class definition refers to the Point class, the Line class can't be compiled without the other being available. When you compile the Line class the compiler will compile the other class too.

How It Works

The intersects() method is called from one Line object, and takes another Line object as an argument. In the code, the local variables num and denom are the numerator and denominator in the expression for t in the diagram. We then use these values to calculate the x and y coordinates for the intersection.

If the lines are parallel, the denominator in the equation for t will be zero, something you should really check for in the code. For the moment, we will ignore it and end up with coordinates that are Infinity if it occurs.

Note how we get at the values of the coordinates for the Point objects defining the lines. The dot notation for referring to a member of an object is just repeated when you want to reference a member of a member. For example, for the object line1, the expression line1.start refers to the Point object at the beginning of the line. Therefore line1.start.x refers to its x coordinate, and line1.start.y accesses its y coordinate.

Now we have a Line class, which we can use to calculate the intersection point of two Line objects. We need a program to test the code out.


To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Created: July 16, 2002
Revised: July 16, 2002


URL: https://webreference.com/programming/java/beginning/chap5/3/3.html