mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkScalar.h" |
| 15 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/utils/SkRandom.h" |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 17 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 18 | static void test_hittest(SkCanvas* canvas, const SkPath& path) { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 19 | SkPaint paint; |
| 20 | SkRect r = path.getBounds(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 22 | paint.setColor(SK_ColorRED); |
| 23 | canvas->drawPath(path, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 25 | const SkScalar MARGIN = SkIntToScalar(4); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 27 | paint.setColor(0x800000FF); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 28 | 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.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 30 | if (path.contains(x, y)) { |
| 31 | canvas->drawPoint(x, y, paint); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 37 | DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 38 | SkPath path; |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 39 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 40 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 41 | int scale = 300; |
| 42 | for (int i = 0; i < 4; ++i) { |
caryclark | 99a69eb | 2015-06-04 09:27:43 -0700 | [diff] [blame] | 43 | // 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.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 54 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 56 | path.setFillType(SkPath::kEvenOdd_FillType); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 57 | path.offset(SkIntToScalar(20), SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 59 | test_hittest(canvas, path); |
| 60 | |
| 61 | canvas->translate(SkIntToScalar(scale), 0); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 62 | path.setFillType(SkPath::kWinding_FillType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 64 | test_hittest(canvas, path); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 65 | } |