[gtk/curve-ops] Add gsk_curve_get_{start,end}_angle



commit f82d3c0d7e57d73cc17af4d28f98c2c848e19425
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Dec 6 23:13:56 2020 -0500

    Add gsk_curve_get_{start,end}_angle
    
    Add a way to get the tangent angles at the start
    and end of the curve. This will be used in stroking.

 gsk/gskcurve.c        | 79 +++++++++++++++++++++++++++++++++++++++++++++++++--
 gsk/gskcurveprivate.h |  2 ++
 2 files changed, 78 insertions(+), 3 deletions(-)
---
diff --git a/gsk/gskcurve.c b/gsk/gskcurve.c
index 3014420dda..b003d1446a 100644
--- a/gsk/gskcurve.c
+++ b/gsk/gskcurve.c
@@ -46,6 +46,8 @@ struct _GskCurveClass
   const graphene_point_t *      (* get_end_point)       (const GskCurve         *curve);
   void                          (* get_bounds)          (const GskCurve         *curve,
                                                          graphene_rect_t        *bounds);
+  float                         (* get_start_angle)     (const GskCurve         *curve);
+  float                         (* get_end_angle)       (const GskCurve         *curve);
 };
 
 /** LINE **/
@@ -151,6 +153,21 @@ gsk_line_curve_get_bounds (const GskCurve  *curve,
   graphene_rect_expand (bounds, &self->points[1], bounds);
 }
 
+static inline float
+get_angle (const graphene_point_t *a,
+           const graphene_point_t *b)
+{
+  return atan2 (b->y - a->y, b->x - a->x);
+}
+
+static float
+gsk_line_curve_get_angle (const GskCurve *curve)
+{
+  const GskLineCurve *self = &curve->line;
+
+  get_angle (&self->points[0], &self->points[1]);
+}
+
 static const GskCurveClass GSK_LINE_CURVE_CLASS = {
   gsk_line_curve_init,
   gsk_line_curve_eval,
@@ -159,7 +176,9 @@ static const GskCurveClass GSK_LINE_CURVE_CLASS = {
   gsk_line_curve_pathop,
   gsk_line_curve_get_start_point,
   gsk_line_curve_get_end_point,
-  gsk_line_curve_get_bounds
+  gsk_line_curve_get_bounds,
+  gsk_line_curve_get_angle,
+  gsk_line_curve_get_angle
 };
 
 /** CURVE **/
@@ -398,6 +417,22 @@ gsk_curve_curve_get_bounds (const GskCurve  *curve,
     }
 }
 
+static float
+gsk_curve_curve_get_start_angle (const GskCurve *curve)
+{
+  const GskCurveCurve *self = &curve->curve;
+
+  return get_angle (self->points[0], self->points[1]);
+}
+
+static float
+gsk_curve_curve_get_end_angle (const GskCurve *curve)
+{
+  const GskCurveCurve *self = &curve->curve;
+
+  return get_angle (self->points[2], self->points[3]);
+}
+
 static const GskCurveClass GSK_CURVE_CURVE_CLASS = {
   gsk_curve_curve_init,
   gsk_curve_curve_eval,
@@ -406,7 +441,9 @@ static const GskCurveClass GSK_CURVE_CURVE_CLASS = {
   gsk_curve_curve_pathop,
   gsk_curve_curve_get_start_point,
   gsk_curve_curve_get_end_point,
-  gsk_curve_curve_get_bounds
+  gsk_curve_curve_get_bounds,
+  gsk_curve_curve_get_start_angle,
+  gsk_curve_curve_get_end_angle
 };
 
 /** CONIC **/
@@ -686,6 +723,22 @@ gsk_conic_curve_get_bounds (const GskCurve  *curve,
     }
 }
 
+static float
+gsk_conic_curve_get_start_angle (const GskCurve *curve)
+{
+  const GskCurveCurve *self = &curve->curve;
+
+  return get_angle (self->points[0], self->points[1]);
+}
+
+static float
+gsk_conic_curve_get_end_angle (const GskCurve *curve)
+{
+  const GskCurveCurve *self = &curve->curve;
+
+  return get_angle (self->points[1], self->points[3]);
+}
+
 static const GskCurveClass GSK_CONIC_CURVE_CLASS = {
   gsk_conic_curve_init,
   gsk_conic_curve_eval,
@@ -694,7 +747,9 @@ static const GskCurveClass GSK_CONIC_CURVE_CLASS = {
   gsk_conic_curve_pathop,
   gsk_conic_curve_get_start_point,
   gsk_conic_curve_get_end_point,
-  gsk_conic_curve_get_bounds
+  gsk_conic_curve_get_bounds,
+  gsk_conic_curve_get_start_angle,
+  gsk_conic_curve_get_end_angle
 };
 
 /** API **/
@@ -773,6 +828,24 @@ gsk_curve_get_bounds (const GskCurve  *curve,
   get_class (curve->op)->get_bounds (curve, bounds);
 }
 
+/* Returns the angle between the tangent at the start
+ * point and the x axis, in the range [-pi, pi]
+ */
+float
+gsk_curve_get_start_angle (const GskCurve *curve)
+{
+  return get_class (curve->op)->get_start_angle (curve);
+}
+
+/* Returns the angle between the tangent at the end
+ * point and the x axis, in the range [-pi, pi]
+ */
+float
+gsk_curve_get_end_angle (const GskCurve *curve)
+{
+  return get_class (curve->op)->get_end_angle (curve);
+}
+
 /** Intersections **/
 
 static int
diff --git a/gsk/gskcurveprivate.h b/gsk/gskcurveprivate.h
index dfdd45b034..5f1a951c11 100644
--- a/gsk/gskcurveprivate.h
+++ b/gsk/gskcurveprivate.h
@@ -108,6 +108,8 @@ int                     gsk_curve_intersect                     (const GskCurve
                                                                  float                  *t2,
                                                                  graphene_point_t       *p,
                                                                  int                     n);
+float                   gsk_curve_get_start_angle               (const GskCurve         *curve);
+float                   gsk_curve_get_end_angle                 (const GskCurve         *curve);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]