blob: d0fe1aabb377e892a8be1564eff42a020514e41a [file] [log] [blame]
mike@reedtribe.org43c62b12012-07-03 02:44:02 +00001/*
2 * Copyright 2011 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#include "gm.h"
9#include "SkCanvas.h"
10#include "SkCullPoints.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000012#include "SkRandom.h"
13
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000014static void test_hittest(SkCanvas* canvas, const SkPath& path) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000015 SkPaint paint;
16 SkRect r = path.getBounds();
rmistry@google.comd6176b02012-08-23 18:14:13 +000017
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000018 paint.setColor(SK_ColorRED);
19 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000020
reed@google.comdbc5d282012-07-03 12:23:22 +000021 const SkScalar MARGIN = SkIntToScalar(4);
rmistry@google.comd6176b02012-08-23 18:14:13 +000022
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000023 paint.setColor(0x800000FF);
reed@google.comdbc5d282012-07-03 12:23:22 +000024 for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
25 for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000026 if (path.contains(x, y)) {
27 canvas->drawPoint(x, y, paint);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000028 }
29 }
30 }
31}
32
halcanary2a243382015-09-09 08:16:41 -070033DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000034 SkPath path;
scroggof9d61012014-12-15 12:54:51 -080035 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000037 int scale = 300;
38 for (int i = 0; i < 4; ++i) {
caryclark99a69eb2015-06-04 09:27:43 -070039 // get the random values deterministically
40 SkScalar randoms[12];
41 for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) {
42 randoms[index] = rand.nextUScalar1();
43 }
44 path.lineTo(randoms[0] * scale, randoms[1] * scale);
45 path.quadTo(randoms[2] * scale, randoms[3] * scale,
46 randoms[4] * scale, randoms[5] * scale);
47 path.cubicTo(randoms[6] * scale, randoms[7] * scale,
48 randoms[8] * scale, randoms[9] * scale,
49 randoms[10] * scale, randoms[11] * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000050 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000052 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000053 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000055 test_hittest(canvas, path);
56
57 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000058 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000060 test_hittest(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070061}