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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <cstdio> // now C++ header
#include <iostream>
#include <string>
using namespace std;
class Sline;
class plane_lp;
class plane_3p;
class fullvector;
// point
class point{
protected:
int x,y,z;
public:
point():x(0),y(0),z(0){};
point(int a,int b,int c):x(a),y(b),z(c){};
point(const point& a):x(a.x),y(a.y),z(a.z){};
point operator-(const point& a) const;
point& operator=(const point& a);
friend point cross_product(const point& P1,const point& P2);
friend int dot_product(const point& P1, const point& P2);
friend ostream& operator<<(ostream& os,const point& a);
};
point point::operator-(const point& a) const {
return point(this->x-a.x,this->y-a.y,this->z-a.z); // reversed
}
point& point::operator=(const point& a){
this->x = a.x;
this->y = a.y;
this->z = a.z;
return *this;
}
int dot_product(const point& P1,const point& P2){ // replaces independent_term()
return (P1.x*P2.x+P1.y*P2.y+P1.z*P2.z);
}
point cross_product(const point& P1,const point& P2){ // was vector_product
return point((P1.y*P2.z)-(P1.z*P2.y),(P1.x*P2.z)-(P1.z*P2.x),(P1.x*P2.y)-(P1.y*P2.x));
}
ostream& operator<<(ostream& os,const point& a){
return os << a.x << "x " << a.y << "y " << a.z << "z"; // compacted!
}
// Sline
class Sline{
private:
point a;
public:
Sline() = default;
Sline(const point& x, const point& y) : a(x - y){
}
Sline operator=(const Sline& val){
this->a = val.a;
return *this;
}
friend ostream& operator<<(ostream& os,const Sline& val);
};
ostream& operator<<(ostream& os,const Sline& val){
return os << val.a;
}
// fullvector
class fullvector:public point,public Sline{ // why inherit from Sline here?
private:
int D;
public:
fullvector():D(0){}; // no need to call base class default constructor explicitly
fullvector(int a,int b,int c,int d):point(a,b,c),D(d){};
fullvector(const point& a,int d):point(a),D(d){};
friend ostream& operator<<(ostream& os,const fullvector& a);
};
// Not good to get input from user in the middle of operator>>
//
//void operator>>(istream& is,fullvector& a){
// cout<<"Enter term x"<<"\n";
// cin>>a.x;
// cout<<"\n";
// cout<<"Enter term y"<<"\n";
// cin>>a.y;
// cout<<"\n";
// cout<<"Enter term z"<<"\n";
// cin>>a.z;
// cout<<"\n";
// cout<<"Enter term D"<<"\n";
// cin>>a.D;
// cout<<"\n";
//}
// better solution (function which gets info
// from user and constructs object)
fullvector makeplane(){
int x, y, z, D;
cout<<"x term:"<<"\n";
cin>>x;
cout<<"\n";
cout<<"y term:"<<"\n";
cin>>y;
cout<<"\n";
cout<<"z term"<<"\n";
cin>>z;
cout<<"\n";
cout<<"independent "<<"\n";
cin>>D;
cout<<"\n";
return fullvector(x, y, z, D);
}
ostream& operator<<(ostream& os,const fullvector& a){
return os << static_cast<const point&>(a) << " " << a.D << "i";
}
int main(int argc, char **argv)
{
point p1(8,5,5);
point p2(3,3,1);
point p3(1,4,7);
Sline S1(p1,p2);
point v1 = p1-p2;
point v2 = p2-p3;
point rawplane = cross_product(v1,v2);
int independent = -dot_product(v1, rawplane); // note -
fullvector myplane(rawplane,independent);
cout<<rawplane<<"\n";
cout<<myplane<<"\n";
//cout<<"Creation of three planes"<<"\n";
cout<<"Creation of one plane"<<"\n";
fullvector plane1 = makeplane();
//fullvector plane2 = makeplane();
//fullvector plane3 = makeplane();
cout<<plane1<<"\n";
//cout<<plane2<<"\n";
//cout<<plane3<<"\n";
}
|