using System; using Sharp3D.Math.Core; namespace PathTracer { public class Square:Triangle { public Square(Vector3F pointA, Vector3F pointB, Vector3F pointC):base(pointA, pointB, pointC) { } #region Intersectable implementation Hitrecord Intersectable.Intersect (Ray r) { Matrix3F A, Ax; float beta, gamma, t; Hitrecord hit; Vector3F hitPoint; Vector3F sol = new Vector3F (pointA.X - r.getOrigin ().X, pointA.Y - r.getOrigin ().Y, pointA.Z - r.getOrigin ().Z); A = new Matrix3F (); A.M11 = pointA.X - pointB.X; A.M12 = pointA.X - pointC.X; A.M13 = r.getDir().X; A.M21 = pointA.Y - pointB.Y; A.M22 = pointA.Y - pointC.Y; A.M23 = r.getDir().Z; A.M31 = pointA.Z - pointB.Z; A.M32 = pointA.Z - pointC.Z; A.M33 = r.getDir().Z; if (A.Determinant() < -Material.EPSILON || A.Determinant () > Material.EPSILON) { // solving using // Cramer's rule float Adet = A.Determinant (); Ax = new Matrix3F (A); Ax.M11 = sol.X; Ax.M21 = sol.Y; Ax.M31 = sol.Z; beta = Ax.Determinant () / Adet; Ax = new Matrix3F (A); Ax.M21 = sol.X; Ax.M22=sol.Y; Ax.M23 = sol.Z; gamma = Ax.Determinant () / Adet; Ax = new Matrix3F (A); Ax.M31 = sol.X; Ax.M32 = sol.Y; Ax.M33 = sol.Z; t = Ax.Determinant () / Adet; // if beta + gamma are // between 0 and 1, we're in // the triangle. if (beta < 1 + Material.EPSILON && gamma < 1 + Material.EPSILON && beta > -Material.EPSILON && gamma > -Material.EPSILON) { hitPoint = new Vector3F (r.getDir ()); hitPoint = Vector3F.Multiply (hitPoint, t); hitPoint = Vector3F.Add (hitPoint, r.getOrigin ()); hit = new Hitrecord (hitPoint, t, getNormal (beta, gamma), ((Intersectable)this).getMaterial (), this); return hit; } } return null; } #endregion } }