Sign in to follow this  

Reg: Any Package available to calculate the projection of a vector on a given plane

This topic is 3452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, I am writing a meshing routine in C language and i need some help regarding the availability of any pre-written code which will evaluate the projection of a vector on a given plane. Thanks Kashyap

Share this post


Link to post
Share on other sites
This should be all you need:
// Assume the normal to the plane is normalized (has length 1)
Vector3D project_vector_on_plane(Vector3D const &vector, Vector3D const &normal) {
return vector - vector.dot(normal) * normal;
}


This is a complete program that shows that code in action:
#include <iostream>

struct Vector3D {
double x, y, z;

Vector3D(double x, double y, double z) : x(x), y(y), z(z) {
}

Vector3D operator+(Vector3D const &rhs) const {
return Vector3D(x+rhs.x, y+rhs.y, z+rhs.z);
}

Vector3D operator-(Vector3D const &rhs) const {
return Vector3D(x-rhs.x, y-rhs.y, z-rhs.z);
}

Vector3D operator*(double s) const {
return Vector3D(s*x, s*y, s*z);
}

double dot(Vector3D const &rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
};

Vector3D operator*(double s, Vector3D const &v) {
return v*s;
}

std::ostream &operator<<(std::ostream &oss, Vector3D const &v) {
return oss << '(' << v.x << ',' << v.y << ',' << v.z << ')';
}

// Assume the normal to the plane is normalized (has length 1)
Vector3D project_vector_on_plane(Vector3D const &vector, Vector3D const &normal) {
return vector - vector.dot(normal) * normal;
}

int main() {
Vector3D v(3.0, 4.0, 5.0);
Vector3D z(0.0, 0.0, 1.0);

std::cout << project_vector_on_plane(v,z) << '\n';
}


Share this post


Link to post
Share on other sites
Sign in to follow this