public IObstacle CreateObstacle()
        {
            var maxShift = 600;
            var vxCount  = rnd.Next(3, 5);
            var points   = new List <Vector2>();
            var shiftX   = rnd.Next(maxShift);
            var shiftY   = rnd.Next(maxShift);

            for (int i = 0; i < vxCount; i++)
            {
                var p = new Vector2((float)rnd.NextDouble() * bounds.Width / 5, (float)rnd.NextDouble() * bounds.Height / 8);
                p.X += shiftX;
                p.Y += shiftY;
                if (points.Count > 2)
                {
                    if (new Obstacle(points.ToArray()).Contains(p))
                    {
                        i--;
                        continue;
                    }
                }
                points.Add(p);
            }
            var sorter = new PointsSorter();
            var sorted = points.ToArray();//sorter.Sort(points.ToArray());

            return(new Obstacle(sorted));
        }
Beispiel #2
0
        private Vector2[] FindLightPoints(Vector2 position)
        {
            var rays = new List <Vector2>();

            foreach (var obs in obstacles)
            {
                foreach (var node in obs.Points)
                {
                    var relative      = node - position;
                    var zeroDotProd   = new Vector2(-relative.Y, relative.X);
                    var normalZeroDot = zeroDotProd / zeroDotProd.Length();
                    var scaledZeroDot = normalZeroDot / 2;// / (int)1e1;
                    rays.Add(relative);
                    rays.Add(relative + scaledZeroDot);
                    rays.Add(relative - scaledZeroDot);
                    //renderer.RenderDots(new[] { position + relative + scaledZeroDot, position + relative - scaledZeroDot });
                }
            }
            //renderer.RenderRays(position, rays.ToArray());

            var segments = new List <(float x1, float y1, float x2, float y2)>();

            foreach (var obs in obstacles)
            {
                segments.AddRange(obs.ChopSegments());
            }

            var result = new List <Vector2>();

            foreach (var ray in rays)
            {
                var eps = 1e-6;
                var t1s = new List <double>();
                var R0x = position.X;
                var R0y = position.Y;
                var ax  = ray.X;
                var ay  = ray.Y;
                foreach (var seg in segments)
                {
                    var S0x = seg.x1;
                    var S0y = seg.y1;
                    var bx  = seg.x2 - seg.x1;
                    var by  = seg.y2 - seg.y1;
                    var q   = ax * by - ay * bx;
                    if (q == 0)
                    {
                        return(null);
                    }
                    //if (Math.Abs(ax * by - ay * bx) < eps) continue;
                    //var t2 = (ax * (S0y - R0y) - ay * (S0x - R0x)) / (ay * bx - ax * by);
                    //if (t2 == float.NaN) { }
                    var dx = R0x - S0x;
                    var dy = R0y - S0y;
                    var t1 = (bx * dy - by * dx) / q;
                    var t2 = (ax * dy - ay * dx) / q;
                    //#warning Тут division by ZERO
                    //var (ax, ay) = r1.Direction.ToTuple();
                    //var (bx, by) = r2.Direction.ToTuple();
                    //var (dx, dy) = (r1.Origin - r2.Origin).ToTuple();
                    //var q = ax * by - ay * bx;
                    //if (q == 0) return null;
                    //var t1 = (bx * dy - by * dx) / q;
                    //var t2 = (ax * dy - ay * dx) / q;
                    //return (t1, t2);

                    //var t1 = (S0x - R0x + bx * t2) / ax;
                    //if (t1 == float.NaN) { }
                    //var t1 =
                    if (t1 < 0)
                    {
                        continue;
                    }
                    if (t2 < -eps || t2 > 1 + eps)
                    {
                        continue;
                    }
                    t1s.Add(t1);
                }
                if (t1s.Count < 1)
                {
                    continue;
                }
                var mint1 = t1s.Min();
                result.Add(new Vector2((float)(R0x + ax * mint1), (float)(R0y + ay * mint1)));
            }
            var sorter = new PointsSorter();
            var sorted = sorter.Sort(result.ToArray(), position);

            return(sorted);
        }