Advertisement
clairec

CSCI 40 Triangles Lab

Apr 27th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. class PointClass {
  7.     public:
  8.         void SetCoords(int x, int y);       /* Sets xCoord and yCoord values */
  9.         double GetDistance(PointClass ptB); /* Returns distance to ptB */
  10.         void Print();                       /* Prints "(xCoord, yCoord)\n" */
  11.         int GetXcoord();                    /* Returns xCoord */
  12.         int GetYcoord();                    /* Returns yCoord */
  13.         PointClass operator+(PointClass B); /* Overloads + to add two points.
  14.                                                (a, b) + (c, d) = (a+b, c+d) */
  15.     private:
  16.         int xCoord;
  17.         int yCoord;
  18. };
  19.  
  20. class TriangleClass {
  21.     public:
  22.         void SetPoints(PointClass A, PointClass B, PointClass C);
  23.         double GetArea();
  24.         void Print();
  25.     private:
  26.         PointClass A;
  27.         PointClass B;
  28.         PointClass C;
  29. };
  30.  
  31. void PointClass::SetCoords(int x, int y) {
  32.     xCoord = x;
  33.     yCoord = y;
  34. }
  35.  
  36. double PointClass::GetDistance(PointClass ptB) {
  37.     return sqrt( pow(xCoord - ptB.xCoord, 2) +
  38.                  pow(yCoord - ptB.yCoord, 2) );
  39. }
  40.  
  41. void PointClass::Print() {
  42.     cout << "(" << xCoord << ", " << yCoord << ")\n";
  43. }
  44.  
  45. int PointClass::GetXcoord() {
  46.     return xCoord;
  47. }
  48.  
  49. int PointClass::GetYcoord() {
  50.     return yCoord;
  51. }
  52.  
  53. PointClass PointClass::operator+(PointClass B) {
  54.     PointClass newPoint;
  55.     newPoint.SetCoords(xCoord + B.xCoord, yCoord + B.yCoord);
  56.     return newPoint;
  57. }
  58.  
  59. void TriangleClass::SetPoints(PointClass A, PointClass B, PointClass C) {
  60.     this->A = A;
  61.     this->B = B;
  62.     this->C = C;
  63. }
  64.  
  65. double TriangleClass::GetArea() {
  66.     /* Using Heron's Formula */
  67.     double a = A.GetDistance(B);
  68.     double b = B.GetDistance(C);
  69.     double c = C.GetDistance(A);
  70.     double s = (a + b + c) / 2;
  71.     double area = sqrt(s * (s - a) * (s - b) * (s - c));
  72.     return area;
  73. }
  74.  
  75. void TriangleClass::Print() {
  76.     cout << "( (" << A.GetXcoord() << ", " << A.GetYcoord() << "), ";
  77.     cout << "(" << B.GetXcoord() << ", " << B.GetYcoord() << "), ";
  78.     cout << "(" << C.GetXcoord() << ", " << C.GetYcoord() << ") )\n";
  79.  
  80. }
  81.  
  82.  
  83. int main() {
  84.     PointClass ptA;
  85.     PointClass ptB;
  86.     PointClass ptC;
  87.     PointClass ptD;
  88.     TriangleClass triangleABC;
  89.    
  90.     ptA.SetCoords(0, 0);
  91.     ptB.SetCoords(0, 3);
  92.     ptC.SetCoords(4, 0);
  93.     ptD = ptA + ptB;                     /* Using the overloaded + operator */
  94.    
  95.     triangleABC.SetPoints(ptA, ptB, ptC);
  96.    
  97.    
  98.     assert(ptA.GetDistance(ptB) == 3);   /* Testing distance function */
  99.    
  100.     assert(ptD.GetXcoord() == 0);        /* Checking to see if point adding works as expected */
  101.     assert(ptD.GetYcoord() == 3);
  102.    
  103.     assert(triangleABC.GetArea() == 6);  /* Testing area function */
  104.    
  105.     cout << "ALL TESTS PASSED!\n";
  106.    
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement