Beispiel #1
0
 void LineTo(double x, double y, bool rel)         // L, l
 {
     if (rel)
     {
         m_storage.RelToAbs(ref x, ref y);
     }
     m_storage.LineTo(x, y);
 }
Beispiel #2
0
        GraphicsPath CreatePath()
        {
            var path = new GraphicsPath();

            path.MoveTo(new Point(10, 10));
            path.LineTo(new Point(20, 90));
            path.LineTo(new Point(10, 60));
            path.LineTo(new Point(90, 80));
            path.LineTo(new Point(60, 30));
            return(path);
        }
Beispiel #3
0
        void PathTest()
        {
            var pathSin = new GraphicsPath();
            var pathCos = new GraphicsPath();

            for (int i = 0; i < 48; i++)
            {
                if (i == 0)
                {
                    pathSin.MoveTo(0, 120 + (int)(Math.Sin(i * 10 * Math.PI / 180) * 100));
                    pathCos.MoveTo(0, 120 + (int)(Math.Cos(i * 10 * Math.PI / 180) * 100));
                    continue;
                }

                pathSin.LineTo(i * 5, 120 + (int)(Math.Sin(i * 10 * Math.PI / 180) * 100));
                pathCos.LineTo(i * 5, 120 + (int)(Math.Cos(i * 10 * Math.PI / 180) * 100));
            }

            graphics.Clear();

            graphics.Stroke = 3;
            graphics.DrawLine(0, 120, 240, 120, Color.White);
            graphics.DrawPath(pathSin, Color.Cyan);
            graphics.DrawPath(pathCos, Color.LawnGreen);

            graphics.Show();
        }
Beispiel #4
0
        private void PaintCurve(double[] points, Color col, Graphics g)
        {
            float w = Width / (points.Length - 1f);
            float h = Height - 1;

            var path = new GraphicsPath();

            path.MoveTo(0, h);
            for (int i = 1; i < points.Length; i++)
            {
                path.LineTo(i * w, (float)(h - (points[i] * h)));
            }
            g.DrawPath(col, path);
        }
Beispiel #5
0
        GraphicsPath CreatePath()
        {
            var newPath = new GraphicsPath();
            var start   = StartFigures;
            var close   = CloseFigures;

            // connected segments

            newPath.MoveTo(10, 10);
            newPath.LineTo(20, 90);
            newPath.LineTo(10, 60);
            newPath.LineTo(90, 80);
            newPath.LineTo(60, 30);
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddArc(100, 0, 100, 50, 200, -160);
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddBezier(new PointF(200, 10), new PointF(285, 20), new PointF(210, 85), new PointF(300, 90));
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddCurve(new PointF(310, 90), new PointF(390, 90), new PointF(390, 10), new PointF(310, 10));
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddLine(410, 10, 410, 90);
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddLines(new PointF(420, 10), new PointF(420, 90));
            if (close && start)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddLines(new PointF(430, 10), new PointF(430, 90));
            if (close)
            {
                newPath.CloseFigure();
            }

            // separate segments

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddEllipse(100, 100, 100, 45);
            if (close)
            {
                newPath.CloseFigure();
            }

            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddRectangle(10, 110, 80, 80);
            if (close)
            {
                newPath.CloseFigure();
            }

            // at the end, draw a line so we can potentially connect to parent path
            if (start)
            {
                newPath.StartFigure();
            }
            newPath.AddLines(new PointF(440, 10), new PointF(440, 90));
            if (close)
            {
                newPath.CloseFigure();
            }

            return(newPath);
        }
Beispiel #6
0
        /// <inheritdoc/>
        public Page Render(IScene scene, IEnumerable <ILightSource> lights, Camera camera)
        {
            Page tbr = new Page(1, 1);

            Graphics gpr = tbr.Graphics;

            lock (RenderLock)
                lock (scene.SceneLock)
                {
                    List <Element3D> sceneElements = new List <Element3D>((scene.SceneElements as IList <Element3D>)?.Count ?? 4);

                    foreach (Element3D element in scene.SceneElements)
                    {
                        if (!(element is Triangle3DElement))
                        {
                            sceneElements.Add(element);
                        }
                        else if (element is IVectorRendererTriangle3DElement)
                        {
                            sceneElements.Add(element);
                        }
                        else if (element is Triangle3DElement triangle)
                        {
                            sceneElements.Add(new VectorRendererTriangle3DElement(triangle)
                            {
                                OverFill = DefaultOverFill
                            });
                        }
                    }

                    List <Element3D> nonCulled = new List <Element3D>();

                    foreach (Element3D element in sceneElements)
                    {
                        if (!camera.IsCulled(element))
                        {
                            if (ResamplingTime == ResamplingTimes.AfterSorting || double.IsNaN(ResamplingMaxSize))
                            {
                                nonCulled.Add(element);
                            }
                            else
                            {
                                nonCulled.AddRange(Resample(camera, element, ResamplingMaxSize));
                            }
                        }
                    }

                    Dictionary <Element3D, List <Element3D> > allDependencies = new Dictionary <Element3D, List <Element3D> >();

                    foreach (Element3D el in nonCulled)
                    {
                        allDependencies[el] = new List <Element3D>();
                        el.SetProjection(camera);
                    }

                    IList <ILightSource> lightList = lights as IList <ILightSource> ?? lights.ToList();
                    bool anyShadows = false;
                    for (int i = 0; i < lightList.Count; i++)
                    {
                        if (lightList[i].CastsShadow)
                        {
                            anyShadows = true;
                            break;
                        }
                    }

                    List <Triangle3DElement> shadowers = null;

                    List <double> noObstructions = new List <double>(lightList.Count);
                    for (int i = 0; i < lightList.Count; i++)
                    {
                        noObstructions.Add(0);
                    }
                    ;

                    if (anyShadows)
                    {
                        shadowers = new List <Triangle3DElement>();

                        foreach (Element3D element in sceneElements)
                        {
                            if (element is Triangle3DElement triangle)
                            {
                                if (triangle.CastsShadow)
                                {
                                    shadowers.Add(triangle);
                                }
                            }
                        }
                    }

                    sbyte[] comparisons = new sbyte[nonCulled.Count * (nonCulled.Count - 1) / 2];

                    Parallel.For(0, comparisons.Length, k =>
                    {
                        int i          = (int)Math.Floor((2 * nonCulled.Count - 1 - Math.Sqrt((2 * nonCulled.Count - 1) * (2 * nonCulled.Count - 1) - 8 * k)) / 2);
                        int j          = k - i * (nonCulled.Count - 1) + i * (i - 1) / 2 + i + 1;
                        comparisons[k] = (sbyte)camera.Compare(nonCulled[i], nonCulled[j]);
                    });

                    for (int i = 0; i < nonCulled.Count; i++)
                    {
                        for (int j = i + 1; j < nonCulled.Count; j++)
                        {
                            int k          = i * (nonCulled.Count - 1) - i * (i - 1) / 2 + (j - i - 1);
                            int comparison = comparisons[k];

                            if (comparison < 0)
                            {
                                allDependencies[nonCulled[j]].Add(nonCulled[i]);
                            }
                            else if (comparison > 0)
                            {
                                allDependencies[nonCulled[i]].Add(nonCulled[j]);
                            }
                        }
                    }

                    List <Element3D> sortedElements = TopologicalSorter.Sort(nonCulled, (element, elements) =>
                    {
                        List <Element3D> dependencies = allDependencies[element];
                        return(dependencies);
                    });

                    if (ResamplingTime == ResamplingTimes.AfterSorting && !double.IsNaN(ResamplingMaxSize))
                    {
                        List <Element3D> resampledElements = new List <Element3D>();

                        foreach (Element3D element in sortedElements)
                        {
                            List <Element3D> resampled = Resample(camera, element, ResamplingMaxSize).ToList();
                            resampledElements.AddRange(resampled);
                        }

                        sortedElements = resampledElements;

                        Parallel.For(0, sortedElements.Count, i =>
                        {
                            sortedElements[i].SetProjection(camera);
                        });
                    }

                    foreach (Element3D element in sortedElements)
                    {
                        if (!camera.IsCulled(element))
                        {
                            if (element is Point3DElement point)
                            {
                                Point pt = element.GetProjection()[0];

                                GraphicsPath path = new GraphicsPath();
                                path.Arc(pt, point.Diameter * 0.5, 0, 2 * Math.PI);

                                gpr.FillPath(path, point.Colour, tag: point.Tag);
                            }
                            else if (element is Line3DElement line)
                            {
                                GraphicsPath path = new GraphicsPath();

                                foreach (Point pt in element.GetProjection())
                                {
                                    path.LineTo(pt);
                                }

                                gpr.StrokePath(path, line.Colour, line.Thickness, lineCap: line.LineCap, lineDash: line.LineDash, tag: line.Tag);
                            }
                            else if (element is Triangle3DElement triangle && element is IVectorRendererTriangle3DElement vectorRendererTriangle)
                            {
                                List <double> triangleObstructions;

                                if (!anyShadows)
                                {
                                    triangleObstructions = noObstructions;
                                }
                                else
                                {
                                    triangleObstructions = new List <double>();

                                    for (int i = 0; i < lightList.Count; i++)
                                    {
                                        if (!lightList[i].CastsShadow)
                                        {
                                            triangleObstructions.Add(0);
                                        }
                                        else
                                        {
                                            triangleObstructions.Add(lightList[i].GetObstruction(triangle.Centroid, from el in shadowers where el != triangle && vectorRendererTriangle.Parent != el select el));
                                        }
                                    }
                                }

                                foreach (IMaterial fill in triangle.Fill)
                                {
                                    FillTriangle(gpr, element.GetProjection(), fill.GetColour(triangle.Centroid, triangle.Normal, camera, lightList, triangleObstructions), vectorRendererTriangle.OverFill, camera.ScaleFactor, triangle.Tag);
                                }
                            }
                        }
                    }

                    tbr.Crop(camera.TopLeft, camera.Size);
                }

            return(tbr);
        }
Beispiel #7
0
        private void FillTriangle(Graphics graphics, IEnumerable <Point> triangleProjection, Colour colour, double overFill, double scaleFactor, string tag)
        {
            GraphicsPath path = new GraphicsPath();

            if (overFill > 0)
            {
                double overfill = overFill * scaleFactor;

                double meanX = 0;
                double meanY = 0;

                int count = 0;

                foreach (Point pt in triangleProjection)
                {
                    meanX += pt.X;
                    meanY += pt.Y;
                    count++;
                }

                meanX /= count;
                meanY /= count;

                Point centroid2D = new Point(meanX, meanY);

                Point prevPoint  = new Point();
                Point firstPoint = new Point();

                foreach (Point pt in triangleProjection)
                {
                    if (path.Segments.Count == 0)
                    {
                        path.MoveTo(pt);
                        prevPoint  = pt;
                        firstPoint = pt;
                    }
                    else
                    {
                        Point meanPoint = Intersections2D.ProjectOnLine(centroid2D, prevPoint, pt);

                        Point  dir    = new Point(meanPoint.X - meanX, meanPoint.Y - meanY);
                        double length = dir.Modulus();

                        Point newMeanPoint = new Point(meanX + dir.X * (length + overfill) / length, meanY + dir.Y * (length + overfill) / length);

                        path.LineTo(prevPoint.X - meanPoint.X + newMeanPoint.X, prevPoint.Y - meanPoint.Y + newMeanPoint.Y);

                        path.LineTo(pt.X - meanPoint.X + newMeanPoint.X, pt.Y - meanPoint.Y + newMeanPoint.Y);

                        path.LineTo(pt);

                        prevPoint = pt;
                    }
                }

                {
                    Point meanPoint = Intersections2D.ProjectOnLine(centroid2D, prevPoint, firstPoint);

                    Point  dir    = new Point(meanPoint.X - meanX, meanPoint.Y - meanY);
                    double length = dir.Modulus();

                    Point newMeanPoint = new Point(meanX + dir.X * (length + overfill) / length, meanY + dir.Y * (length + overfill) / length);

                    path.LineTo(prevPoint.X - meanPoint.X + newMeanPoint.X, prevPoint.Y - meanPoint.Y + newMeanPoint.Y);

                    path.LineTo(firstPoint.X - meanPoint.X + newMeanPoint.X, firstPoint.Y - meanPoint.Y + newMeanPoint.Y);
                }
            }
            else
            {
                foreach (Point pt in triangleProjection)
                {
                    path.LineTo(pt);
                }
            }

            path.Close();

            graphics.FillPath(path, colour, tag: tag);
        }
        void panel2_Paint(object sender, GUI.PaintEventArgs e)
        {
            Graphics graphics           = e.Graphics;
            bool     isSoftwareGraphics = graphics is SoftwareGraphics;

            //if (isSoftwareGraphics)
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
            }


            // Two simple paths
            if (rbGeometry5.IsChecked)
            {
                GraphicsPath ps1_path = new GraphicsPath();

                double x = m_X - m_DrawingPanel.ClientRectangle.Center.X / 2 + 100;
                double y = m_Y - m_DrawingPanel.ClientRectangle.Center.Y / 2 + 100;
                ps1_path.MoveTo(x + 140, y + 145);
                ps1_path.LineTo(x + 225, y + 44);
                ps1_path.LineTo(x + 296, y + 219);
                ps1_path.ClosePolygon();

                ps1_path.LineTo(x + 226, y + 289);
                ps1_path.LineTo(x + 82, y + 292);

                ps1_path.MoveTo(x + 220, y + 222);
                ps1_path.LineTo(x + 363, y + 249);
                ps1_path.LineTo(x + 265, y + 331);

                ps1_path.MoveTo(x + 242, y + 243);
                ps1_path.LineTo(x + 268, y + 309);
                ps1_path.LineTo(x + 325, y + 261);

                ps1_path.MoveTo(x + 259, y + 259);
                ps1_path.LineTo(x + 273, y + 288);
                ps1_path.LineTo(x + 298, y + 266);

                Rect ps1Bounds          = ps1_path.Bounds;
                GeometryTransformer ps1 = GeometryMatrix4Transformer.CreateOptimizedTransformer(ps1_path, Matrix4.CreateTranslation(
                                                                                                    m_X - ps1Bounds.Center.X,
                                                                                                    m_Y - ps1Bounds.Center.Y));


                GraphicsPath ps2_path = new GraphicsPath();
                ps2_path.MoveTo(100 + 32, 100 + 77);
                ps2_path.LineTo(100 + 473, 100 + 263);
                ps2_path.LineTo(100 + 351, 100 + 290);
                ps2_path.LineTo(100 + 354, 100 + 374);
                Rect ps2Bounds          = ps2_path.Bounds;
                GeometryTransformer ps2 = GeometryMatrix4Transformer.CreateOptimizedTransformer(ps2_path, Matrix4.CreateTranslation(
                                                                                                    (m_DrawingPanel.Width - ps2Bounds.Width) / 2 - ps2Bounds.Left,
                                                                                                    (m_DrawingPanel.Height - ps2Bounds.Height) / 2 - ps2Bounds.Top));


                graphics.FillGeometry(m_GeometryBColor, ps2);
                graphics.FillGeometry(m_GeometryAColor, ps1);

                CombinedGeometry clp = new CombinedGeometry(ps1, ps2, CombinedGeometry.CombineMode.Or, CombinedGeometry.FillType.NonZero, CombinedGeometry.FillType.NonZero);

                PerformRendering(graphics, clp);
            }
            // Closed stroke
            else if (rbGeometry4.IsChecked)
            {
                GraphicsPath ps1_path = new GraphicsPath();

                double x = m_X - m_DrawingPanel.ClientRectangle.Center.X / 2 + 100;
                double y = m_Y - m_DrawingPanel.ClientRectangle.Center.Y / 2 + 100;
                ps1_path.MoveTo(x + 140, y + 145);
                ps1_path.LineTo(x + 225, y + 44);
                ps1_path.LineTo(x + 296, y + 219);
                ps1_path.ClosePolygon();

                ps1_path.LineTo(x + 226, y + 289);
                ps1_path.LineTo(x + 82, y + 292);

                ps1_path.MoveTo(x + 220 - 50, y + 222);
                ps1_path.LineTo(x + 265 - 50, y + 331);
                ps1_path.LineTo(x + 363 - 50, y + 249);
                ps1_path.ClosePolygon(GeometryVertexCommandAndFlags.FlagCCW);

                Rect ps1Bounds          = ps1_path.Bounds;
                GeometryTransformer ps1 = GeometryMatrix4Transformer.CreateOptimizedTransformer(ps1_path, Matrix4.CreateTranslation(
                                                                                                    m_X - ps1Bounds.Center.X,
                                                                                                    m_Y - ps1Bounds.Center.Y));


                GraphicsPath ps2_path = new GraphicsPath();
                ps2_path.MoveTo(100 + 32, 100 + 77);
                ps2_path.LineTo(100 + 473, 100 + 263);
                ps2_path.LineTo(100 + 351, 100 + 290);
                ps2_path.LineTo(100 + 354, 100 + 374);
                ps2_path.ClosePolygon();
                Rect ps2Bounds          = ps2_path.Bounds;
                GeometryTransformer ps2 = GeometryMatrix4Transformer.CreateOptimizedTransformer(ps2_path, Matrix4.CreateTranslation(
                                                                                                    (m_DrawingPanel.Width - ps2Bounds.Width) / 2 - ps2Bounds.Left,
                                                                                                    (m_DrawingPanel.Height - ps2Bounds.Height) / 2 - ps2Bounds.Top));

                GeometryStroke stroke = new GeometryStroke(ps2, 10);


                graphics.FillGeometry(m_GeometryBColor, stroke);
                graphics.FillGeometry(m_GeometryAColor, ps1);

                CombinedGeometry clp = new CombinedGeometry(ps1, stroke, CombinedGeometry.CombineMode.Or, CombinedGeometry.FillType.NonZero, CombinedGeometry.FillType.NonZero);

                PerformRendering(graphics, clp);
            }
            // Great Britain and Arrows
            else if (rbGeometry3.IsChecked)
            {
                Geometry poly       = GreatBritain;
                Rect     polyBounds = poly.Bounds;
                double   scale      = 3;
                Matrix4  mtx        = Matrix4.CreateTranslation(-polyBounds.Center.X, -polyBounds.Center.Y);
                mtx *= Matrix4.CreateScaling(scale, scale);
                mtx *= Matrix4.CreateTranslation(m_DrawingPanel.Width / 2, m_DrawingPanel.Height / 2);
                GeometryTransformer trans_gb_poly = GeometryMatrix4Transformer.CreateOptimizedTransformer(poly, mtx);

                Geometry arrows       = Arrows;
                Rect     arrowsBounds = arrows.Bounds;
                mtx  = Matrix4.CreateTranslation(-arrowsBounds.Center.X, -arrowsBounds.Center.Y);
                mtx *= Matrix4.CreateScaling(scale, scale);
                mtx *= Matrix4.CreateTranslation(m_X, m_Y);
                GeometryTransformer trans_arrows = GeometryMatrix4Transformer.CreateOptimizedTransformer(arrows, mtx);

                CombinedGeometry clp = new CombinedGeometry(trans_gb_poly, trans_arrows,
                                                            CombinedGeometry.CombineMode.Or,
                                                            CombinedGeometry.FillType.NonZero,
                                                            CombinedGeometry.FillType.NonZero);

                graphics.FillGeometry(m_GeometryBColor, trans_gb_poly);
                graphics.DrawGeometry(isSoftwareGraphics ? m_GreatBritainContourPen_SWR : m_GreatBritainContourPen_HWR, trans_gb_poly);
                graphics.FillGeometry(m_GeometryAColor, trans_arrows);

                PerformRendering(graphics, clp);
            }
            // Great Britain and a Spiral
            else if (rbGeometry2.IsChecked)
            {
                Geometry poly       = GreatBritain;
                Rect     polyBounds = poly.Bounds;
                double   scale      = 3;
                Matrix4  mtx        = Matrix4.CreateTranslation(-polyBounds.Center.X, -polyBounds.Center.Y);
                mtx *= Matrix4.CreateScaling(scale, scale);
                mtx *= Matrix4.CreateTranslation(m_DrawingPanel.Width / 2, m_DrawingPanel.Height / 2);
                GeometryTransformer trans_gb_poly = GeometryMatrix4Transformer.CreateOptimizedTransformer(poly, mtx);

                Geometry       sp     = GeometryMatrix4Transformer.CreateOptimizedTransformer(Spiral, Matrix4.CreateTranslation(m_X, m_Y));
                GeometryStroke stroke = new GeometryStroke(sp, 15);

                CombinedGeometry clp = new CombinedGeometry(trans_gb_poly, stroke,
                                                            CombinedGeometry.CombineMode.Or,
                                                            CombinedGeometry.FillType.NonZero,
                                                            CombinedGeometry.FillType.NonZero);

                graphics.FillGeometry(m_GeometryBColor, trans_gb_poly);
                graphics.DrawGeometry(isSoftwareGraphics ? m_GreatBritainContourPen_SWR : m_GreatBritainContourPen_HWR, trans_gb_poly);
                graphics.DrawGeometry(m_SpiralPen, sp);

                PerformRendering(graphics, clp);
            }
            // Spiral and text
            else if (rbGeometry1.IsChecked)
            {
                Geometry            text            = Text;
                Rect                textBounds      = text.Bounds;
                GeometryTransformer transformedText = GeometryMatrix4Transformer.CreateOptimizedTransformer(text,
                                                                                                            Matrix4.CreateTranslation(
                                                                                                                (m_DrawingPanel.Width - textBounds.Width) / 2 - textBounds.Left,
                                                                                                                (m_DrawingPanel.Height - textBounds.Height) / 2 - textBounds.Top));

                Geometry       sp     = GeometryMatrix4Transformer.CreateOptimizedTransformer(Spiral, Matrix4.CreateTranslation(m_X, m_Y));
                GeometryStroke stroke = new GeometryStroke(sp, 15);

                CombinedGeometry clp = new CombinedGeometry(stroke, transformedText,
                                                            CombinedGeometry.CombineMode.Or,
                                                            CombinedGeometry.FillType.NonZero,
                                                            CombinedGeometry.FillType.NonZero);

                graphics.FillGeometry(m_GeometryBColor, transformedText);
                graphics.DrawGeometry(m_SpiralPen, sp);

                PerformRendering(graphics, clp);
            }
        }