1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use crate::{Path, PathDirection, PathPoint};

impl PathPoint {
    /// Calculates the curvature of the path at the point.
    ///
    /// Optionally, returns the center of the osculating circle as well.
    /// The curvature is the inverse of the radius of the osculating circle.
    ///
    /// Lines have a curvature of zero (indicating an osculating circle of
    /// infinite radius. In this case, the @center is not modified.
    ///
    /// Circles with a radius of zero have `INFINITY` as curvature
    ///
    /// Note that certain points on a path may not have a single curvature,
    /// such as sharp turns. At such points, there are two curvatures --
    /// the (limit of) the curvature of the path going into the point,
    /// and the (limit of) the curvature of the path coming out of it.
    /// The @direction argument lets you choose which one to get.
    ///
    /// <picture>
    ///   <source srcset="curvature-dark.png" media="(prefers-color-scheme: dark)">
    ///   <img alt="Osculating circle" src="curvature-light.png">
    /// </picture>
    /// ## `path`
    /// the path that @self is on
    /// ## `direction`
    /// the direction for which to return the curvature
    ///
    /// # Returns
    ///
    /// The curvature of the path at the given point
    ///
    /// ## `center`
    /// Return location for
    ///   the center of the osculating circle
    #[doc(alias = "gsk_path_point_get_curvature")]
    #[doc(alias = "get_curvature")]
    pub fn curvature(
        &self,
        path: &Path,
        direction: PathDirection,
    ) -> (f32, Option<graphene::Point>) {
        unsafe {
            let mut center = graphene::Point::uninitialized();
            let ret = ffi::gsk_path_point_get_curvature(
                self.to_glib_none().0,
                path.to_glib_none().0,
                direction.into_glib(),
                center.to_glib_none_mut().0,
            );

            if ret == 0.0 {
                (ret, None)
            } else {
                (ret, Some(center))
            }
        }
    }
}