blob: 27dab179cf74d8a94d18b84edd967e85aca6b0eb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRect.h"
14#include "include/core/SkScalar.h"
15#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/utils/SkRandom.h"
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000017
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000018static void test_hittest(SkCanvas* canvas, const SkPath& path) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000019 SkPaint paint;
20 SkRect r = path.getBounds();
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000022 paint.setColor(SK_ColorRED);
23 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comdbc5d282012-07-03 12:23:22 +000025 const SkScalar MARGIN = SkIntToScalar(4);
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000027 paint.setColor(0x800000FF);
reed@google.comdbc5d282012-07-03 12:23:22 +000028 for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
29 for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000030 if (path.contains(x, y)) {
31 canvas->drawPoint(x, y, paint);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000032 }
33 }
34 }
35}
36
halcanary2a243382015-09-09 08:16:41 -070037DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000038 SkPath path;
scroggof9d61012014-12-15 12:54:51 -080039 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000041 int scale = 300;
42 for (int i = 0; i < 4; ++i) {
caryclark99a69eb2015-06-04 09:27:43 -070043 // get the random values deterministically
44 SkScalar randoms[12];
45 for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) {
46 randoms[index] = rand.nextUScalar1();
47 }
48 path.lineTo(randoms[0] * scale, randoms[1] * scale);
49 path.quadTo(randoms[2] * scale, randoms[3] * scale,
50 randoms[4] * scale, randoms[5] * scale);
51 path.cubicTo(randoms[6] * scale, randoms[7] * scale,
52 randoms[8] * scale, randoms[9] * scale,
53 randoms[10] * scale, randoms[11] * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000054 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000056 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000057 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000059 test_hittest(canvas, path);
60
61 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000062 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000063
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000064 test_hittest(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070065}