Skip to content

Commit 7fd28f8

Browse files
author
unknown
committed
part1
0 parents  commit 7fd28f8

11 files changed

+584
-0
lines changed

AutoLayoutApplication.csproj

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>9.0.30729</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>AutoLayoutApplication</RootNamespace>
12+
<AssemblyName>AutoLayoutApplication</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<TargetFrameworkSubset>
16+
</TargetFrameworkSubset>
17+
<StartupObject>
18+
</StartupObject>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="System" />
39+
<Reference Include="System.Core">
40+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
41+
</Reference>
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Deployment" />
44+
<Reference Include="System.Drawing" />
45+
<Reference Include="System.Windows.Forms" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="NodeCollection.cs" />
50+
<Compile Include="Node.cs" />
51+
<Compile Include="Vector.cs" />
52+
<Compile Include="Program.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<EmbeddedResource Include="Properties\Resources.resx">
55+
<Generator>ResXFileCodeGenerator</Generator>
56+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
57+
<SubType>Designer</SubType>
58+
</EmbeddedResource>
59+
<Compile Include="Properties\Resources.Designer.cs">
60+
<AutoGen>True</AutoGen>
61+
<DependentUpon>Resources.resx</DependentUpon>
62+
<DesignTime>True</DesignTime>
63+
</Compile>
64+
<None Include="Properties\Settings.settings">
65+
<Generator>SettingsSingleFileGenerator</Generator>
66+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
67+
</None>
68+
<Compile Include="Properties\Settings.Designer.cs">
69+
<AutoGen>True</AutoGen>
70+
<DependentUpon>Settings.settings</DependentUpon>
71+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
72+
</Compile>
73+
</ItemGroup>
74+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
75+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
76+
Other similar extension points exist, see Microsoft.Common.targets.
77+
<Target Name="BeforeBuild">
78+
</Target>
79+
<Target Name="AfterBuild">
80+
</Target>
81+
-->
82+
</Project>

AutoLayoutApplication.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 10.00
3+
# Visual Studio 2008
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoLayoutApplication", "AutoLayoutApplication.csproj", "{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{BF45783A-8A0D-4BEB-BBAC-B850A53DD379}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

Node.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace AutoLayoutApplication
5+
{
6+
public class Node
7+
{
8+
public string Name { get; private set; }
9+
public ICollection<Node> Neighbors { get; private set; }
10+
11+
public Vector R { get; set; }
12+
public Vector V { get; set; }
13+
14+
public Node(string name)
15+
{
16+
this.Name = name;
17+
this.Neighbors = new List<Node>();
18+
}
19+
20+
public Vector GetSpringForce(Node n)
21+
{
22+
// ばねの力は自然長からの変位に比例 (比例定数 -k, ばねの長さ l)
23+
const double k = 0.1d;
24+
const double l = 60.0d;
25+
double dx = this.R.X - n.R.X;
26+
double dy = this.R.Y - n.R.Y;
27+
double d2 = dx * dx + dy * dy;
28+
if (d2 < double.Epsilon)
29+
{
30+
// 距離0の時は例外として乱数で決定
31+
Random rand = new Random();
32+
return new Vector()
33+
{
34+
X = rand.NextDouble() - 0.5d,
35+
Y = rand.NextDouble() - 0.5d
36+
};
37+
}
38+
double d = Math.Sqrt(d2);
39+
double cos = dx / d;
40+
double sin = dy / d;
41+
double dl = d - l;
42+
return new Vector()
43+
{
44+
X = -k * dl * cos,
45+
Y = -k * dl * sin
46+
};
47+
}
48+
49+
public Vector GetReplusiveForce(Node n)
50+
{
51+
// 反発は距離の2乗に反比例 (比例定数 g)
52+
const double g = 500.0d;
53+
double dx = this.R.X - n.R.X;
54+
double dy = this.R.Y - n.R.Y;
55+
double d2 = dx * dx + dy * dy;
56+
if (d2 < double.Epsilon)
57+
{
58+
// 距離0の時は例外として乱数で決定
59+
Random rand = new Random();
60+
return new Vector()
61+
{
62+
X = rand.NextDouble() - 0.5d,
63+
Y = rand.NextDouble() - 0.5d
64+
};
65+
}
66+
double d = Math.Sqrt(d2);
67+
double cos = dx / d;
68+
double sin = dy / d;
69+
return new Vector()
70+
{
71+
X = g / d2 * cos,
72+
Y = g / d2 * sin
73+
};
74+
}
75+
76+
public Vector GetFrictionalForce()
77+
{
78+
// 摩擦力は速度に比例 (比例定数 -m)
79+
const double m = 0.3d;
80+
return new Vector()
81+
{
82+
X = -m * this.V.X,
83+
Y = -m * this.V.Y
84+
};
85+
}
86+
87+
public void MoveEular(double dt, Vector f)
88+
{
89+
// 質量は1とする
90+
this.R = new Vector()
91+
{
92+
X = this.R.X + dt * this.V.X,
93+
Y = this.R.Y + dt * this.V.Y
94+
};
95+
this.V = new Vector()
96+
{
97+
X = this.V.X + dt * f.X,
98+
Y = this.V.Y + dt * f.Y
99+
};
100+
}
101+
}
102+
}

NodeCollection.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
3+
namespace AutoLayoutApplication
4+
{
5+
public class NodeCollection : Dictionary<string, Node>
6+
{
7+
public NodeCollection Add(params string[] names)
8+
{
9+
foreach (string name in names)
10+
{
11+
this[name] = new Node(name);
12+
}
13+
return this;
14+
}
15+
16+
public NodeCollection Link(params string[] links)
17+
{
18+
foreach (string link in links)
19+
{
20+
string[] n = link.Split(',');
21+
this[n[0]].Neighbors.Add(this[n[1]]);
22+
this[n[1]].Neighbors.Add(this[n[0]]);
23+
}
24+
return this;
25+
}
26+
27+
public void MoveAll()
28+
{
29+
const double dt = 0.1d;
30+
foreach (Node n in this.Values)
31+
{
32+
Vector f = new Vector();
33+
foreach (Node nn in n.Neighbors)
34+
{
35+
f += n.GetSpringForce(nn);
36+
}
37+
foreach (Node nn in this.Values)
38+
{
39+
if (n != nn)
40+
{
41+
f += n.GetReplusiveForce(nn);
42+
}
43+
}
44+
f += n.GetFrictionalForce();
45+
n.MoveEular(dt, f);
46+
}
47+
}
48+
49+
}
50+
}

Program.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
3+
namespace AutoLayoutApplication
4+
{
5+
static class Program
6+
{
7+
static void Main()
8+
{
9+
NodeCollection nodes = CreateGraph();
10+
Circularize(nodes, 200.0);
11+
12+
for (int i = 0; i < 1000; i++)
13+
{
14+
nodes.MoveAll();
15+
}
16+
foreach (Node n in nodes.Values)
17+
{
18+
Console.WriteLine("{0} ({1}, {2})", n.Name, n.R.X, n.R.Y);
19+
}
20+
}
21+
22+
static NodeCollection CreateGraph()
23+
{
24+
return new NodeCollection()
25+
.Add("a", "a'", "b", "b'", "c", "c'",
26+
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
27+
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
28+
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29")
29+
.Link("0,1", "0,10", "0,21", "1,2", "1,28",
30+
"2,3", "2,6", "3,4", "3,b'", "4,5",
31+
"4,8", "5,6", "5,7", "6,11", "7,14",
32+
"7,c'", "8,9", "8,10", "9,16", "9,a'",
33+
"10,29", "11,14", "11,28", "12,13", "12,20",
34+
"12,27", "13,14", "13,19", "15,16", "15,18",
35+
"15,29", "16,17", "17,18", "17,c", "18,26",
36+
"19,22", "19,28", "20,22", "20,25", "21,24",
37+
"21,29", "22,23", "23,24", "23,a", "24,25",
38+
"25,26", "26,27", "27,b");
39+
}
40+
41+
static void Circularize(NodeCollection nodes, double r)
42+
{
43+
int count = nodes.Count;
44+
double dtheta = 2.0d * Math.PI / (double)count;
45+
double theta = 0.0d;
46+
foreach (Node n in nodes.Values)
47+
{
48+
n.R = new Vector()
49+
{
50+
X = r * Math.Cos(theta),
51+
Y = r * Math.Sin(theta)
52+
};
53+
n.V = new Vector()
54+
{
55+
X = 0.0d,
56+
Y = 0.0d
57+
};
58+
theta += dtheta;
59+
}
60+
}
61+
62+
}
63+
}

Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
6+
// アセンブリに関連付けられている情報を変更するには、
7+
// これらの属性値を変更してください。
8+
[assembly: AssemblyTitle("WindowsFormsApplication1")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("WindowsFormsApplication1")]
13+
[assembly: AssemblyCopyright("Copyright © 2009")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから
18+
// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、
19+
// その型の ComVisible 属性を true に設定してください。
20+
[assembly: ComVisible(false)]
21+
22+
// 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です
23+
[assembly: Guid("5564249e-4af9-47ed-9fd7-ea8b164be5d8")]
24+
25+
// アセンブリのバージョン情報は、以下の 4 つの値で構成されています:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
33+
// 既定値にすることができます:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)