-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEpsilonComparer.cs
More file actions
53 lines (42 loc) · 1.42 KB
/
EpsilonComparer.cs
File metadata and controls
53 lines (42 loc) · 1.42 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
using System;
using System.Collections.Generic;
using GeoAPI.Coordinates;
using NPack;
namespace SharpMap.Tests
{
public class EpsilonComparer : IComparer<Double>, IComparer<ICoordinate>
{
public static readonly Double DefaultEpsilon = 0.0005;
public static EpsilonComparer Default = new EpsilonComparer(DefaultEpsilon);
private readonly Double _epsilon;
private EpsilonComparer(Double epsilon)
{
_epsilon = epsilon;
}
public Int32 Compare(Double x, Double y)
{
Double difference = Math.Abs(x) - Math.Abs(y);
Int32 result = _epsilon.CompareTo(difference);
return result >= 0 ? 0 : x.CompareTo(y);
}
public Int32 Compare(ICoordinate x, ICoordinate y)
{
DoubleComponent[] xComponents = x.Components;
DoubleComponent[] yComponents = y.Components;
Int32 result = xComponents.Length.CompareTo(yComponents.Length);
if (result != 0)
{
return result;
}
for (Int32 i = 0; i < xComponents.Length; i++)
{
result = Compare((Double)xComponents[i], (Double)yComponents[i]);
if (result != 0)
{
return result;
}
}
return 0;
}
}
}