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

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

## 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 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 on other sites
Thank you very much

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×