Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright 2017 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkInsetConvexPolygon_DEFINED
      9 #define SkInsetConvexPolygon_DEFINED
     10 
     11 #include <functional>
     12 
     13 #include "SkTDArray.h"
     14 #include "SkPoint.h"
     15 
     16 /**
     17  * Generates a polygon that is inset a given distance from the boundary of a given convex polygon.
     18  *
     19  * @param inputPolygonVerts  Array of points representing the vertices of the original polygon.
     20  *  It should be convex and have no coincident points.
     21  * @param inputPolygonSize  Number of vertices in the original polygon.
     22  * @param insetDistanceFunc  How far we wish to inset the polygon for a given index in the array.
     23  *  This should return a positive value.
     24  * @param insetPolygon  The resulting inset polygon, if any.
     25  * @return true if an inset polygon exists, false otherwise.
     26  */
     27 bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
     28                           std::function<SkScalar(int index)> insetDistanceFunc,
     29                           SkTDArray<SkPoint>* insetPolygon);
     30 
     31 inline bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
     32                                  SkScalar inset,
     33                                  SkTDArray<SkPoint>* insetPolygon) {
     34     return SkInsetConvexPolygon(inputPolygonVerts, inputPolygonSize,
     35                                 [inset](int) { return inset; },
     36                                 insetPolygon);
     37 }
     38 
     39 /**
     40  * Offset a segment by the given distance at each point.
     41  * Uses the outer tangents of two circles centered on each endpoint.
     42  * See: https://en.wikipedia.org/wiki/Tangent_lines_to_circles
     43  *
     44  * @param p0  First endpoint.
     45  * @param p1  Second endpoint.
     46  * @param d0  Offset distance from first endpoint.
     47  * @param d1  Offset distance from second endpoint.
     48  * @param side  Indicates whether we want to offset to the left (1) or right (-1) side of segment.
     49  * @param offset0  First endpoint of offset segment.
     50  * @param offset1  Second endpoint of offset segment.
     51  * @return true if an offset segment exists, false otherwise.
     52  */
     53 bool SkOffsetSegment(const SkPoint& p0, const SkPoint& p1, SkScalar d0, SkScalar d1,
     54                      int side, SkPoint* offset0, SkPoint* offset1);
     55 
     56 #endif
     57