1

I am currently building an an application in C# which helps students use and understand the algorithms in the D1 module of my Maths A-Level course as part of my COMP4 project.

A portion of these algorithms use graphs which need to be inputted as a distance matrix, then a true scale isomorphic graph needs to be drawn onto a windows form. As shown in the diagram linked below.

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

For the sake of simplicity assume you have the following functions

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.

So I started by representing the distance matrix in classes

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));
                    }
                }
            }
        }
    }

I have got to this point and ensured that inputted data is represented correctly but now I am not sure where to go in terms of actually drawing the item to the screen.

I have considered different strategies such as having an off screen drawing kind of queue where the details are drafted to. An exception is thrown if collisions occur or if the graph starts going off screen, but cannot see it working in the long run.

If it makes any difference in terms of language features I am using c#.net with framework version limited to 4.0.3 (due to college computers).

I am happy to use an external library if the problem is too complex in this context but would prefer to use my own code if possible.

All suggestions and advice welcome.

Edward J Brown
  • 333
  • 1
  • 5
  • 17
  • Don't worry about it going off screen. If internally you create a canvas that's 2X (width and height) the largest distance, then you can build it without worrying about boundaries. When you're done, draw a bounding box around the drawn portion and translate that to the origin for display. – Jim Mischel Aug 26 '14 at 19:22
  • Also take a look at the related questions to the right. Particularly http://stackoverflow.com/questions/18911994/visualize-distance-matrix-as-a-graph, which might give you some good ideas. – Jim Mischel Aug 26 '14 at 19:29
  • Googling _distance matrix_ results in an amazing variety of images.. You may want to point us to one or two that look like what you want to create. – TaW Aug 27 '14 at 11:59
  • Thanks guys for the help so far, the main issue is converting the points into actual x and y co-ordinates on the screen, it might be easier to work on a grid, (like this one: http://www.mr-d-n-t.co.uk/Graphics%20Resources/DRAWING/isomgrid.gif) and then limit it so that one single node may only be connected to 6 other nodes. I've added an image link to give people and idea on what I wish to achieve. @JimMischel I like the suggestion but I'm not sure on the implementation, any idea how to do the translation you describe when working with .NET graphics? – Edward J Brown Aug 27 '14 at 15:52

0 Answers0