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.
5 #include "ui/views/rect_based_targeting_utils.h"
7 #include "ui/gfx/point.h"
8 #include "ui/gfx/rect.h"
14 // The positive distance from |pos| to the nearest endpoint of the interval
15 // [start, end] is returned if |pos| lies within the interval, otherwise
17 int DistanceToInterval(int pos, int start, int end) {
27 bool UsePointBasedTargeting(const gfx::Rect& rect) {
28 return rect.width() == 1 && rect.height() == 1;
31 float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) {
32 gfx::Rect intersection(rect_1);
33 intersection.Intersect(rect_2);
34 int intersect_area = intersection.size().GetArea();
35 int rect_1_area = rect_1.size().GetArea();
37 static_cast<float>(intersect_area) / static_cast<float>(rect_1_area) : 0;
40 int DistanceSquaredFromCenterLineToPoint(const gfx::Point& point,
41 const gfx::Rect& rect) {
42 gfx::Point center_point = rect.CenterPoint();
43 int dx = center_point.x() - point.x();
44 int dy = center_point.y() - point.y();
46 if (rect.width() > rect.height()) {
47 dx = DistanceToInterval(point.x(),
48 rect.x() + (rect.height() / 2),
49 rect.right() - (rect.height() / 2));
51 dy = DistanceToInterval(point.y(),
52 rect.y() + (rect.width() / 2),
53 rect.bottom() - (rect.width() / 2));
55 return (dx * dx) + (dy * dy);