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