示例#1
0
        /// <summary>
        /// Set node coordinates on all nodes
        /// </summary>
        /// <param name="nodeLayoutDataList">list of nodes to set coordinates on</param>
        private void SetNodeCoordinates(IEnumerable<NodeLayoutData> nodeLayoutDataList)
        {
            double? minX = 0;
            double? maxX = 0;
            double? minY = 0;
            double? maxY = 0;

            NodePointList nodeCoordinateData = new NodePointList();

            foreach (NodeLayoutData nodeData in nodeLayoutDataList)
            {
                if (minX == null || minX > nodeData.Coordinates.X)
                {
                    minX = nodeData.Coordinates.X;
                }

                if (maxX == null || maxX < nodeData.Coordinates.X)
                {
                    maxX = nodeData.Coordinates.X;
                }

                if (minY == null || minY > nodeData.Coordinates.Y)
                {
                    minY = nodeData.Coordinates.Y;
                }

                if (maxY == null || maxY < nodeData.Coordinates.Y)
                {
                    maxY = nodeData.Coordinates.Y;
                }
            }

            if (minX != null && maxX != null && minY != null && maxX != null)
            {
                double xRange = maxX.Value - minX.Value;
                double yRange = maxY.Value - minY.Value;

                foreach (NodeLayoutData nodeData in nodeLayoutDataList)
                {
                    double x = ((nodeData.Coordinates.X - minX.Value) / xRange) * MaximumScaledX;
                    double y = ((nodeData.Coordinates.Y - minY.Value) / yRange) * MaximumScaledY;

                    //x = nodeData.Coordinates.X;
                    //y = nodeData.Coordinates.Y;

                    nodeCoordinateData.Add(new NodePoint() { Node = nodeData.Node, Coordinate = new Point(x, y) });
                }
            }

            double minDist = double.MaxValue;
            foreach (NodePoint np in nodeCoordinateData)
            {
                foreach (NodePoint np2 in nodeCoordinateData)
                {
                    if (np != np2)
                    {
                        double dx = np.Coordinate.X - np2.Coordinate.X;
                        double dy = np.Coordinate.Y - np2.Coordinate.Y;

                        double distance = Math.Sqrt((Math.Pow(dx, 2) + Math.Pow(dy, 2)));

                        if (distance < minDist)
                        {
                            minDist = distance;
                        }
                    }
                }
            }

            // avoid rendering invalid solutions
            if (minDist > _defaultSpringStableDistance / 10.0)
            {
                _setNodeCoordinatesDelegate(nodeCoordinateData);
            }
        }