Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/Force/SpringForce.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Netron.Diagramming.Core.Layout.Force
6{
7
8    /// <summary>
9    ///  Force function that computes the force acting on ForceItems due to a  given Spring.
10    /// </summary>
11    public class SpringForce : AbstractForce
12    {
13
14        #region Fields
15        Random rnd = new Random();
16        private static String[] pnames = new String[] { "SpringCoefficient", "DefaultSpringLength" };
17        public static float DefaultSpriongCoeff = 1E-4f;
18        public static float DefaultMaxSpringCoeff = 1E-3f;
19        public static float DefaultMinSpringCoeff = 1E-5f;
20        public static float DefaultSpringLength = 50;
21        public static float DefaultMinSpringLength = 0;
22        public static float DefaultMaxSpringLength = 200;
23        public static int SpringCoeff = 0;
24        public static int SpringLength = 1;
25
26        #endregion
27
28        #region Properties
29        /// <summary>
30        /// Gets a value indicating whether [spring force].
31        /// </summary>
32        /// <value><c>true</c> if [spring force]; otherwise, <c>false</c>.</value>
33        public override bool IsSpringForce
34        {
35            get
36            {
37                return true;
38            }
39        }
40        /// <summary>
41        /// Gets the parameter names.
42        /// </summary>
43        /// <value></value>
44        /// <returns></returns>
45        protected override String[] ParameterNames
46        {
47            get
48            {
49                return pnames;
50            }
51        }
52        #endregion
53
54        #region Constructor
55        /// <summary>
56        ///  Create a new SpringForce.
57        /// </summary>
58        /// <param name="springCoeff"> the default spring co-efficient to use. This will be used if the spring's own co-efficient is less than zero..</param>
59        /// <param name="defaultLength">the default spring length to use. This will be used if the spring's own length is less than zero.</param>
60        public SpringForce(float springCoeff, float defaultLength)
61        {
62            parms = new float[] { springCoeff, defaultLength };
63            minValues = new float[] { DefaultMinSpringCoeff, DefaultMinSpringLength };
64            maxValues = new float[] { DefaultMaxSpringCoeff, DefaultMaxSpringLength };
65        }
66
67        /// <summary>
68        /// Constructs a new SpringForce instance with default parameters.
69        /// </summary>
70        public SpringForce()
71            : this(DefaultSpriongCoeff, DefaultSpringLength)
72        {
73        }
74        #endregion
75
76        #region Methdos
77        /// <summary>
78        /// Gets the force.
79        /// </summary>
80        /// <param name="s">The s.</param>
81        public override void GetForce(Spring s)
82        {
83            ForceItem item1 = s.Item1;
84            ForceItem item2 = s.Item2;
85            float length = (s.Length < 0 ? parms[SpringLength] : s.Length);
86            float x1 = item1.Location[0], y1 = item1.Location[1];
87            float x2 = item2.Location[0], y2 = item2.Location[1];
88            float dx = x2 - x1, dy = y2 - y1;
89            float r = (float)Math.Sqrt(dx * dx + dy * dy);
90            if (r == 0.0)
91            {
92                dx = ((float)rnd.NextDouble() - 0.5f) / 50.0f;
93                dy = ((float)rnd.NextDouble() - 0.5f) / 50.0f;
94                r = (float)Math.Sqrt(dx * dx + dy * dy);
95            }
96            float d = r - length;
97            float coeff = (s.Coeff < 0 ? parms[SpringCoeff] : s.Coeff) * d / r;
98            item1.Force[0] += coeff * dx;
99            item1.Force[1] += coeff * dy;
100            item2.Force[0] += -coeff * dx;
101            item2.Force[1] += -coeff * dy;
102        }
103
104        #endregion
105
106    }
107}
Note: See TracBrowser for help on using the repository browser.