如何从C#中的距离矩阵绘制图形?

时间:2014-08-26 17:36:01

标签: c# algorithm graph distance adjacency-matrix

我目前正在C#中构建一个应用程序,它帮助学生使用和理解我的数学A-Level课程的D1模块中的算法,作为我的COMP4项目的一部分。

这些算法的一部分使用需要作为距离矩阵输入的图形,然后需要将真实比例同构图绘制到窗体上。如下图链接所示。

https://www.dropbox.com/s/k5sl2y98pml8mji/DistanceMatrix.png?dl=0

为简单起见,假设您具有以下功能

DrawLine(int aX, int aY, int bX, int bY) : void
// Draws line to screen given start and end points.

DrawNode(int x, int y) : void
// Draws node circle to screen given x and y co-ordinates of centre of circle.

所以我开始在类

中表示距离矩阵
public class Node : DataItem , INode
{
    public IList<IArc> Arcs { get; set; }
    public Node(string nodeName, IList<IArc> nodeNeighbors)
    {
        this.Name = nodeName;
        Arcs = nodeNeighbors;
    }

    public Node(string nodeName)
    {
        this.Name = nodeName;
        Arcs = new List<IArc>();
    }
}
public class Arc : DataItem, IArc
{
    public Arc(string arcName, decimal arcWeight, INode destinationNode)
    {
        this.Magnitude = arcWeight;
        this.Name = arcName;
        this.DestinationNode = destinationNode;
    }

    public INode DestinationNode { get; set; }
}
public class DataItem : IDataItem
{
    public string Name { get; set; }
    public decimal Magnitude { get; set; }
}

public class GraphDataSet<TNode> : DataSet<TNode>, IGraphSet<TNode> where TNode : INode 
    {
        public int NumberOfNodes { get; set; }
        public GraphDataSet(decimal[,] distanceMatrix, int NumberOfNodes)
        {
            for (int i = 0; i < NumberOfNodes; i++)
            {
                TNode NodeToAdd = default(TNode);
                NodeToAdd.Name = Convert.ToString(i);
            }

            for (int i = 0; i < NumberOfNodes; i++)
            {
                for (int j = 0; j < NumberOfNodes; j++)
                {
                    if (distanceMatrix[i, j] != 0)
                    {
                        INode Destination = GetItem(i);
                        GetItem(i).Arcs.Add(new Arc(i + ":" + Destination.Name ,distanceMatrix[i, j], Destination));
                    }
                }
            }
        }
    }

我已经到了这一步并确保输入的数据被正确显示但现在我不确定在实际将项目绘制到屏幕方面的去处。

我考虑过不同的策略,例如在屏幕上绘制一种队列,其中详细说明了。如果发生碰撞或者图表开始离开屏幕,但是从长远来看看它无法正常工作,则会抛出异常。

如果它在语言功能方面有任何不同,我使用c#.net,框架版本限制为4.0.3(由于大学计算机)。

如果问题在这种情况下过于复杂,我很乐意使用外部库,但如果可能的话,我更愿意使用我自己的代码。

欢迎所有建议和意见。

0 个答案:

没有答案