Commit 1605ed39 authored by Jan Möbius's avatar Jan Möbius

Improved Vector in QtScript

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@14819 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 3405301b
......@@ -89,3 +89,43 @@ void prototypeVec3d::sub(QScriptValue _vector) {
thisObject().setProperty("y", QScriptValue(thisObject().property("y").toNumber() - _vector.property("y").toNumber()));
thisObject().setProperty("z", QScriptValue(thisObject().property("z").toNumber() - _vector.property("z").toNumber()));
}
void prototypeVec3d::zero() {
thisObject().setProperty("x", QScriptValue(0.0));
thisObject().setProperty("y", QScriptValue(0.0));
thisObject().setProperty("z", QScriptValue(0.0));
}
QScriptValue prototypeVec3d::sprod(QScriptValue _vector) {
return QScriptValue( thisObject().property("x").toNumber() * _vector.property("x").toNumber() +
thisObject().property("y").toNumber() * _vector.property("y").toNumber() +
thisObject().property("z").toNumber() * _vector.property("z").toNumber() );
}
QScriptValue prototypeVec3d::norm() {
return QScriptValue( sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() ) );
}
QScriptValue prototypeVec3d::sqrnorm() {
return QScriptValue( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() );
}
void prototypeVec3d::normalize() {
double length = sqrt( thisObject().property("x").toNumber() * thisObject().property("x").toNumber() +
thisObject().property("y").toNumber() * thisObject().property("y").toNumber() +
thisObject().property("z").toNumber() * thisObject().property("z").toNumber() );
if ( length != 0.0 ) {
thisObject().setProperty("x", ( thisObject().property("x").toNumber() / length ) );
thisObject().setProperty("y", ( thisObject().property("y").toNumber() / length ) );
thisObject().setProperty("z", ( thisObject().property("z").toNumber() / length ) );
}
}
......@@ -54,19 +54,29 @@
/** \page scripting_vector_type Vector data type for scripting
*
* The vector data type is corresponding to the Vec3d type in C++. It does not support all
* of its operations yet. The implementation of the scripting type is done in prototypeVec3d.
* The vector data type is corresponding to the Vec3d type in C++. The implementation of the
* scripting type is done in prototypeVec3d.
*
* You can use the following functions:
* \code
* var vec = Vector(1,2,3)
* var vec2 = Vector(10,20,30)
*
* vec.multiply(0.5) // Multiply vec with 0.5
* vec.add(vec2) // Add vec2 to vec
* vec.sub(vec2) // Subtract vec2 from vec
* vec.multiply(0.5) // Multiply vec with 0.5
* vec.add(vec2) // Add vec2 to vec
* vec.sub(vec2) // Subtract vec2 from vec
*
* var norm = vec.norm() // Calculate the length of the vector
* var sqrnorm = vec.sqrnorm() // Calculate the squared length of the vector
*
* var sprod = vec.sprod(vec) //Compute scalar product with the vector and itself
*
* vec.normalize() // Normalize vector
*
* vec.zero() // Set all components of the vector to zero
*
* print(vec) // Output vector to the console
*
* print(vec) // Output the current value: 0.5,1,1.5
* \endcode
*
*/
......@@ -111,6 +121,36 @@ class prototypeVec3d : public QObject , public QScriptable
*/
void sub(QScriptValue _vector);
/** \brief Resets all components of the vector to zero
*
*/
void zero();
/** \brief Calculate scalar product
*
* @param _vector Second vector for scalar product (Can be the vector itself)
* @return Scalar product value
*/
QScriptValue sprod(QScriptValue _vector);
/** \brief Calculate the Euclidean norm of the vector
*
* @return Norm of the vector
*/
QScriptValue norm();
/** \brief Calculate the squared Euclidean norm of the vector
*
* @return Squared norm of the vector
*/
QScriptValue sqrnorm();
/** \brief Normalize the vector
*/
void normalize();
public Q_SLOTS:
QString toString() const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment