Starting point code:
#include <iostream>
#include <cmath>
using namespace std;
double PI = acos(-1);
struct Point {
double x, y;
};
struct Triangle {
Point a, b, c;
};
double dist (Point a, Point b) {
// Pythagoream Theorem a^2 + b^2 = c^2
double dx = a.x - b.x;
double dy = a.y - b.y;
return (sqrt (dx * dx + dy * dy));
}
double triangleArea (Triangle triangle) {
// Compute the area of a triangle from its corner points
// 1. Compute the lenghts of the side
double a = dist(triangle.b, triangle.c);
double b = dist(triangle.a, triangle.c);
double c = dist(triangle.a, triangle.b);
// 2. Plug the lengths into the standard formula
double s = (a + b + c) / 2;
double t = s * (s-a) * (s-b) * (s-c);
return sqrt (t); // <cmath>
}
string pointContents (Point p) {
return "{ " + to_string(p.x) + ", " +
to_string(p.y) + " }";
}
// NOT ADVISED
string printContents (Point p) {
cout "{ " << to_string(p.x) << ", " <<
to_string(p.y) << " }";
}
string triangleContents (Triangle t) {
return "{ " + pointContents(t.a) + ", " +
pointContents(t.b) + ", " +
pointContents(t.c) + " }";
}
Code at end of class.
Triangle.hpp:
//
// Triangle.hpp
// Nov07
//
#ifndef Triangle_hpp
#define Triangle_hpp
#include <string>
using namespace std;
struct Point {
double x, y;
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 ();
};
#endif /* Triangle_hpp */
Triangle.cpp:
//
// Triangle.cpp
// Nov07
//
// Created by Mark Brautigam on 11/7/16.
// Copyright © 2016 Mark Brautigam. All rights reserved.
//
#include <iostream>
#include <string>
#include <cmath>
#include "Triangle.hpp"
using namespace std;
// POINT functions
//
// this causes problems and I didn't figure out why
//
/*Point (double xx, double yy) {
x = xx;
y = yy;
}
Point (Point &p) {
x = p.x;
y = p.y;
}*/
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>
}
main.cpp:
//
// main.cpp
// Nov07
//
#include <iostream>
#include <cmath>
#include <vector>
#include "Triangle.hpp"
using namespace std;
// Swap function does not work unless the parameters
// are passed by reference using &
//
void swap (int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main(int argc, const char * argv[]) {
vector<int> tester = { 1, 2, 3, 4, 5 };
for (int i=0; i<tester.size(); i++) {
cout << tester[i] << " ";
}
swap (tester[1], tester[2]);
for (int i=0; i<tester.size(); i++) {
cout << tester[i] << " ";
}
Point p = { 42, 17 };
Point q = { 17, 42 };
double d = p.distanceTo(q);
cout << d;
d = q.distanceTo(p);
cout << d;
cout << p.contents();
cout << endl;
Triangle t ( p, q, { 100, 100 } );
cout << t.area();
cout << endl;
return 0;
}
No comments:
Post a Comment