Jump to content
  • Advertisement
Sign in to follow this  
kashyap

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.

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
Advertisement
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  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!