예제 #1
0
        public static Matrix Rotation(Tuple u, Tuple v)
        {
            double cosPhi      = u.DotProduct(v);
            var    uv          = u * v;
            var    uvMagnitude = u.Magnitude * v.Magnitude;
            double sinPhi      = uv.Magnitude / uvMagnitude;
            Tuple  n           = uv / sinPhi;
            // https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
            var m1 = new Matrix(4,
                                cosPhi, 0, 0, 0,
                                0, cosPhi, 0, 0,
                                0, 0, cosPhi, 0,
                                0, 0, 0, 1
                                );

            var m2 = new Matrix(4,
                                n.X * n.X, n.X * n.Y, n.X * n.Z, 0,
                                n.X * n.Y, n.Y * n.Y, n.Y * n.Z, 0,
                                n.X * n.Z, n.Z * n.Y, n.Z * n.Z, 0,
                                0, 0, 0, 0
                                );

            var m3 = new Matrix(4,
                                0, -n.Z, n.Y, 0,
                                n.Z, 0, -n.X, 0,
                                -n.Y, n.X, 0, 0,
                                0, 0, 0, 0);

            var m = m1 + (1 - cosPhi) * m2 + sinPhi * m3;

            return(m);
        }
예제 #2
0
파일: Helper.cs 프로젝트: fremag/ray-tracer
        // https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-plane-and-ray-disk-intersection
        public static bool IntersectPlane(ref Tuple normal, ref Tuple p0, ref Tuple l0, ref Tuple l, ref double t)
        {
            // assuming vectors are all normalized
            double denom = normal.DotProduct(l);

            if (Math.Abs(denom) > Epsilon)
            {
                var p0l0 = p0 - l0;
                t = p0l0.DotProduct(normal) / denom;
                return(t >= 0);
            }

            return(false);
        }