blob: a0555ab51e99aa8f814af7cedf35f52247dc80d0 [file] [log] [blame]
Brian Salomonab664fa2017-03-24 16:07:20 +00001/*
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
Jim Van Verth4db18ed2018-04-03 10:00:37 -04008#ifndef SkOffsetPolygon_DEFINED
9#define SkOffsetPolygon_DEFINED
Brian Salomonab664fa2017-03-24 16:07:20 +000010
Jim Van Verthda965502017-04-11 15:29:14 -040011#include <functional>
12
Brian Salomonab664fa2017-03-24 16:07:20 +000013#include "SkTDArray.h"
14#include "SkPoint.h"
15
Jim Van Verthda965502017-04-11 15:29:14 -040016/**
Brian Salomonab664fa2017-03-24 16:07:20 +000017 * 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.
Jim Van Verthda965502017-04-11 15:29:14 -040022 * @param insetDistanceFunc How far we wish to inset the polygon for a given index in the array.
23 * This should return a positive value.
Brian Salomonab664fa2017-03-24 16:07:20 +000024 * @param insetPolygon The resulting inset polygon, if any.
25 * @return true if an inset polygon exists, false otherwise.
26 */
27bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
Jim Van Verthda965502017-04-11 15:29:14 -040028 std::function<SkScalar(int index)> insetDistanceFunc,
29 SkTDArray<SkPoint>* insetPolygon);
30
31inline bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
Jim Van Verth4db18ed2018-04-03 10:00:37 -040032 SkScalar inset, SkTDArray<SkPoint>* insetPolygon) {
Jim Van Verthda965502017-04-11 15:29:14 -040033 return SkInsetConvexPolygon(inputPolygonVerts, inputPolygonSize,
34 [inset](int) { return inset; },
35 insetPolygon);
36}
37
38/**
Jim Van Verth4db18ed2018-04-03 10:00:37 -040039 * Generates a simple polygon (if possible) that is offset a given distance from the boundary of a
40 * given simple polygon.
41 *
42 * @param inputPolygonVerts Array of points representing the vertices of the original polygon.
43 * @param inputPolygonSize Number of vertices in the original polygon.
44 * @param offset How far we wish to offset the polygon.
45 * Positive value means inset, negative value means outset.
46 * @param offsetPolgon The resulting offset polygon, if any.
47 * @return true if an offset simple polygon exists, false otherwise.
48 */
49bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
50 SkScalar offset, SkTDArray<SkPoint>* offsetPolygon);
51
52/**
Jim Van Verthda965502017-04-11 15:29:14 -040053 * Offset a segment by the given distance at each point.
54 * Uses the outer tangents of two circles centered on each endpoint.
55 * See: https://en.wikipedia.org/wiki/Tangent_lines_to_circles
56 *
57 * @param p0 First endpoint.
58 * @param p1 Second endpoint.
59 * @param d0 Offset distance from first endpoint.
60 * @param d1 Offset distance from second endpoint.
61 * @param side Indicates whether we want to offset to the left (1) or right (-1) side of segment.
62 * @param offset0 First endpoint of offset segment.
63 * @param offset1 Second endpoint of offset segment.
64 * @return true if an offset segment exists, false otherwise.
65 */
66bool SkOffsetSegment(const SkPoint& p0, const SkPoint& p1, SkScalar d0, SkScalar d1,
67 int side, SkPoint* offset0, SkPoint* offset1);
Brian Salomonab664fa2017-03-24 16:07:20 +000068
69#endif