|
LinearEquation |
|
1 // joi/2/linear/LinearEquation.java 2 // 3 // 4 // Copyright 2003 Bill Campbell and Ethan Bolker 5 6 /** 7 * A LinearEquation models equations of the form y = mx + b. 8 * 9 * @version 2 10 */ 11 12 public class LinearEquation 13 { 14 private double m; // The equations's slope 15 private double b; // The equations's y-intercept 16 17 /** 18 * Construct a LinearEquation from a slope and y-intercept. 19 * 20 * @param m the slope. 21 * @param b the y-intercept. 22 */ 23 24 public LinearEquation( double m, double b ) 25 { 26 this.m = m; 27 this.b = b; 28 } 29 30 /** 31 * Construct a LinearEquation from two points. 32 * 33 * @param x1 the x coordinate of the first point 34 * @param y1 the y coordinate of the first point 35 * @param x2 the x coordinate of the second point 36 * @param y2 the y coordinate of the second point 37 */ 38 39 public LinearEquation( double x1, double y1, 40 double x2, double y2 ) 41 { 42 m = (y2 - y1) / (x2 - x1); 43 b = y1 - x1 * m; 44 } 45 46 /** 47 * Compute y, given x. 48 * 49 * @param x the input value. 50 * @return the corresponding value of y: mx+b. 51 */ 52 53 public double compute( double x ) 54 { 55 return m*x + b; 56 } 57 58 /** 59 * Compute the inverse of this linear equation. 60 * 61 * @return the LinearEquation object you get by "solving for x". 62 */ 63 64 public LinearEquation getInverse() 65 { 66 return new LinearEquation( 1.0/m, -b/m ); 67 } 68 } 69
|
LinearEquation |
|