1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
//Quadrilateral.h
#ifndef QUADRILATERAL_H_INCLUDED
#define QUADRILATERAL_H_INCLUDED
#include "Point.h"
#include <iostream>
using namespace std;
class Quadrilateral : public Point
{
protected:
virtual void print(ostream &out, Point& a, Point& b, Point& c, Point& d);
Point A;
Point B;
Point C;
Point D;
public:
Quadrilateral();
Quadrilateral(Point a, Point b, Point c, Point d );
Quadrilateral(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4);
void setAll(Point a, Point b, Point c, Point d);
double Perimeter();
double Area();
virtual void print(ostream &out, Quadrilateral &Q );
friend ostream & operator <<(ostream &out, Quadrilateral &Q);
};
#endif
#include "Quadrilateral.h"
#include <iostream>
Quadrilateral::Quadrilateral() : A(), B() , C(), D()
{
A.SetAll(0.0,0.0 );
B.SetAll(6.0, -2.0);
C.SetAll(4.0, 8.0);
D.SetAll(1.0, 5.0);
}
Quadrilateral::Quadrilateral(Point a, Point b, Point c, Point d ): A(a), B(b), C(c), D(d) //Normal constructor using Points
{
if(a.getX() >= b.getX() || a.getY() >= d.getY() ||
d.getX() >= c.getX() || b.getY() >= c.getY() ) //if coordinates are illegal,
{ //set to default values
cout << "\nIllegal Quadrilateral: "<<a<<b<<c<<d<<endl<<"Setting to: ";
a.SetAll(0.0,0.0 );
b.SetAll(6.0, -2.0);
c.SetAll(4.0, 8.0);
d.SetAll(1.0, 5.0);
cout<<a<<b<<c<<d<<endl;
A=a;
B=b;
C=c;
D=d;
}
else
{ //else
setAll(a, b, c, d);
}
}
Quadrilateral::Quadrilateral(double x1, double y1, double x2, double y2, //Normal constructor using coordinates
double x3, double y3, double x4, double y4)
{
Point a(x1, y1), b(x2, y2), c(x3, y3), d(x4, y4);
setAll(a, b, c, d);
}
void Quadrilateral::setAll( Point a, Point b, Point c, Point d )
{
A = a;
B = b;
C = c;
D = d;
}
double Quadrilateral::Perimeter()
{
double total=0;
total = A.Distance(B) + B.Distance(C) + C.Distance(D) + D.Distance(A);
return total;
}
double Quadrilateral::Area()
{
double t1=0, t2=0, a1 = A.Distance(D), b1 = C.Distance(D); //a1, b1, & c make up the 1st triangle (t1)
double a2 = A.Distance(B), b2 = B.Distance(C), c = A.Distance(C); //a2, b2, & c make up the 2nd (t2)
//Heron's formula
t1 = sqrt((a1+b1+c)*(b1-a1+c)*(a1-b1+c)*(a1+b1-c))/4;
t2 = sqrt((a2+b2+c)*(b2-a2+c)*(a2-b2+c)*(a2+b2-c))/4;
return t1+t2;
}
ostream & operator <<(ostream &out, Quadrilateral &Q)
{
//out <<"I'm a Quadrilateral: "<<Q.A<<Q.B<<Q.C<<Q.D<<flush;
Q.print(out, Q.A, Q.B, Q.C, Q.D);
return out;
}
void print(ostream &out, Point& a, Point& b, Point& c, Point& d)
{
out <<"I'm a Quadrilateral: "<<a<<b<<c<<d<<flush;
}
|