usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< PreviousNext >>

Code to Animate an Image Body through a Straight Line in Java



Using the Equation of a Straight Line in Java

The equation of a straight line has the general form y = mx + c;
where m is the slope and c is the intercept on the y-axis.

For a vertical line, x is constant and for a horizontal line, y is constant.

Generating Straight Lines for Java

Given any 2 points (x1, y1) and (x2, y2); we'll have:

Line Coordinates
  y2 - y1 = y - y1
x2 - x1 x - x1
⇒ y   =   ( y2 - y1 ) x   +   x2y1 - x1y2
x2 - x1 x2 - x1

Comparing this to the general equation of a straight line, i.e. y = mx + c

m   =    y2 - y1
x2 - x1
&
c   =    x2y1 - x1y2
x2 - x1

Say we are to find the equation for the line represented by the arbitrary points (50, 50) and (200, 100):

m   =    100 - 50  =  50   =  1
200 - 50 150 3
&
c   =    200(50) - 50(100)   =  10000 - 5000
200 - 50 150
  =  5000   =  100
150 3

Hence,
         y = 1/3x + 100/3
or
         3y = x + 100


Code to Animate a Graphic Object by a Line Equation in Java

To make a graphic (dot) travel by the equation of a line, continuously increment x, and use the equation to get the corresponding y value.
Let's do so with the above equation representing points (x1, y1) = (50, 50) and (x2, y2) = (100, 200).

Create 2 new classes; File, New.
Call them PanelsStraightLine and StraightLine.
Type out the adjoining Java code for animating an image body through the path of a straight line.









<< PreviousNext >>