Skip to content

Commit

Permalink
add 三角函数
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Apr 6, 2022
1 parent 1332988 commit 4281a7f
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/algos/math/trigonometry.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# 三角
# 三角函数

### 三角形边长计算
计算角为 2 弧度、对边长度为 80 的直角三角形的斜边长度。

```rust,editable
fn main() {
let angle: f64 = 2.0;
let side_length = 80.0;
let hypotenuse = side_length / angle.sin();
println!("Hypotenuse: {}", hypotenuse);
}
```

### 验证 tan = sin / cos

```rust,editable
fn main() {
let x: f64 = 6.0;
let a = x.tan();
let b = x.sin() / x.cos();
assert_eq!(a, b);
}
```

### 地球上两点间的距离
下面的代码使用 [Haversine 公式](https://blog.csdn.net/Hardict/article/details/105267473) 计算地球上两点之间的公里数。

```rust,editable
fn main() {
let earth_radius_kilometer = 6371.0_f64;
let (paris_latitude_degrees, paris_longitude_degrees) = (48.85341_f64, -2.34880_f64);
let (london_latitude_degrees, london_longitude_degrees) = (51.50853_f64, -0.12574_f64);
let paris_latitude = paris_latitude_degrees.to_radians();
let london_latitude = london_latitude_degrees.to_radians();
let delta_latitude = (paris_latitude_degrees - london_latitude_degrees).to_radians();
let delta_longitude = (paris_longitude_degrees - london_longitude_degrees).to_radians();
let central_angle_inner = (delta_latitude / 2.0).sin().powi(2)
+ paris_latitude.cos() * london_latitude.cos() * (delta_longitude / 2.0).sin().powi(2);
let central_angle = 2.0 * central_angle_inner.sqrt().asin();
let distance = earth_radius_kilometer * central_angle;
println!(
"Distance between Paris and London on the surface of Earth is {:.1} kilometers",
distance
);
}
```

0 comments on commit 4281a7f

Please sign in to comment.