Пример #1
0
        protected void DrawMaze(Network _network, Graphics g)
        {
            g.Clear(colBackground);

            Pen        p          = new Pen(colWall, 1f);
            SolidBrush brush      = new SolidBrush(colCorridor);
            SolidBrush brushDebug = new SolidBrush(Color.LightGray);

            foreach (Node n in _network.nodeDict.Keys)
            {
                ShapeNode s      = (ShapeNode)n;
                PointF    centre = s.GetCentre();

                // Create centre area and region fill it.
                PointF[] centrePoints = s.points.ToArray();

                for (int pointIndex = 0; pointIndex < s.points.Count; pointIndex++)
                {
                    Vector2D vec = new Vector2D(centre, s.points[pointIndex]);
                    vec.Scale((float)corridorWidthCtrl.Value);

                    centrePoints[pointIndex].X = vec.b.X * (float)scaleCtrl.Value;
                    centrePoints[pointIndex].Y = vec.b.Y * (float)scaleCtrl.Value;
                }

                g.FillPolygon(/*brushDebug*/ brush, centrePoints);

                // Render edges
                for (int pointIndex = 0; pointIndex < s.points.Count; pointIndex++)
                {
                    int nextIndex = (pointIndex + 1) % s.points.Count;

                    PointF centre1 = centrePoints[pointIndex];
                    PointF centre2 = centrePoints[nextIndex];

                    if (s.LinkList[pointIndex] == null || s.LinkList[pointIndex].visited == false)
                    {
                        // Edge unvisited
                        // Draw a line along edge of centre area
                        g.DrawLine(p, centre1, centre2);
                    }
                    else
                    {
                        // Calculate outer edge (in screen space)
                        PointF p1 = s.points[pointIndex];
                        PointF p2 = s.points[nextIndex];
                        p1.X *= (float)scaleCtrl.Value;
                        p1.Y *= (float)scaleCtrl.Value;
                        p2.X *= (float)scaleCtrl.Value;
                        p2.Y *= (float)scaleCtrl.Value;

                        // worldScale outer edge by corridor size
                        Vector2D vec = new Vector2D(p1, p2);
                        vec.Scale(0.5f - (0.5f * (float)corridorWidthCtrl.Value));
                        PointF outer1 = vec.b;

                        vec.a = p1;
                        vec.b = p2;
                        vec.Scale(0.5f + (0.5f * (float)corridorWidthCtrl.Value));
                        PointF outer2 = vec.b;

                        // Fill region between outer and inner edge
                        PointF[] poly = { outer1, centre1, centre2, outer2 };
                        g.FillPolygon(brush, poly);

                        // Draw lines from outer edge points -> centre edge points.
                        g.DrawLine(p, outer1, centre1);
                        g.DrawLine(p, outer2, centre2);
                    }
                }
            }
        }
Пример #2
0
        protected void DrawNodes(Graphics g, Pen p)
        {
            PointF[] renderPoints = new PointF[4];
            Vector2D v;

            foreach (KeyValuePair <Node, List <NodeLink> > kvp in network.nodeDict)
            {
                ShapeNode n = kvp.Key as ShapeNode;

                PointF centre       = n.GetCentre();
                bool   bBezierDrawn = false;

                for (int edgeIndex1 = 0; edgeIndex1 < n.points.Count; edgeIndex1++)
                {
                    NodeLink link1 = n.LinkList[edgeIndex1];

                    if (link1 != null && link1.visited == true)
                    {
                        renderPoints[0] = n.GetEdgeCentre(edgeIndex1);

                        v = new Vector2D(renderPoints[0], centre);
                        v.Scale((float)curveTension.Value);

                        renderPoints[1] = v.b;

                        renderPoints[0].X *= (int)nodeSize.Value;
                        renderPoints[0].Y *= (int)nodeSize.Value;

                        renderPoints[1].X *= (int)nodeSize.Value;
                        renderPoints[1].Y *= (int)nodeSize.Value;

                        DrawCircle(g, renderPoints[0], p.Width, p.Color);

                        for (int edgeIndex2 = (edgeIndex1 + 1); edgeIndex2 < n.points.Count; edgeIndex2++)
                        {
                            NodeLink link2 = n.LinkList[edgeIndex2];
                            if (link2 != null && link2 != link1 && link2.visited == true)
                            {
                                renderPoints[3] = n.GetEdgeCentre(edgeIndex2);

                                v = new Vector2D(renderPoints[3], centre);
                                v.Scale((float)curveTension.Value);

                                renderPoints[3].X *= (int)nodeSize.Value;
                                renderPoints[3].Y *= (int)nodeSize.Value;

                                renderPoints[2]    = v.b;
                                renderPoints[2].X *= (int)nodeSize.Value;
                                renderPoints[2].Y *= (int)nodeSize.Value;

                                g.DrawBeziers(p, renderPoints);
                                bBezierDrawn = true;
                            }
                        }

                        if (bBezierDrawn == false)
                        {
                            // A dead end
                            renderPoints[1]    = centre;
                            renderPoints[1].X *= (int)nodeSize.Value;
                            renderPoints[1].Y *= (int)nodeSize.Value;

                            g.DrawLine(p, renderPoints[0], renderPoints[1]);
                            DrawCircle(g, renderPoints[1], p.Width, p.Color);
                        }
                    }
                }
            }
        }