Difference between revisions of "Scratch"

From WhyAskWhy.org Wiki
Jump to: navigation, search
(Test page)
 
m (More content to test styles against)
Line 19: Line 19:
  
 
};
 
};
 +
 +
point2d::point2d(double xval, double yval): x_(xval), y_(yval) {}
 +
double point2d::x()const { return x_;}
 +
double point2d::y()const { return y_;}
 +
point2d & point2d::x(double xval){
 +
    x_ = xval;
 +
    return *this;
 +
}
 +
point2d & point2d::y(double yval){
 +
    y_ = yval;
 +
    return *this;
 +
}
 +
double point2d::modulus() const { return std::sqrt(x_*x_ + y_*y_); }
 +
 +
point2d & point2d::modulus(double newmod) {
 +
    double const oldmod(modulus());
 +
    double const scale(newmod/oldmod);
 +
    x_ *= scale;
 +
    y_ *= scale;
 +
    return *this;
 +
}
 +
 +
double point2d::argument() const {
 +
    return degrees(atan2(y_, x_)); 
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 05:21, 3 August 2012

class point2d {
public:
// constructor:
    explicit point2d(double xval=0, double yval=0);
// read access functions
    double x()const;
    double y()const;
    double modulus()const;
    double argument()const;
// write access functions
    point2d & x(double xval);
    point2d & y(double yval);
    point2d & modulus(double mod);
    point2d & argument(double degrees);
private:
    double x_;
    double y_;

};

point2d::point2d(double xval, double yval): x_(xval), y_(yval) {}
double point2d::x()const { return x_;}
double point2d::y()const { return y_;}
point2d & point2d::x(double xval){
    x_ = xval;
    return *this;
}
point2d & point2d::y(double yval){
    y_ = yval;
    return *this;
}
double point2d::modulus() const { return std::sqrt(x_*x_ + y_*y_); }

point2d & point2d::modulus(double newmod) {
    double const oldmod(modulus());
    double const scale(newmod/oldmod);
    x_ *= scale;
    y_ *= scale;
    return *this;
}

double point2d::argument() const {
    return degrees(atan2(y_, x_));   
}