-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
80 lines (71 loc) · 3.08 KB
/
Copy pathProgram.cs
File metadata and controls
80 lines (71 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Diagnostics;
using System.Globalization;
namespace KDTree;
class Program
{
static void Main(string[] args)
{
string? filePath = null;
int pointsAmount = 1_000_000;
double targetX = 157;
double targetY = 19;
int width = 200;
int height = 200;
foreach (var arg in args)
{
if (arg == "-h" || arg == "/?" || arg == "--help")
{
ShowHelp();
return;
}
else if (arg.StartsWith("--file=")) filePath = arg.Substring(7);
else if (arg.StartsWith("--count=")) pointsAmount = int.Parse(arg.Substring(8));
else if (arg.StartsWith("--target="))
{
var coords = arg.Substring(9).Split(',');
targetX = double.Parse(coords[0], CultureInfo.InvariantCulture);
targetY = double.Parse(coords[1], CultureInfo.InvariantCulture);
}
else if (arg.StartsWith("--size="))
{
var size = arg.Substring(7).Split(',');
width = int.Parse(size[0]);
height = int.Parse(size[1]);
}
}
List<Point> points;
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
points = Utils.ReadPointsFromFile(filePath);
}
else
{
points = Utils.GetRandomPoints(pointsAmount, width, height);
}
Point targetPoint = new Point(targetX, targetY);
Console.WriteLine($"Target Point = {targetPoint.X} : {targetPoint.Y}");
var sw = Stopwatch.StartNew();
var kdTree = new KDTree(points);
Console.WriteLine($"Tree Depth = {kdTree.Depth}");
Console.WriteLine($"Build tree time: {sw.ElapsedMilliseconds} ms");
sw.Restart();
var neighborTree = kdTree.FindNeighbor(targetPoint);
Console.WriteLine($"Point from Tree = {neighborTree.X} : {neighborTree.Y}");
Console.WriteLine($"Distance from target point = {Utils.GetDistance(targetPoint, neighborTree)}");
Console.WriteLine($"Time from Tree: {sw.ElapsedMilliseconds} ms");
sw.Restart();
var neighborArray = Utils.GetNearestFromArray(points, targetPoint);
Console.WriteLine($"Point from Array = {neighborArray.X} : {neighborArray.Y}");
Console.WriteLine($"Distance from target point = {Utils.GetDistance(targetPoint, neighborArray)}");
Console.WriteLine($"Time from Array: {sw.ElapsedMilliseconds} ms");
}
static void ShowHelp()
{
Console.WriteLine("KDTree Console App Usage:");
Console.WriteLine(" -h, /?, --help Show help");
Console.WriteLine(" --file=<path> Input file path (optional)");
Console.WriteLine(" --count=<number> Number of random points to generate (default: 1_000_000)");
Console.WriteLine(" --target=x,y Target point coordinates (default: 157,19)");
Console.WriteLine(" --size=width,height Width and height of rectangle for random generation (default: 200x200)");
}
}