Browse Source

initial commit

bmallred 12 years ago
commit
85a3a9d4ea

+ 1 - 0
.gitignore

@ -0,0 +1 @@
1
bin/

+ 20 - 0
EuclideanDistance.sln

@ -0,0 +1,20 @@
1

2
Microsoft Visual Studio Solution File, Format Version 11.00
3
# Visual Studio 2010
4
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EuclideanDistance", "EuclideanDistance\EuclideanDistance.csproj", "{110100C8-2E96-45B3-BC9F-F7996D2783E9}"
5
EndProject
6
Global
7
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
8
		Debug|x86 = Debug|x86
9
		Release|x86 = Release|x86
10
	EndGlobalSection
11
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
12
		{110100C8-2E96-45B3-BC9F-F7996D2783E9}.Debug|x86.ActiveCfg = Debug|x86
13
		{110100C8-2E96-45B3-BC9F-F7996D2783E9}.Debug|x86.Build.0 = Debug|x86
14
		{110100C8-2E96-45B3-BC9F-F7996D2783E9}.Release|x86.ActiveCfg = Release|x86
15
		{110100C8-2E96-45B3-BC9F-F7996D2783E9}.Release|x86.Build.0 = Release|x86
16
	EndGlobalSection
17
	GlobalSection(MonoDevelopProperties) = preSolution
18
		StartupItem = EuclideanDistance\EuclideanDistance.csproj
19
	EndGlobalSection
20
EndGlobal

+ 27 - 0
EuclideanDistance/AssemblyInfo.cs

@ -0,0 +1,27 @@
1
using System.Reflection;
2
using System.Runtime.CompilerServices;
3
4
// Information about this assembly is defined by the following attributes. 
5
// Change them to the values specific to your project.
6
7
[assembly: AssemblyTitle("EuclideanDistance")]
8
[assembly: AssemblyDescription("")]
9
[assembly: AssemblyConfiguration("")]
10
[assembly: AssemblyCompany("")]
11
[assembly: AssemblyProduct("")]
12
[assembly: AssemblyCopyright("Bryan Allred")]
13
[assembly: AssemblyTrademark("")]
14
[assembly: AssemblyCulture("")]
15
16
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19
20
[assembly: AssemblyVersion("1.0.*")]
21
22
// The following attributes are used to specify the signing key for the assembly, 
23
// if desired. See the Mono documentation for more information about signing.
24
25
//[assembly: AssemblyDelaySign(false)]
26
//[assembly: AssemblyKeyFile("")]
27

+ 57 - 0
EuclideanDistance/DoubleExtensions.cs

@ -0,0 +1,57 @@
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
namespace EuclideanDistance
6
{
7
	/// <summary>
8
	/// Double extensions.
9
	/// </summary>
10
	public static class DoubleExtensions
11
	{
12
		/// <summary>
13
		/// Euclideans the distance.
14
		/// </summary>
15
		/// <returns>
16
		/// The distance.
17
		/// </returns>
18
		/// <param name='pointA'>
19
		/// Point a.
20
		/// </param>
21
		/// <param name='pointB'>
22
		/// Point b.
23
		/// </param>
24
		public static double EuclideanDistance(this IEnumerable<double> pointA, IEnumerable<double> pointB)
25
		{
26
			// Make sure the dimensions are the same.
27
			if (pointA.Count() != pointB.Count())
28
			{
29
				throw new ArgumentOutOfRangeException("Dimensions do not match");
30
			}
31
32
			// Iterate through each point and create the vector.
33
			var vectors = new List<double>();
34
			for (int i = 0; i < pointA.Count(); i++)
35
			{
36
				vectors.Add(Math.Pow((pointA.ElementAt(i) + pointB.ElementAt(i)), 2));
37
			}
38
39
			// Return the sqare root of the sum of vectors.
40
			return Math.Sqrt(vectors.Sum());
41
		}
42
43
		/// <summary>
44
		/// Tos the point.
45
		/// </summary>
46
		/// <returns>
47
		/// The point.
48
		/// </returns>
49
		/// <param name='point'>
50
		/// Point.
51
		/// </param>
52
		public static string ToPoint(this IEnumerable<double> point)
53
		{
54
			return string.Format("({0})", string.Join(",", point));
55
		}
56
	}
57
}

+ 44 - 0
EuclideanDistance/EuclideanDistance.csproj

@ -0,0 +1,44 @@
1
<?xml version="1.0" encoding="utf-8"?>
2
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
  <PropertyGroup>
4
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6
    <ProductVersion>10.0.0</ProductVersion>
7
    <SchemaVersion>2.0</SchemaVersion>
8
    <ProjectGuid>{110100C8-2E96-45B3-BC9F-F7996D2783E9}</ProjectGuid>
9
    <OutputType>Exe</OutputType>
10
    <RootNamespace>EuclideanDistance</RootNamespace>
11
    <AssemblyName>EuclideanDistance</AssemblyName>
12
  </PropertyGroup>
13
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
14
    <DebugSymbols>true</DebugSymbols>
15
    <DebugType>full</DebugType>
16
    <Optimize>false</Optimize>
17
    <OutputPath>bin\Debug</OutputPath>
18
    <DefineConstants>DEBUG;</DefineConstants>
19
    <ErrorReport>prompt</ErrorReport>
20
    <WarningLevel>4</WarningLevel>
21
    <PlatformTarget>x86</PlatformTarget>
22
    <Externalconsole>true</Externalconsole>
23
  </PropertyGroup>
24
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
25
    <DebugType>none</DebugType>
26
    <Optimize>true</Optimize>
27
    <OutputPath>bin\Release</OutputPath>
28
    <ErrorReport>prompt</ErrorReport>
29
    <WarningLevel>4</WarningLevel>
30
    <PlatformTarget>x86</PlatformTarget>
31
    <Externalconsole>true</Externalconsole>
32
  </PropertyGroup>
33
  <ItemGroup>
34
    <Reference Include="System" />
35
    <Reference Include="System.Core" />
36
  </ItemGroup>
37
  <ItemGroup>
38
    <Compile Include="Main.cs" />
39
    <Compile Include="AssemblyInfo.cs" />
40
    <Compile Include="DoubleExtensions.cs" />
41
    <Compile Include="StringExtensions.cs" />
42
  </ItemGroup>
43
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
44
</Project>

+ 87 - 0
EuclideanDistance/Main.cs

@ -0,0 +1,87 @@
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
namespace EuclideanDistance
6
{
7
	/// <summary>
8
	/// Main class.
9
	/// </summary>
10
	public class MainClass
11
	{
12
		/// <summary>
13
		/// The entry point of the program, where the program control starts and ends.
14
		/// </summary>
15
		/// <param name='args'>
16
		/// The command-line arguments.
17
		/// </param>
18
		public static void Main(string[] args)
19
		{
20
			var points = new List<IEnumerable<double>>();
21
			IEnumerable<double> input;
22
23
			// Accumalate the intial data points needed before computation.
24
			Console.WriteLine("Please enter the initial points");
25
			while (true)
26
			{
27
				Console.Write("Point [blank to stop]: ");
28
				var p = Console.ReadLine().ToPoint();
29
30
				if (p == null)
31
				{
32
					if (points.Count() > 0)
33
					{
34
						break;
35
					}
36
					else
37
					{
38
						Console.WriteLine("No points have been entered.");
39
					}
40
				}
41
				else if (points.Count() > 0 && p.Count() != points.First().Count())
42
				{
43
					Console.WriteLine("Dimensions do not match.");
44
				}
45
				else
46
				{
47
					points.Add(p);
48
				}
49
			}
50
51
			// Retrieve the focal point to determine our distances.
52
			Console.WriteLine();
53
			Console.WriteLine("Please enter a focal point");
54
			while (true)
55
			{
56
				Console.Write("Point: ");
57
				var p = Console.ReadLine().ToPoint();
58
59
				if (p == null)
60
				{
61
					Console.WriteLine("Invalid input. Please try again.");
62
				}
63
				else if (p.Count() != points.First().Count())
64
				{
65
					Console.WriteLine("Dimensions do not match.");
66
				}
67
				else
68
				{
69
					input = p;
70
					break;
71
				}
72
			}
73
74
			// Store all the distances.
75
			var distances = new List<double>();
76
			foreach (var p in points)
77
			{
78
				distances.Add(input.EuclideanDistance(p));
79
			}
80
81
			// Display both the closest and furthest data points from the focal point.
82
			Console.WriteLine();
83
			Console.WriteLine("Minimum distance: {0}", points[distances.IndexOf(distances.Min())].ToPoint());
84
			Console.WriteLine("Maximum distance: {0}", points[distances.IndexOf(distances.Max())].ToPoint());
85
		}
86
	}
87
}

+ 28 - 0
EuclideanDistance/StringExtensions.cs

@ -0,0 +1,28 @@
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
namespace EuclideanDistance
6
{
7
	/// <summary>
8
	/// String extensions.
9
	/// </summary>
10
	public static class StringExtensions
11
	{
12
		/// <summary>
13
		/// Tos the point.
14
		/// </summary>
15
		/// <returns>
16
		/// The point.
17
		/// </returns>
18
		/// <param name='input'>
19
		/// Input.
20
		/// </param>
21
		public static IEnumerable<double> ToPoint(this string input)
22
		{
23
			return string.IsNullOrWhiteSpace(input)
24
				? null
25
				: input.Split(new char[] { ',' }).Select(Double.Parse);
26
		}
27
	}
28
}