public static void ClockTest()
        {
            int w = 500;
            int h = 500;

            FrameBuffer frameBuffer = new FrameBuffer(w, h, 0);
            Vector2     a           = new Vector2(250, 250);

            double angle0 = 0;
            double angle1 = 30;
            float  raio0  = 125;
            float  raio1  = 75;

            while (true)
            {
                frameBuffer.Clear();
                double  cos = Math.Cos(NumericExtensions.ToRadians(angle0));
                double  sin = Math.Sin(NumericExtensions.ToRadians(angle0));
                Vector2 b   = new Vector2((float)(cos * raio0) + a.X, (float)(sin * raio0) + a.Y);
                plotLine(a, b, frameBuffer, 255);

                cos = Math.Cos(NumericExtensions.ToRadians(angle1));
                sin = Math.Sin(NumericExtensions.ToRadians(angle1));
                b   = new Vector2((float)(cos * raio1) + a.X, (float)(sin * raio1) + a.Y);
                plotLine(a, b, frameBuffer, 125);

                angle0 += 1f;
                angle1 += 3f;

                frameBuffer.Export("clock.pgm");

                Thread.Sleep(TimeSpan.FromSeconds(0.5f));
            }
        }
示例#2
0
        public Vector2 RayCasting(Player player, float angle)
        {
            double radians = NumericExtensions.ToRadians((double)angle);
            float  cos     = (float)Math.Cos(radians) + player.Position.X;
            float  sin     = (float)Math.Sin(radians) + player.Position.Y;

            Vector2 ray       = new Vector2(cos, sin);
            Vector2 signalRay = Vector2.VectorDistance(player.Position, ray);

            Line playerLine = new Line(player.Position, ray);

            List <Vector2> intersections = new List <Vector2>();

            Vector2 finalPoint = null;

            foreach (intTuple item in this.edge)
            {
                Vector2Tuple points = Vector2.Reorganize(this.vertex [item.A],
                                                         this.vertex [item.B]);

                Line lineMap = new Line(points.A, points.B);

                Vector2 intersect = Line.Intersection(playerLine, lineMap);

                Vector2 signalIntersect = Vector2.VectorDistance(player.Position, intersect);

                bool signalX = float.IsNegative(signalRay.X) == float.IsNegative(signalIntersect.X);
                bool signalY = float.IsNegative(signalRay.Y) == float.IsNegative(signalIntersect.Y);

                bool XinLimit = intersect.X >= points.A.X && intersect.X <= points.B.X;
                bool YinLimit = intersect.Y >= points.A.Y && intersect.Y <= points.B.Y;

                if (signalX && signalY && XinLimit && YinLimit)
                {
                    intersections.Add(intersect);

                    if (finalPoint == null)
                    {
                        finalPoint = intersect;
                    }
                    else if (Vector2.Distance(intersect, player.Position) < Vector2.Distance(finalPoint, player.Position))
                    {
                        finalPoint = intersect;
                    }
                }
            }

            Console.WriteLine(finalPoint);
            return(finalPoint);
        }