blob: 5d0d1fc4614cc472ee930d7f38d790f6e7aabb03 [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"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000011#include "SkRandom.h"
12
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000013static void test_hittest(SkCanvas* canvas, const SkPath& path) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000014 SkPaint paint;
15 SkRect r = path.getBounds();
rmistry@google.comd6176b02012-08-23 18:14:13 +000016
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000017 paint.setColor(SK_ColorRED);
18 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.comdbc5d282012-07-03 12:23:22 +000020 const SkScalar MARGIN = SkIntToScalar(4);
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000022 paint.setColor(0x800000FF);
reed@google.comdbc5d282012-07-03 12:23:22 +000023 for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
24 for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000025 if (path.contains(x, y)) {
26 canvas->drawPoint(x, y, paint);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000027 }
28 }
29 }
30}
31
halcanary2a243382015-09-09 08:16:41 -070032DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000033 SkPath path;
scroggof9d61012014-12-15 12:54:51 -080034 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000035
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000036 int scale = 300;
37 for (int i = 0; i < 4; ++i) {
caryclark99a69eb2015-06-04 09:27:43 -070038 // get the random values deterministically
39 SkScalar randoms[12];
40 for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) {
41 randoms[index] = rand.nextUScalar1();
42 }
43 path.lineTo(randoms[0] * scale, randoms[1] * scale);
44 path.quadTo(randoms[2] * scale, randoms[3] * scale,
45 randoms[4] * scale, randoms[5] * scale);
46 path.cubicTo(randoms[6] * scale, randoms[7] * scale,
47 randoms[8] * scale, randoms[9] * scale,
48 randoms[10] * scale, randoms[11] * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000049 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000051 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000052 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000054 test_hittest(canvas, path);
55
56 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000057 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000059 test_hittest(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070060}