blob: 6fcc3a8ae57523bd09625e245e21da17ed14e27b [file] [log] [blame]
 Brad 439cafb 2017-11-11 15:31:54 -0500 [diff] [blame] 1 package islandairport; 2 3 import java.lang.Math; 4 import java.awt.geom.Point2D; 5 6 Brad 7c809ea 2017-11-13 15:15:49 -0500 [diff] [blame] 7 public fun angleBetween(a, b) { 8 return angleBetween(a.getX(), a.getY(), b.getX(), b.getY()); 9 } 10 Brad 439cafb 2017-11-11 15:31:54 -0500 [diff] [blame] 11 // calculates angle between two points 12 // x1, y1 -> point a 13 // x2, y2 -> point b 14 public fun angleBetween(x1, y1, x2, y2) { 15 var dx = x2 - x2; 16 var dy = y2 - y1; 17 var angle = Math.atan2(dy, dx); 18 if (angle < 0) 19 return -angle; 20 return 2 * Math.PI - angle; 21 } 22 23 // Calculates the 2d distance between two points 24 // x1, y1 point a 25 // x2, y2 point b 26 public fun distanceBetween(x1, y1, x2, y2) { 27 return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); 28 } 29 30 // tests whether a point is on the given line 31 // px1, py1 -> the point to test 32 // x2, y2, x3, y3 -> the line 33 public fun onLine(px1, py1, x2, y2, x3, y3) { Brad 52c28e0 2017-11-13 14:33:59 -0500 [diff] [blame] 34 var d23 = distanceBetween(x2, y2, x3, y3); Brad 439cafb 2017-11-11 15:31:54 -0500 [diff] [blame] 35 var d = distanceBetween(px1, py1, x3, y3) + distanceBetween(px1, py1, x2, y2); 36 var eps = 0.00000000001; Brad 439cafb 2017-11-11 15:31:54 -0500 [diff] [blame] 37 return (d + eps) > d23 && (d - eps) < d23; 38 }