예제 #1
0
        public void TransformIdnetityReturnsSahpeObject()
        {
            IPath shape           = new RectangularPolygon(0, 0, 200, 60);
            IPath transformdShape = shape.Transform(Matrix3x2.Identity);

            Assert.Same(shape, transformdShape);
        }
예제 #2
0
        public void Transform()
        {
            IPath shape = new RectangularPolygon(0, 0, 200, 60);

            IPath newShape = shape.Transform(new Matrix3x2(0, 1, 1, 0, 20, 2));

            Assert.Equal(new PointF(20, 2), newShape.Bounds.Location);
            Assert.Equal(new SizeF(60, 200), newShape.Bounds.Size);
        }
예제 #3
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));
            }
예제 #4
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);
        }