Example #1
0
        public override Image Render(string mode = "human")
        {
            float       b, t, r, l;
            const int   screen_width  = 600;
            const int   screen_height = 400;
            const float world_width   = x_threshold * 2;
            const float scale         = screen_width / world_width;
            const int   carty         = 300;
            const float polewidth     = 10.0f;
            const float poleheight    = scale * (2 * length);
            const float cartwidth     = 50.0f;
            const float cartheight    = 30.0f;


            if (_viewer == null)
            {
                lock (this) {
                    //to prevent double initalization.
                    if (_viewer == null)
                    {
                        if (_viewerFactory == null)
                        {
                            _viewerFactory = NullEnvViewer.Factory;
                        }
                        _viewer = _viewerFactory(screen_width, screen_height, "cartpole-v1");
                    }
                }
            }

            //pole
            l = -polewidth / 2;
            r = polewidth / 2;
            t = poleheight - polewidth / 2;
            b = -polewidth / 2;
            var pole   = new RectangularPolygon(-polewidth / 2, carty - poleheight, polewidth, poleheight);
            var circle = new EllipsePolygon(0, carty - polewidth / 2, polewidth / 2);

            //cart
            l = -cartwidth / 2;
            r = cartwidth / 2;
            t = cartheight / 2;
            b = -cartheight / 2;
            var axleoffset = cartheight / 4.0;
            var cart       = new RectangularPolygon(-cartwidth / 2, carty - cartheight / 2, cartwidth, cartheight);
            var draw       = new List <(IPath, Rgba32)>();

            if (!(state is null))
            {
                var center_x = (float)(state.GetDouble(0) * scale + screen_width / 2.0f);
                //no y cuz it doesnt change.
                var cbounds    = circle.Bounds;
                var pivotPoint = new PointF(cbounds.X + cbounds.Width / 2f, cbounds.Y + cbounds.Height / 2f);

                draw.Add((cart.Translate(center_x, 0), Rgba32.Black));
                draw.Add((pole.Transform(Matrix3x2.CreateRotation((float)state.GetDouble(2), pivotPoint)).Translate(center_x, 0), new Rgba32(204, 153, 102)));
                draw.Add((circle.Translate(center_x, 0), Rgba32.Teal));
            }
Example #2
0
        public override byte[] Render(string mode = "human")
        {
            float b;
            float t;
            float r;
            float l;
            var   screen_width  = 600;
            var   screen_height = 400;
            var   world_width   = x_threshold * 2;
            var   scale         = screen_width / world_width;
            var   carty         = 300;
            var   polewidth     = 10.0f;
            var   poleheight    = scale * (2 * length);
            var   cartwidth     = 50.0f;
            var   cartheight    = 30.0f;

            if (viewer == null)
            {
                lock (this) {
                    //to prevent double initalization.
                    if (viewer == null)
                    {
                        viewer = Viewer.Run(screen_width, screen_height, "cartpole-v1");
                    }
                }
            }

            //pole
            l = -polewidth / 2;
            r = polewidth / 2;
            t = poleheight - polewidth / 2;
            b = -polewidth / 2;
            var pole   = new RectangularPolygon(-polewidth / 2, carty - poleheight, polewidth, poleheight);
            var circle = new EllipsePolygon(0, carty - polewidth / 2, polewidth / 2);

            //cart
            l = -cartwidth / 2;
            r = cartwidth / 2;
            t = cartheight / 2;
            b = -cartheight / 2;
            var axleoffset = cartheight / 4.0;
            var cart       = new RectangularPolygon(-cartwidth / 2, carty - cartheight / 2, cartwidth, cartheight);
            var draw       = new List <(IPath, Rgba32)>();

            if (!Equals(state, null))
            {
                var center_x = (float)(state.GetDouble(0) * scale + screen_width / 2.0f);
                //no y cuz it doesnt change.
                var cbounds    = circle.Bounds;
                var pivotPoint = new PointF(cbounds.X + cbounds.Width / 2f, cbounds.Y + cbounds.Height / 2f);

                draw.Add((cart.Translate(center_x, 0), Rgba32.Black));
                draw.Add((pole.Transform(Matrix3x2.CreateRotation((float)-state.GetDouble(2), pivotPoint)).Translate(center_x, 0), new Rgba32(204, 153, 102)));
                draw.Add((circle.Translate(center_x, 0), Rgba32.Teal));
            }
            else
            {
                draw.Add((pole, Rgba32.Orange));
                draw.Add((cart, Rgba32.Black));
                draw.Add((circle, Rgba32.Teal));
            }

            var img = new Image <Rgba32>(screen_width, screen_height);

            //line
            img.Mutate(i => i.BackgroundColor(Rgba32.White));
            img.Mutate(i => i.BackgroundColor(Rgba32.White));
            img.Mutate(i => i.Fill(Rgba32.Black, new RectangularPolygon(new PointF(0, carty), new PointF(screen_width, carty + 1))));
            foreach (var(path, rgba32) in draw)
            {
                img.Mutate(i => i.Fill(rgba32, path));
            }

            viewer.Render(img);

            //todo if (mode == "rgb_array") {
            //todo     ImageConverter converter = new ImageConverter();
            //todo     return (byte[]) converter.ConvertTo(((ImageImage) canvas.GetImage()).Image, typeof(byte[]));
            //todo }

            return(null);
        }