| package cluster |
|
|
| import ( |
| "math" |
| "testing" |
|
|
| "github.com/stretchr/testify/assert" |
| ) |
|
|
| var _ WeightedLocatable[weightedPoint, float64, float64] = (*weightedPoint)(nil) |
|
|
| |
| type weightedPoint struct { |
| X, Y, W float64 |
| } |
|
|
| func (a weightedPoint) DistanceTo(b weightedPoint) float64 { |
| dx := a.X - b.X |
| dy := a.Y - b.Y |
| return math.Hypot(dx, dy) |
| } |
|
|
| func (a weightedPoint) Weight() float64 { |
| return a.W |
| } |
|
|
| func TestGroupByDistanceAndSort(t *testing.T) { |
| points := []weightedPoint{ |
| {0.10, 0.10, 1.0}, |
| {0.12, 0.10, 1.0}, |
| {0.13, 0.12, 2.0}, |
| {0.90, 0.90, 3.0}, |
| {0.91, 0.93, 3.0}, |
| {0.52, 0.90, 5.0}, |
| } |
|
|
| threshold := 0.05 |
|
|
| |
| groups := GroupByDistance[weightedPoint, float64](points, threshold) |
|
|
| |
| assert.Len(t, groups, 3) |
|
|
| |
| SortGroupsBySize(groups) |
| assert.Len(t, groups[0].Items, 3) |
| assert.Len(t, groups[1].Items, 2) |
| assert.Len(t, groups[2].Items, 1) |
|
|
| |
| SortGroupsByWeight(groups) |
| assert.Len(t, groups[0].Items, 2) |
| assert.Len(t, groups[1].Items, 1) |
| assert.Len(t, groups[2].Items, 3) |
| } |
|
|