UNPKG

agentscape

Version:

Agentscape is a library for creating agent-based simulations. It provides a simple API for defining agents and their behavior, and for defining the environment in which the agents interact. Agentscape is designed to be flexible and extensible, allowing

32 lines 1.23 kB
export default class GeoLocation { constructor(lat, lon) { this.lat = lat; this.lon = lon; } toCartesian(boundingBox, xMax, yMax) { const { lat, lon } = this; const { south, north, west, east } = boundingBox; const x = ((lon - west) / (east - west)) * xMax; const y = ((north - lat) / (north - south)) * yMax; return { x, y }; } toString() { return `(${this.lat}, ${this.lon})`; } /** * Gives the distance between two points on the Earth's surface using the Haversine formula. * The returned distance is in meters. */ haversineDistance(other) { const R = 6371000; // Earth's radius in meters const toRadians = (deg) => (deg * Math.PI) / 180; const dLat = toRadians(other.lat - this.lat); const dLon = toRadians(other.lon - this.lon); const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(this.lat)) * Math.cos(toRadians(other.lat)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; // Distance in meters } } //# sourceMappingURL=GeoLocation.js.map