计算射线与平面的交点

#include "stdafx.h"
#include<iostream>
using namespace std;
 
struct Point3{
    float x;
    float y;
    float z;
};
struct Vector{
    float x;
    float y;
    float z;
};
struct Ray{       //一点,和一个方向向量(两点求差)确定一条射线,
    Point3 p0;
    Vector u;
};
struct Plane{     //一点,和一个法向量确定一个平面
    Point3 p1;
    Vector n;
};
 
float operator*(Vector v1,Vector v2)
{
    float x = v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
    return x;
}
float operator*(Vector v1, Point3 v2)
{
    float x = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
    return x;
}
Point3 operator*(float t, Vector v)
{
    Point3 p;
    p = { t*v.x, t*v.y, t*v.z };
    return p;
}
 
Vector operator-(Vector v1, Vector v2)
{
    Vector v3 = { v2.x-v1.x, v2.y-v1.y, v2.z-v1.z };
    return v3;
}
 
Point3 operator+(Point3 v1, Point3 v2)
{
    Point3 v3 = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
    return v3;
}
Point3 operator-(Point3 v1, Point3 v2)
{
    Point3 v3 = { v2.x - v1.x, v2.y - v1.y, v2.z - v1.z };
    return v3;
}
Point3 RayPlaneIntersection(Ray ray,Plane plane)
{
    Point3 p;
    float t;
    t = (plane.n*plane.p1 - plane.n*ray.p0) / (plane.n*ray.u);
    p = ray.p0 + t*ray.u;    
    return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
    Ray r1;
    Plane p1;
    r1 = { { 0, 0, 0 }, { 0, 1, 0 } };
    p1 = { { 0, 3, 0 }, { 0, -1, 0 } };
    Point3 pt = RayPlaneIntersection(r1, p1);
    cout << pt.x << " " << pt.y << " " << pt.z << endl;
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/yangpan011/article/details/81316713