bearing-angle-calculator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Bearing Angle 🧭 Calculator
2
+
3
+ A lightweight JavaScript utility library for calculating bearing angles between geographic coordinates.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install bearing-calculator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ import { calculateBearing, getCompassDirection, calculateDistance } from 'bearing-calculator';
15
+
16
+ // Calculate bearing from New York to London
17
+ const bearing = calculateBearing(40.7128, -74.0060, 51.5074, -0.1278);
18
+ console.log(bearing); // ~51.2°
19
+
20
+ // Get compass direction
21
+ const direction = getCompassDirection(bearing);
22
+ console.log(direction); // "NE"
23
+
24
+ // Calculate distance
25
+ const distance = calculateDistance(40.7128, -74.0060, 51.5074, -0.1278, 'km');
26
+ console.log(distance); // ~5570 km
27
+ ```
28
+
29
+ ### CommonJS
30
+
31
+ ```javascript
32
+ const { calculateBearing, getCompassDirection } = require('bearing-calculator');
33
+ ```
34
+
35
+ ## API Reference
36
+
37
+ ### `calculateBearing(startLat, startLng, endLat, endLng)`
38
+
39
+ Returns bearing angle (0-360°) from start to end point.
40
+
41
+ ### `getCompassDirection(bearing)`
42
+
43
+ Returns 8-point compass direction (N, NE, E, SE, S, SW, W, NW).
44
+
45
+ ### `getDetailedCompassDirection(bearing)`
46
+
47
+ Returns 16-point compass direction (N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW).
48
+
49
+ ### `calculateDistance(startLat, startLng, endLat, endLng, unit)`
50
+
51
+ Returns distance between points.
52
+
53
+ | Parameter | Type | Description |
54
+ |-----------|------|-------------|
55
+ | `startLat` | number | Starting latitude (-90 to 90) |
56
+ | `startLng` | number | Starting longitude (-180 to 180) |
57
+ | `endLat` | number | Ending latitude (-90 to 90) |
58
+ | `endLng` | number | Ending longitude (-180 to 180) |
59
+ | `unit` | string | `'km'` (default), `'miles'`, or `'meters'` |
60
+
61
+ ### `toRadians(degrees)`
62
+
63
+ Converts degrees to radians.
64
+
65
+ ### `toDegrees(radians)`
66
+
67
+ Converts radians to degrees.
68
+
69
+ ## License
70
+
71
+ MIT
package/bearing.js ADDED
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Bearing Calculator Utilities
3
+ * Calculate bearing angle between two geographic coordinates
4
+ */
5
+
6
+ /**
7
+ * Convert degrees to radians
8
+ * @param {number} degrees - Angle in degrees
9
+ * @returns {number} Angle in radians
10
+ */
11
+ function toRadians(degrees) {
12
+ return degrees * (Math.PI / 180);
13
+ }
14
+
15
+ /**
16
+ * Convert radians to degrees
17
+ * @param {number} radians - Angle in radians
18
+ * @returns {number} Angle in degrees
19
+ */
20
+ function toDegrees(radians) {
21
+ return radians * (180 / Math.PI);
22
+ }
23
+
24
+ /**
25
+ * Calculate the bearing angle between two geographic points
26
+ * The bearing is the compass direction from the start point to the end point
27
+ *
28
+ * @param {number} startLat - Starting point latitude (-90 to 90)
29
+ * @param {number} startLng - Starting point longitude (-180 to 180)
30
+ * @param {number} endLat - Ending point latitude (-90 to 90)
31
+ * @param {number} endLng - Ending point longitude (-180 to 180)
32
+ * @returns {number} Bearing angle in degrees (0-360), where 0/360 is North
33
+ */
34
+ function calculateBearing(startLat, startLng, endLat, endLng) {
35
+ // Validate inputs
36
+ if (typeof startLat !== 'number' || typeof startLng !== 'number' ||
37
+ typeof endLat !== 'number' || typeof endLng !== 'number') {
38
+ throw new Error('All coordinates must be numbers');
39
+ }
40
+
41
+ if (startLat < -90 || startLat > 90 || endLat < -90 || endLat > 90) {
42
+ throw new Error('Latitude must be between -90 and 90 degrees');
43
+ }
44
+
45
+ if (startLng < -180 || startLng > 180 || endLng < -180 || endLng > 180) {
46
+ throw new Error('Longitude must be between -180 and 180 degrees');
47
+ }
48
+
49
+ // Convert to radians
50
+ const startLatRad = toRadians(startLat);
51
+ const startLngRad = toRadians(startLng);
52
+ const endLatRad = toRadians(endLat);
53
+ const endLngRad = toRadians(endLng);
54
+
55
+ // Calculate the difference in longitude
56
+ const dLng = endLngRad - startLngRad;
57
+
58
+ // Calculate bearing using the forward azimuth formula
59
+ const x = Math.sin(dLng) * Math.cos(endLatRad);
60
+ const y = Math.cos(startLatRad) * Math.sin(endLatRad) -
61
+ Math.sin(startLatRad) * Math.cos(endLatRad) * Math.cos(dLng);
62
+
63
+ // Calculate bearing in radians and convert to degrees
64
+ let bearing = toDegrees(Math.atan2(x, y));
65
+
66
+ // Normalize to 0-360 range
67
+ bearing = (bearing + 360) % 360;
68
+
69
+ return bearing;
70
+ }
71
+
72
+ /**
73
+ * Get the compass cardinal/intercardinal direction from a bearing angle
74
+ *
75
+ * @param {number} bearing - Bearing angle in degrees (0-360)
76
+ * @returns {string} Compass direction (N, NE, E, SE, S, SW, W, NW)
77
+ */
78
+ function getCompassDirection(bearing) {
79
+ if (typeof bearing !== 'number' || bearing < 0 || bearing > 360) {
80
+ throw new Error('Bearing must be a number between 0 and 360');
81
+ }
82
+
83
+ const directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
84
+ const index = Math.round(bearing / 45) % 8;
85
+ return directions[index];
86
+ }
87
+
88
+ /**
89
+ * Get detailed compass direction (16-point)
90
+ *
91
+ * @param {number} bearing - Bearing angle in degrees (0-360)
92
+ * @returns {string} Detailed compass direction (N, NNE, NE, ENE, E, etc.)
93
+ */
94
+ function getDetailedCompassDirection(bearing) {
95
+ if (typeof bearing !== 'number' || bearing < 0 || bearing > 360) {
96
+ throw new Error('Bearing must be a number between 0 and 360');
97
+ }
98
+
99
+ const directions = [
100
+ 'N', 'NNE', 'NE', 'ENE',
101
+ 'E', 'ESE', 'SE', 'SSE',
102
+ 'S', 'SSW', 'SW', 'WSW',
103
+ 'W', 'WNW', 'NW', 'NNW'
104
+ ];
105
+ const index = Math.round(bearing / 22.5) % 16;
106
+ return directions[index];
107
+ }
108
+
109
+ /**
110
+ * Calculate distance between two points using Haversine formula
111
+ *
112
+ * @param {number} startLat - Starting point latitude
113
+ * @param {number} startLng - Starting point longitude
114
+ * @param {number} endLat - Ending point latitude
115
+ * @param {number} endLng - Ending point longitude
116
+ * @param {string} unit - Unit of measurement ('km', 'miles', 'meters')
117
+ * @returns {number} Distance in specified unit
118
+ */
119
+ function calculateDistance(startLat, startLng, endLat, endLng, unit = 'km') {
120
+ const R = {
121
+ km: 6371,
122
+ miles: 3959,
123
+ meters: 6371000
124
+ };
125
+
126
+ const radius = R[unit] || R.km;
127
+
128
+ const dLat = toRadians(endLat - startLat);
129
+ const dLng = toRadians(endLng - startLng);
130
+
131
+ const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
132
+ Math.cos(toRadians(startLat)) * Math.cos(toRadians(endLat)) *
133
+ Math.sin(dLng / 2) * Math.sin(dLng / 2);
134
+
135
+ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
136
+
137
+ return radius * c;
138
+ }
139
+
140
+ module.exports = {
141
+ calculateBearing,
142
+ getCompassDirection,
143
+ getDetailedCompassDirection,
144
+ calculateDistance,
145
+ toRadians,
146
+ toDegrees
147
+ };
package/index.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Type definitions for bearing-calculator
2
+
3
+ /**
4
+ * Convert degrees to radians
5
+ */
6
+ export function toRadians(degrees: number): number;
7
+
8
+ /**
9
+ * Convert radians to degrees
10
+ */
11
+ export function toDegrees(radians: number): number;
12
+
13
+ /**
14
+ * Calculate bearing angle between two geographic points
15
+ * @param startLat Starting latitude (-90 to 90)
16
+ * @param startLng Starting longitude (-180 to 180)
17
+ * @param endLat Ending latitude (-90 to 90)
18
+ * @param endLng Ending longitude (-180 to 180)
19
+ * @returns Bearing angle in degrees (0-360)
20
+ */
21
+ export function calculateBearing(
22
+ startLat: number,
23
+ startLng: number,
24
+ endLat: number,
25
+ endLng: number
26
+ ): number;
27
+
28
+ /**
29
+ * Get 8-point compass direction from bearing
30
+ * @param bearing Bearing angle (0-360)
31
+ * @returns Cardinal direction (N, NE, E, SE, S, SW, W, NW)
32
+ */
33
+ export function getCompassDirection(bearing: number): string;
34
+
35
+ /**
36
+ * Get 16-point compass direction from bearing
37
+ * @param bearing Bearing angle (0-360)
38
+ * @returns Detailed compass direction (N, NNE, NE, ENE, etc.)
39
+ */
40
+ export function getDetailedCompassDirection(bearing: number): string;
41
+
42
+ /**
43
+ * Calculate distance between two geographic points
44
+ * @param startLat Starting latitude
45
+ * @param startLng Starting longitude
46
+ * @param endLat Ending latitude
47
+ * @param endLng Ending longitude
48
+ * @param unit Unit of measurement ('km', 'miles', 'meters')
49
+ * @returns Distance in specified unit
50
+ */
51
+ export function calculateDistance(
52
+ startLat: number,
53
+ startLng: number,
54
+ endLat: number,
55
+ endLng: number,
56
+ unit?: 'km' | 'miles' | 'meters'
57
+ ): number;
package/index.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Bearing Calculator
3
+ * Calculate bearing angles between geographic coordinates
4
+ *
5
+ * @module bearing-calculator
6
+ */
7
+
8
+ // Core utilities
9
+ const {
10
+ calculateBearing,
11
+ getCompassDirection,
12
+ getDetailedCompassDirection,
13
+ calculateDistance,
14
+ toRadians,
15
+ toDegrees
16
+ } = require('./bearing');
17
+
18
+ module.exports = {
19
+ // Core functions
20
+ calculateBearing,
21
+ getCompassDirection,
22
+ getDetailedCompassDirection,
23
+ calculateDistance,
24
+ toRadians,
25
+ toDegrees,
26
+
27
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "bearing-angle-calculator",
3
+ "version": "1.0.0",
4
+ "description": "Calculate bearing angle between two geographic coordinates for React applications",
5
+ "keywords": [
6
+ "react",
7
+ "bearing",
8
+ "latitude",
9
+ "longitude",
10
+ "bearing-angle",
11
+ "compass",
12
+ "navigation",
13
+ "geolocation"
14
+ ],
15
+ "license": "MIT",
16
+ "author": "rajath002",
17
+ "main": "index.js",
18
+ "module": "index.js",
19
+ "types": "index.d.ts",
20
+ "files": [
21
+ "index.js",
22
+ "index.d.ts",
23
+ "bearing.js",
24
+ "useBearing.js",
25
+ "BearingIndicator.jsx",
26
+ "README.md"
27
+ ],
28
+ "peerDependencies": {
29
+ "react": ">=16.8.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/rajath002/bearing-calculator"
34
+ },
35
+ "scripts": {
36
+ "test": "node test.js"
37
+ }
38
+ }