3 个不稳定版本
0.2.1 | 2022年6月9日 |
---|---|
0.2.0 | 2022年6月9日 |
0.1.0 | 2022年6月8日 |
#1276 in 数学
8KB
94 行
Rust 中的 Haversine 公式
这是一个小型库,用于在 Rust 中实现 Haversine 公式。已经有一个 haversine crate(你可以在这里找到它),但该库自 2015 年以来没有更新,因此存在一些问题,特别是缺少文档以及 API 稍显繁琐和效率不高。
因此,这里提供了一个重写版本,具有更直观的接口和可选的 #![no_std]
支持。
用法
将 haversine-redux 添加为项目 Cargo.toml 文件中的依赖项
[dependencies]
haversine-redux = "0.2"
示例用法
use haversine_redux::{Location, Unit};
fn main() {
let start = Location::new(38.898556, -77.037852);
let end = Location::new(38.897147, -77.043934);
// Kilometers
let km = start.distance_to(&end, Unit::Kilometer);
// OR
let km = start.kilometers_to(&end);
// Miles
let miles = start.distance_to(&end, Unit::Mile);
// OR
let miles = start.miles_to(&end);
// To use the haversine formular with a custom sphere which is not the earth use the CustomSphere unit and add the radius
let custom = start.distance_to(&end, Unit::CustomSphere(50.0));
}
#![no_std]
的可选支持
要启用 no_std 支持,只需将 no_std
功能添加到 haversine-redux 依赖项
[dependencies]
haversine-redux = {version = "0.2", features = ["no_std"]}
这将加载依赖库 libm,用于实现 sin、cos 或 atan2 等数学函数。
许可协议
本库采用 MIT 和 Apache 2.0 许可协议的双重许可。
依赖项
~105KB