/*This
code was written by A. El-Gadi of Tripoli University for educational
purposes. It uses operator overloading to implement vector addition, subtraction, negation, and scaling. For any
questions
use the comment section below.*/
#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
const double pi=3.14159;
class Vec{
private:
double magnitude;
double angle;
public:
#include<stdlib.h>
#include<cmath>
using namespace std;
const double pi=3.14159;
class Vec{
private:
double magnitude;
double angle;
public:
//Accessor and mutator member functions go here.
Vec(){magnitude=0; angle=0;}
Vec(double mag,double angle):magnitude(mag),angle(angle){};
Vec operator+(Vec vright){double xleft, yleft, xright, yright, newmag, newang;
xleft=magnitude*cos(angle);
yleft=magnitude*sin(angle);
xright=vright.magnitude*cos(vright.angle);
yright=vright.magnitude*sin(vright.angle);
newmag=sqrt(pow(xleft+xright,2)+pow(yleft+yright,2));
newang=asin((yleft+yright)/newmag);
return Vec(newmag,newang);}
Vec operator-(Vec vright){return *this+Vec(vright.magnitude,vright.angle+pi);}
Vec operator-(){return Vec(magnitude,angle+pi);}
friend Vec operator*(double,Vec);
friend Vec operator*(Vec,double);
void show(){cout<<magnitude<<' '<<angle<<'\n';}
};
Vec operator*(double scalar,Vec vr){return Vec(scalar*vr.magnitude,vr.angle);}
Vec operator*(Vec vl,double scalar){return Vec(scalar*vl.magnitude,vl.angle);}
int main(){
Vec v1(4,0), v2(3,pi/2);
Vec v3;
v1.show(); v2.show();
(v1+v2).show();
(v2+v1).show();
(v1+(-v2)).show();
(v1-v2).show();
v3=-v1+v2-3*v1+1.7*v2+v2*2.3;
v3.show();
(-v3).show();
(3.4*v3*2.3).show();
return 0;
}
Vec(){magnitude=0; angle=0;}
Vec(double mag,double angle):magnitude(mag),angle(angle){};
Vec operator+(Vec vright){double xleft, yleft, xright, yright, newmag, newang;
xleft=magnitude*cos(angle);
yleft=magnitude*sin(angle);
xright=vright.magnitude*cos(vright.angle);
yright=vright.magnitude*sin(vright.angle);
newmag=sqrt(pow(xleft+xright,2)+pow(yleft+yright,2));
newang=asin((yleft+yright)/newmag);
return Vec(newmag,newang);}
Vec operator-(Vec vright){return *this+Vec(vright.magnitude,vright.angle+pi);}
Vec operator-(){return Vec(magnitude,angle+pi);}
friend Vec operator*(double,Vec);
friend Vec operator*(Vec,double);
void show(){cout<<magnitude<<' '<<angle<<'\n';}
};
Vec operator*(double scalar,Vec vr){return Vec(scalar*vr.magnitude,vr.angle);}
Vec operator*(Vec vl,double scalar){return Vec(scalar*vl.magnitude,vl.angle);}
int main(){
Vec v1(4,0), v2(3,pi/2);
Vec v3;
v1.show(); v2.show();
(v1+v2).show();
(v2+v1).show();
(v1+(-v2)).show();
(v1-v2).show();
v3=-v1+v2-3*v1+1.7*v2+v2*2.3;
v3.show();
(-v3).show();
(3.4*v3*2.3).show();
return 0;
}
No comments:
Post a Comment