Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 8 | #ifndef SkOffsetPolygon_DEFINED |
| 9 | #define SkOffsetPolygon_DEFINED |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 10 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 11 | #include <functional> |
| 12 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
| 14 | #include "SkPoint.h" |
| 15 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 16 | /** |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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. |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 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. |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 28 | std::function<SkScalar(int index)> insetDistanceFunc, |
| 29 | SkTDArray<SkPoint>* insetPolygon); |
| 30 | |
| 31 | inline bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 32 | SkScalar inset, SkTDArray<SkPoint>* insetPolygon) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 33 | return SkInsetConvexPolygon(inputPolygonVerts, inputPolygonSize, |
| 34 | [inset](int) { return inset; }, |
| 35 | insetPolygon); |
| 36 | } |
| 37 | |
| 38 | /** |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 39 | * 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 | */ |
| 49 | bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, |
| 50 | SkScalar offset, SkTDArray<SkPoint>* offsetPolygon); |
| 51 | |
| 52 | /** |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 53 | * 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 | */ |
| 66 | bool SkOffsetSegment(const SkPoint& p0, const SkPoint& p1, SkScalar d0, SkScalar d1, |
| 67 | int side, SkPoint* offset0, SkPoint* offset1); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 68 | |
| 69 | #endif |