USGS

Isis 3.0 Object Programmers' Reference

Home

LineEquation.cpp
Go to the documentation of this file.
1 
23 #include "LineEquation.h"
24 #include "IException.h"
25 #include "IString.h"
26 #include <iostream>
27 #include <iomanip>
28 
29 using namespace std;
30 
31 namespace Isis {
35  LineEquation::LineEquation() {
36  p_defined = false;
37  p_slopeDefined = false;
38  p_interceptDefined = false;
39  }
40 
48  LineEquation::LineEquation(double x1, double y1, double x2, double y2) {
49  p_defined = false;
50  p_slopeDefined = false;
51  p_interceptDefined = false;
52  AddPoint(x1, y1);
53  AddPoint(x2, y2);
54  p_defined = true;
55  p_slope = Slope();
56  p_intercept = Intercept();
57  }
58 
67  void LineEquation::AddPoint(double x, double y) {
68  if(p_defined) {
69  std::string msg = "Line equation is already defined with 2 points";
70  throw IException(IException::Io, msg, _FILEINFO_);
71  }
72  p_x.push_back(x);
73  p_y.push_back(y);
74  if(Points() == 2) p_defined = true;
75  }
76 
82  double LineEquation::Slope() {
83  if(!p_defined) {
84  std::string msg = "Line equation undefined: 2 points are required";
85  throw IException(IException::Io, msg, _FILEINFO_);
86  }
87  else if(p_x[0] == p_x[1]) {
88  std::string msg = "Points have identical independent variables -- no slope";
89  throw IException(IException::Io, msg, _FILEINFO_);
90  }
91  else if(!p_slopeDefined) {
92  p_slope = (p_y[0] - p_y[1]) / (p_x[0] - p_x[1]);
93  }
94  return p_slope;
95  }
96 
102  double LineEquation::Intercept() {
103  if(!p_defined) {
104  std::string msg = "Line equation undefined: 2 points are required";
105  throw IException(IException::Io, msg, _FILEINFO_);
106  }
107  else if(p_x[0] == p_x[1]) {
108  std::string msg = "Points have identical independent variables -- no intercept";
109  throw IException(IException::Io, msg, _FILEINFO_);
110  }
111  else if(!p_interceptDefined) {
112  p_intercept = p_y[0] - Slope() * p_x[0];
113  }
114 
115  return p_intercept;
116  }
117 
118 }