rect_based_targeting_utils.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/views/rect_based_targeting_utils.h"
  5. #include "ui/gfx/geometry/point.h"
  6. #include "ui/gfx/geometry/rect.h"
  7. namespace views {
  8. bool UsePointBasedTargeting(const gfx::Rect& rect) {
  9. return rect.width() == 1 && rect.height() == 1;
  10. }
  11. float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) {
  12. gfx::Rect intersection(rect_1);
  13. intersection.Intersect(rect_2);
  14. int intersect_area = intersection.size().GetArea();
  15. int rect_1_area = rect_1.size().GetArea();
  16. return rect_1_area ? static_cast<float>(intersect_area) /
  17. static_cast<float>(rect_1_area)
  18. : 0;
  19. }
  20. int DistanceSquaredFromCenterToPoint(const gfx::Point& point,
  21. const gfx::Rect& rect) {
  22. gfx::Point center_point = rect.CenterPoint();
  23. int dx = center_point.x() - point.x();
  24. int dy = center_point.y() - point.y();
  25. return (dx * dx) + (dy * dy);
  26. }
  27. } // namespace views