blob: b6c3a222ad48698e4042bc4b226a193e6ac3cf52 [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.
Jim Van Verth872da6b2018-04-10 11:24:11 -040047 * @param polygonIndices The indices of the original polygon that map to the new one.
Jim Van Verth4db18ed2018-04-03 10:00:37 -040048 * @return true if an offset simple polygon exists, false otherwise.
49 */
50bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize,
Jim Van Verth872da6b2018-04-10 11:24:11 -040051 SkScalar offset, SkTDArray<SkPoint>* offsetPolygon,
52 SkTDArray<int>* polygonIndices = nullptr);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040053
54/**
Jim Van Verthda965502017-04-11 15:29:14 -040055 * Offset a segment by the given distance at each point.
56 * Uses the outer tangents of two circles centered on each endpoint.
57 * See: https://en.wikipedia.org/wiki/Tangent_lines_to_circles
58 *
59 * @param p0 First endpoint.
60 * @param p1 Second endpoint.
61 * @param d0 Offset distance from first endpoint.
62 * @param d1 Offset distance from second endpoint.
63 * @param side Indicates whether we want to offset to the left (1) or right (-1) side of segment.
64 * @param offset0 First endpoint of offset segment.
65 * @param offset1 Second endpoint of offset segment.
66 * @return true if an offset segment exists, false otherwise.
67 */
68bool SkOffsetSegment(const SkPoint& p0, const SkPoint& p1, SkScalar d0, SkScalar d1,
69 int side, SkPoint* offset0, SkPoint* offset1);
Brian Salomonab664fa2017-03-24 16:07:20 +000070
71#endif