문제

Im making a simple Ray Traycer in C#. And for my viewport I have a rectangle class.

public class Rectangle3D
{
    public readonly Point3D Point1;
    public readonly Point3D Point2;
    public readonly Point3D Point3;
    public readonly Point3D Point4;

    public Rectangle3D(Point3D point1, Point3D point2, Point3D point3, Point3D point4)
    {
        this.Point1 = point1;
        this.Point2 = point2;
        this.Point3 = point3;
        this.Point4 = point4;
    }

    public Point3D FindCrossPoint(Ray ray)
    {
        //Intersection
    }

how would I go about writing this function. Help is much appreciated. P.S Point3D has all the needed functions. Such as cross,normalize etc...

도움이 되었습니까?

해결책

All right, let's put this in the form of an answer instead of all those comments.

Break your problem down into sub-problems:

1) Find the plane that the rectangle lies on, in the form Ax+By+Cz+D=0 Two methods:

Method one:

  • Find the equations of two orthogonal lines; the edges of the rectangle will be orthogonal by definition.

  • Given two orthogonal lines, figure out how to get the equation of the plane the lines define, in the form Ax+By+Cz+D=0.

Some hints here:

  • Suppose the two orthogonal lines were both going through the origin. Make them vectors. What is the meaning of the cross product of those two vectors?

  • What is the relationship between the cross product vector and the plane you're seeking? Specifically, what is the relationship between the cross product vector, and the constants A, B and C?

Method two:

  • Pick three of the points on the rectangle that do not lie in a line; obviously any three corners will do. Substitute those points in for x, y, z in Ax+By+Cz+D=0 to make three new equations; solve those equations for A, B, C and D.

2) Once you have the equation of the plane, work out the intersection of the plane with the ray.

3) Now that you have the intersection point -- if it exists, remember, the ray might be parallel to the plane or might be entirely in the plane -- check to see if the intersection point is inside the rectangle.

  • Hint: this is a special case of the more general problem of "is this point inside a polygon".
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top