Ejemplo n.º 1
0
        public Color FindCollision(HPoint a, HPoint b)
        {
            //figures

            var    sphere = new HPoint(0, 0, -3);
            double radius = 1;

            var plane = new HPoint[3]
            {
                new HPoint(1, -1, 1),
                new HPoint(1, -1, -1),
                new HPoint(-1, -1, 1)
            };

            //figures

            //List<HPoint> collisions

            //if (((b - a)*(sphere - a)).Length()/(a - b).Length() < radius)
            //    return Color.Black;

            HPoint coll = HGeometry.OldIntersectPlaneLine(plane[0], plane[1], plane[2], a, b);

            return(((int)Math.Round(coll.X) + (int)Math.Round(coll.Z)) % 2 == 0 ? Color.White : Color.Black);
        }
Ejemplo n.º 2
0
 public HFaceCollider(HPoint a, HPoint b, HPoint c, HShaders.Shader shader)
 {
     A      = a;
     B      = b;
     C      = c;
     Shader = shader;
 }
Ejemplo n.º 3
0
        public static bool IsPointOnRay(HPoint p, HRay ray)
        {
            if (!HAccuracy.DoubleEqual(0, ((p - ray.Source) * (ray.Direction)).Length())) //is not on line(colinear)
            {
                return(false);
            }

            return(HAccuracy.DoubleGreater((p - ray.Source) % (ray.Direction), 0)); //is codirectional
        }
Ejemplo n.º 4
0
        public static HPoint OldIntersectPlaneLine(HPoint p0, HPoint p1, HPoint p2, HPoint l0, HPoint l1)
        {
            Point3D tmp = EquationIntersectPlaneLine(
                new Point3D(p0.X, p0.Y, p0.Z),
                new Point3D(p1.X, p1.Y, p1.Z),
                new Point3D(p2.X, p2.Y, p2.Z),
                new Point3D(l0.X, l0.Y, l0.Z),
                new Point3D(l1.X, l1.Y, l1.Z)
                );

            return(new HPoint(tmp.X, tmp.Y, tmp.Z));
        }
Ejemplo n.º 5
0
        public Color TraceRay(HRay ray)
        {
            var collisions = new List <KeyValuePair <HPoint, ICollider> >();

            foreach (ICollider collider in Colliders)
            {
                HPoint collisionPoint = collider.CollisionPoint(ray);
                if (collisionPoint != null)
                {
                    collisions.Add(new KeyValuePair <HPoint, ICollider>(collisionPoint, collider));
                }
            }

            collisions.Sort(
                (KeyValuePair <HPoint, ICollider> a, KeyValuePair <HPoint, ICollider> b) =>
                (ray.Source - a.Key).Length().CompareTo((ray.Source - b.Key).Length()));

            return(collisions.Count == 0 ? Color.White : collisions[0].Value.ProcessCollision(this, collisions[0].Value, ray));
        }
Ejemplo n.º 6
0
        public HPoint CollisionPoint(HRay ray)
        {
            HPoint intersectionPoint = HGeometry.OldIntersectPlaneLine(A, B, C, ray.Source, ray.Source + ray.Direction);

            if (intersectionPoint == null) //no intersection point with plane
            {
                return(null);
            }
            if (!HGeometry.IsPointOnRay(intersectionPoint, ray)) //point is invisible
            {
                return(null);
            }

            double firstSquare  = HGeometry.TriangleSquare(A, B, C);
            double secondSquare = HGeometry.TriangleSquare(A, B, intersectionPoint) +
                                  HGeometry.TriangleSquare(A, C, intersectionPoint) +
                                  HGeometry.TriangleSquare(C, B, intersectionPoint);

            return(HAccuracy.DoubleEqual(firstSquare, secondSquare) ? intersectionPoint : null);
        }
Ejemplo n.º 7
0
        public Bitmap Visualize()
        {
            var bmp = new Bitmap(WidthIterations, HeightIterations);

            for (int y = 0; y < HeightIterations; y++)
            {
                for (int x = 0; x < WidthIterations; x++)
                {
                    var a = new HPoint(0, 0, 0);
                    var b = new HPoint(
                        ViewportWidth / WidthIterations * x - ViewportWidth / 2,
                        ViewportHeight / HeightIterations * y - ViewportHeight / 2,
                        -ViewportDistance);

                    bmp.SetPixel(x, HeightIterations - y - 1, TraceRay(new HRay(a, b)));
                }
            }

            return(bmp);
        }
Ejemplo n.º 8
0
        public Bitmap Run(RichTextBox r)
        {
            var bmp = new Bitmap(WidthIterations, HeightIterations);

            for (int y = 0; y < HeightIterations; y++)
            {
                for (int x = 0; x < WidthIterations; x++)
                {
                    var a = new HPoint(0, 0, 0);
                    var b = new HPoint(
                        ViewportWidth / WidthIterations * x - ViewportWidth / 2,
                        ViewportHeight / HeightIterations * y - ViewportHeight / 2,
                        -ViewportDistance);

                    //r.AppendText(b.ToString() + '\n');

                    bmp.SetPixel(x, y, FindCollision(a, b));
                }
            }

            return(bmp);
        }
Ejemplo n.º 9
0
 public void ProjectPointOnLine(HPoint l0, HPoint l1, HPoint toProject)  //todo in process
 {
     HPoint planeNormal = (l1 - l0) * (toProject - l0);
     HPoint lineNormal  = planeNormal * (l1 - l0);
     double l           = TriangleSquare(l0, l1, toProject) / (l0 - l1).Length();
 }
Ejemplo n.º 10
0
 public static double TriangleSquare(HPoint a, HPoint b, HPoint c)
 {
     return(((b - a) * (c - a)).Length() / 2);
 }
Ejemplo n.º 11
0
 public static HPoint ReflectVector(HPoint vector, HPoint reflectionNormal)  //todo in process
 {
     return(null);
 }
Ejemplo n.º 12
0
 public HSphereCollider(HPoint center, double radius)
 {
     Center = center;
     Radius = radius;
 }