Draw and fill polygon with 4 vertices (positions in 2D) via sharpdx API
Asked Answered
F

1

7

I have 4 float points (positions in 2D) and I want to draw and fill a polygon (with vertices at those 4 positions) in 2D.

How can I do that with sharpdx?

Frisco answered 4/12, 2014 at 8:36 Comment(0)
F
8

I have found how to draw and fill a polygon. I have used drawGeometry and fillGeometry methods of RenderTarget object.

I wont mention how to initialize the directx

    PathGeometry geo1;
    GeometrySink sink1;

        FactoryD2D factory = new FactoryD2D();
        var dpi = factory.DesktopDpi;
        RenderTarget renderTarget;

        renderTarget = new RenderTarget(factory, backBuffer, new RenderTargetProperties()
        {
            DpiX = dpi.Width,
            DpiY = dpi.Height,
            MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
            PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Ignore),
            Type = RenderTargetType.Hardware,
            Usage = RenderTargetUsage.None
        });

        // and after all initialization

            pta[0] = new SharpDX.Vector2(pts[4].X, pts[4].Y);
            pta[1] = new SharpDX.Vector2(pts[5].X, pts[5].Y);
            pta[2] = new SharpDX.Vector2(pts[6].X, pts[6].Y);
            pta[3] = new SharpDX.Vector2(pts[7].X, pts[7].Y);

            geo1 = new PathGeometry(factory);
            sink1 = geo1.Open();
            sink1.BeginFigure(pta[0], new FigureBegin());
            sink1.AddLines(new SharpDX.Vector2[] { pta[1], pta[2], pta[3] });
            sink1.EndFigure(new FigureEnd());
            sink1.Close();

        Color penColor = Color.Black;
        SolidColorBrush penBrush = new SolidColorBrush(g, new SharpDX.Color(penColor.R, penColor.G, penColor.B));
        Color color = AddColor(pt, zmin, zmax);
        SolidColorBrush aBrush = new SolidColorBrush(g, new SharpDX.Color(color.R, color.G, color.B));

            renderTarget.DrawGeometry(geo1, penBrush);
            renderTarget.FillGeometry(geo1, aBrush);
            geo1.Dispose();
            sink1.Dispose();

You can add other geos and sinks in order to add more shapes on to it

Hope this help to somebody who is lost and trying to find an answer desperately like me

Frisco answered 5/12, 2014 at 11:59 Comment(1)
after 4 years, I have come to my own answer while searching for the same solution : )) Helping others helped me too.Frisco

© 2022 - 2024 — McMap. All rights reserved.