-1

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

Armen Aghajanyan
  • 368
  • 3
  • 15
  • I tried representing a rectangle through triangles and then testing collision but that way to complicated really fast. And I didn't say for you guys to write the code I asked, how should I write it. Trust me I'm not trying to be lazy even if it does look like that. I'm also really naïve to this field. Thank you. – Armen Aghajanyan Jul 20 '13 at 02:04
  • I did line-line intersection in 3D. But I don't understand how to do this for a rectangle – Armen Aghajanyan Jul 20 '13 at 02:10
  • OK, can you intersect a line with a *plane* in 3D? – Eric Lippert Jul 20 '13 at 02:12
  • That I don't know how to do. Sorry if I'm being annoying. – Armen Aghajanyan Jul 20 '13 at 02:16
  • okay... so I understand how to solve the equation above. But how do you find a plane from 4 points? P.S Never learned this in school. I'm just 15 – Armen Aghajanyan Jul 20 '13 at 02:23

1 Answers1

3

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".
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067