Starting point code for tonight:
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
struct Point {
double x, y;
Point (double xx, double yy);
Point (Point &p);
Point ();
double distanceTo (Point other);
string contents ();
void print ();
};
struct Triangle {
Point a, b, c;
Triangle (Point aa, Point bb, Point cc);
string contents ();
double area ();
};
// POINT functions
//
// this causes problems and I didn't figure out why
//
Point::Point (double xx, double yy) {
x = xx;
y = yy;
}
Point::Point (Point &p) {
x = p.x;
y = p.y;
}
Point::Point () {
x = 0; y = 0;
}
double Point::distanceTo (Point other) {
// Pythagoream Theorem a^2 + b^2 = c^2
double dx = other.x - x;
double dy = other.y - y;
return (sqrt (dx * dx + dy * dy));
}
string Point::contents () {
return "{ " + to_string(x) + ", " +
to_string(y) + " }";
}
void Point::print () {
cout << contents ();
}
// TRIANGLE functions
//
// Constructor
//
Triangle::Triangle (Point aa, Point bb, Point cc) {
a = aa;
b = bb;
c = cc;
}
string Triangle::contents () {
return "{ " + a.contents() + ", " +
b.contents() + ", " +
c.contents() + " }";
}
double Triangle::area () {
// Compute the area of a triangle from its corner points
// 1. Compute the lengths of the side
double dbc = b.distanceTo(c);
double dac = a.distanceTo(c);
double dab = a.distanceTo(b);
// 2. Plug the lengths into the standard formula
double s = (dbc + dac + dab) / 2;
double t = s * (s-dbc) * (s-dac) * (s-dab);
return sqrt (t); // <cmath>
}
int main(int argc, const char * argv[]) {
Point p ( 42, 17 );
Point q ( 17, 42 );
double d = p.distanceTo(q);
cout << d << endl;
d = q.distanceTo(p);
cout << d << endl;
cout << p.contents();
cout << endl;
Triangle t ( p, q, { 100, 100 } );
cout << t.area();
cout << endl;
return 0;
}
No comments:
Post a Comment