adding / and /= operators to Point and Vector types in db [ci skip]

This commit is contained in:
Thomas Ferreira de Lima 2018-08-02 00:37:43 -04:00
parent 1db2bea26c
commit cfbcc158bf
No known key found for this signature in database
GPG Key ID: 43E98870EAA0A86E
5 changed files with 170 additions and 0 deletions

View File

@ -194,6 +194,20 @@ public:
*/
point<C> &operator*= (long s);
/**
* @brief Division by some divisor.
*
* Scaline involves rounding which in our case is simply handled
* with the coord_traits scheme.
*/
point<C> &operator/= (double s);
/**
* @brief Dividing self by some integer divisor
*/
point<C> &operator/= (long s);
/**
* @brief The euclidian distance to another point
*
@ -452,6 +466,32 @@ operator* (const db::point<C> &p, unsigned int s)
return point<C> (p.x () * s, p.y () * s);
}
template <class C, typename Number>
inline point<C>
operator/ (const db::point<C> &p, Number s)
{
double mult = 1.0 / static_cast<double>(s);
return point<C> (p.x () * mult, p.y () * mult);
}
template <class C>
inline point<C> &
point<C>::operator/= (double s)
{
double mult = 1.0 / static_cast<double>(s);
*this *= mult;
return *this;
}
template <class C>
inline point<C> &
point<C>::operator/= (long s)
{
double mult = 1.0 / static_cast<double>(s);
*this *= mult;
return *this;
}
template <class C>
inline point<C> &
point<C>::operator*= (double s)

View File

@ -266,6 +266,20 @@ public:
*/
vector<C> operator*= (long s);
/**
* @brief Division by some divisor.
*
* Scaline involves rounding which in our case is simply handled
* with the coord_traits scheme.
*/
vector<C> &operator/= (double s);
/**
* @brief Dividing self by some integer divisor
*/
vector<C> &operator/= (long s);
/**
* @brief The euclidian length
*/
@ -450,6 +464,32 @@ vector<C>::operator* (long s) const
return vector<C> (m_x * s, m_y * s);
}
template <class C, typename Number>
inline vector<C>
operator/ (const db::vector<C> &p, Number s)
{
double mult = 1.0 / static_cast<double>(s);
return vector<C> (p.x () * mult, p.y () * mult);
}
template <class C>
inline vector<C> &
vector<C>::operator/= (double s)
{
double mult = 1.0 / static_cast<double>(s);
*this *= mult;
return *this;
}
template <class C>
inline vector<C> &
vector<C>::operator/= (long s)
{
double mult = 1.0 / static_cast<double>(s);
*this *= mult;
return *this;
}
template <class C>
inline vector<C>
vector<C>::operator*= (double s)

View File

@ -70,6 +70,23 @@ struct point_defs
return C (*p * s);
}
static C divide (const C *p, double s)
{
return C (*p / s);
}
static C iscale (C *p, double s)
{
*p *= s;
return *p;
}
static C idiv (C *p, double s)
{
*p /= s;
return *p;
}
static C negate (const C *p)
{
return -*p;
@ -175,6 +192,30 @@ struct point_defs
"Returns the scaled object. All coordinates are multiplied with the given factor and if "
"necessary rounded."
) +
method_ext ("*=", &iscale,
"@brief Scaling by some factor\n"
"\n"
"@args f\n"
"\n"
"Scales object in place. All coordinates are multiplied with the given factor and if "
"necessary rounded."
) +
method_ext ("/", &divide,
"@brief Division by some divisor\n"
"\n"
"@args d\n"
"\n"
"Returns the scaled object. All coordinates are divided with the given divisor and if "
"necessary rounded."
) +
method_ext ("/=", &idiv,
"@brief Division by some divisor\n"
"\n"
"@args d\n"
"\n"
"Divides the object in place. All coordinates are divided with the given divisor and if "
"necessary rounded."
) +
method ("distance", (double (C::*) (const C &) const) &C::double_distance,
"@brief The Euclidian distance to another point\n"
"\n"

View File

@ -70,6 +70,23 @@ struct vector_defs
return C (*p * s);
}
static C divide (const C *p, double s)
{
return C (*p / s);
}
static C iscale (C *p, double s)
{
*p *= s;
return *p;
}
static C idiv (C *p, double s)
{
*p /= s;
return *p;
}
static C negate (const C *p)
{
return -*p;
@ -202,6 +219,30 @@ struct vector_defs
"Returns the scaled object. All coordinates are multiplied with the given factor and if "
"necessary rounded."
) +
method_ext ("*=", &iscale,
"@brief Scaling by some factor\n"
"\n"
"@args f\n"
"\n"
"Scales object in place. All coordinates are multiplied with the given factor and if "
"necessary rounded."
) +
method_ext ("/", &divide,
"@brief Division by some divisor\n"
"\n"
"@args d\n"
"\n"
"Returns the scaled object. All coordinates are divided with the given divisor and if "
"necessary rounded."
) +
method_ext ("/=", &idiv,
"@brief Division by some divisor\n"
"\n"
"@args d\n"
"\n"
"Divides the object in place. All coordinates are divided with the given divisor and if "
"necessary rounded."
) +
method_ext ("vprod", &vprod,
"@brief Computes the vector product between self and the given vector\n"
"\n"

View File

@ -493,7 +493,11 @@ static std::string extract_python_name (const std::string &name)
} else if (name == "-@") {
return "__neg__";
} else if (name == "/") {
#if PY_MAJOR_VERSION < 3
return "__div__";
#else
return "__truediv__";
#endif
} else if (name == "*") {
return "__mul__";
} else if (name == "%") {
@ -515,7 +519,11 @@ static std::string extract_python_name (const std::string &name)
} else if (name == "-=") {
return "__isub__";
} else if (name == "/=") {
#if PY_MAJOR_VERSION < 3
return "__idiv__";
#else
return "__itruediv__";
#endif
} else if (name == "*=") {
return "__imul__";
} else if (name == "%=") {