function assertEqual Hackish function for stupid comparison number str

  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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
function assertEqual(a, b) {
/* Hackish function for stupid comparison number, strings and arrays */
if (a.toString() != b.toString()) {
alert(a + ' != ' + b);
}
}
function find_distance(a, b) {
/* Find distance between a and b не учитывая замкнутость координат на сфере */
if ((a * b) > 0) {
return Math.abs(a - b)
} else {
return Math.abs(a) + Math.abs(b)
}
}
function find_clockwise_distance(a, b) {
/* Find shortest distance between a and b учитывая замкнутость координат на сфере */
if (a < b) {
return b - a
} else {
return find_distance(a, 180) + find_distance(-180, b);
}
}
/*
function find_real_distance(distance) {
return Math.abs(Math.min(distance, 360 - distance))
}
function test_find_real_distance() {
assertEqual(find_real_distance(10), 10)
assertEqual(find_real_distance(180), 180)
assertEqual(find_real_distance(190), 170)
}
*/
function find_min_distance(points) {
/*
Find the start and end points between all other points are placed and
distance is minimal
Return start, end and distance
*/
distance = 360
start = null
end = null
points.sort(function(a, b) { return a - b; })
for(x = 0; x < points.length; x++) {
if (x == 0) {
prev = points.length - 1
} else {
prev = x - 1
}
test_distance = find_clockwise_distance(points[x], points[prev])
if (test_distance < distance) {
distance = test_distance
start = points[x]
end = points[prev]
}
}
return [start, end, distance]
}
function test_min_distance() {
assertEqual(find_min_distance([10, 20]), [10, 20, 10])
assertEqual(find_min_distance([10, -10]), [-10, 10, 20])
assertEqual(find_min_distance([170, -170]), [170, -170, 20])
assertEqual(find_min_distance([-170, 170]), [170, -170, 20])
assertEqual(find_min_distance([-170, 160, 170]), [160, -170, 30])
assertEqual(find_min_distance([-10, 10]), [-10, 10, 20])
}
function test_find_clockwise_distance() {
assertEqual(find_clockwise_distance(10, 20), 10)
assertEqual(find_clockwise_distance(20, 10), 350)
assertEqual(find_clockwise_distance(10, -20), 330)
assertEqual(find_clockwise_distance(-20, 10), 30)
assertEqual(find_clockwise_distance(-20, -30), 350)
assertEqual(find_clockwise_distance(-40, -30), 10)
}
function test_find_distance() {
assertEqual(find_distance(10, 20), 10)
assertEqual(find_distance(-10, 20), 30)
assertEqual(find_distance(20, -10), 30)
assertEqual(find_distance(-10, -50), 40)
assertEqual(find_distance(0, 5), 5)
assertEqual(find_distance(0, -5), 5)
}
function run_tests() {
test_find_distance();
test_find_clockwise_distance();
test_min_distance();
}