Isis 3 Programmer Reference
LineEquation.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "LineEquation.h"
8#include "IException.h"
9#include "IString.h"
10#include <iostream>
11#include <iomanip>
12
13using namespace std;
14
15namespace Isis {
20 p_defined = false;
21 p_slopeDefined = false;
22 p_interceptDefined = false;
23 }
24
32 LineEquation::LineEquation(double x1, double y1, double x2, double y2) {
33 p_defined = false;
34 p_slopeDefined = false;
35 p_interceptDefined = false;
36 AddPoint(x1, y1);
37 AddPoint(x2, y2);
38 p_defined = true;
39 p_slope = Slope();
40 p_intercept = Intercept();
41 }
42
51 void LineEquation::AddPoint(double x, double y) {
52 if(p_defined) {
53 std::string msg = "Line equation is already defined with 2 points";
54 throw IException(IException::Io, msg, _FILEINFO_);
55 }
56 p_x.push_back(x);
57 p_y.push_back(y);
58 if(Points() == 2) p_defined = true;
59 }
60
67 if(!p_defined) {
68 std::string msg = "Line equation undefined: 2 points are required";
69 throw IException(IException::Io, msg, _FILEINFO_);
70 }
71 else if(p_x[0] == p_x[1]) {
72 std::string msg = "Points have identical independent variables -- no slope";
73 throw IException(IException::Io, msg, _FILEINFO_);
74 }
75 else if(!p_slopeDefined) {
76 p_slope = (p_y[0] - p_y[1]) / (p_x[0] - p_x[1]);
77 p_slopeDefined = true;
78 }
79 return p_slope;
80 }
81
88 if(!p_defined) {
89 std::string msg = "Line equation undefined: 2 points are required";
90 throw IException(IException::Io, msg, _FILEINFO_);
91 }
92 else if(p_x[0] == p_x[1]) {
93 std::string msg = "Points have identical independent variables -- no intercept";
94 throw IException(IException::Io, msg, _FILEINFO_);
95 }
96 else if(!p_interceptDefined) {
97 p_intercept = p_y[0] - Slope() * p_x[0];
98 p_interceptDefined = true;
99 }
100
101 return p_intercept;
102 }
103
104}
Isis exception class.
Definition IException.h:91
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
bool p_interceptDefined
Variable indicating if intercept is defined yet.
double Slope()
Compute the slope of the line.
void AddPoint(double x, double y)
Add a point to the object.
bool p_slopeDefined
Variable indicating if slope is defined yet.
bool p_defined
Variable indicating if line is defined yet.
std::vector< double > p_x
Independent variables.
LineEquation()
Constructors.
double Intercept()
Compute the intercept of the line.
std::vector< double > p_y
Dependent variables.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.