hittestpath.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkRect.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkTypes.h"
  15. #include "include/utils/SkRandom.h"
  16. static void test_hittest(SkCanvas* canvas, const SkPath& path) {
  17. SkPaint paint;
  18. SkRect r = path.getBounds();
  19. paint.setColor(SK_ColorRED);
  20. canvas->drawPath(path, paint);
  21. const SkScalar MARGIN = SkIntToScalar(4);
  22. paint.setColor(0x800000FF);
  23. 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) {
  25. if (path.contains(x, y)) {
  26. canvas->drawPoint(x, y, paint);
  27. }
  28. }
  29. }
  30. }
  31. DEF_SIMPLE_GM(hittestpath, canvas, 700, 460) {
  32. SkPath path;
  33. SkRandom rand;
  34. int scale = 300;
  35. for (int i = 0; i < 4; ++i) {
  36. // get the random values deterministically
  37. SkScalar randoms[12];
  38. for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) {
  39. randoms[index] = rand.nextUScalar1();
  40. }
  41. path.lineTo(randoms[0] * scale, randoms[1] * scale);
  42. path.quadTo(randoms[2] * scale, randoms[3] * scale,
  43. randoms[4] * scale, randoms[5] * scale);
  44. path.cubicTo(randoms[6] * scale, randoms[7] * scale,
  45. randoms[8] * scale, randoms[9] * scale,
  46. randoms[10] * scale, randoms[11] * scale);
  47. }
  48. path.setFillType(SkPath::kEvenOdd_FillType);
  49. path.offset(SkIntToScalar(20), SkIntToScalar(20));
  50. test_hittest(canvas, path);
  51. canvas->translate(SkIntToScalar(scale), 0);
  52. path.setFillType(SkPath::kWinding_FillType);
  53. test_hittest(canvas, path);
  54. }