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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkTypes.h"
16 #include "include/utils/SkRandom.h"
17 
test_hittest(SkCanvas * canvas,const SkPath & path)18 static void test_hittest(SkCanvas* canvas, const SkPath& path) {
19     SkPaint paint;
20     SkRect r = path.getBounds();
21 
22     paint.setColor(SK_ColorRED);
23     canvas->drawPath(path, paint);
24 
25     const SkScalar MARGIN = SkIntToScalar(4);
26 
27     paint.setColor(0x800000FF);
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) {
30             if (path.contains(x, y)) {
31                 canvas->drawPoint(x, y, paint);
32             }
33         }
34     }
35 }
36 
37 DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) {
38         SkPath path;
39         SkRandom rand;
40 
41         int scale = 300;
42         for (int i = 0; i < 4; ++i) {
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);
54         }
55 
56         path.setFillType(SkPathFillType::kEvenOdd);
57         path.offset(SkIntToScalar(20), SkIntToScalar(20));
58 
59         test_hittest(canvas, path);
60 
61         canvas->translate(SkIntToScalar(scale), 0);
62         path.setFillType(SkPathFillType::kWinding);
63 
64         test_hittest(canvas, path);
65 }
66