- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / rect_based_targeting_utils.cc
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
5 #include "ui/views/rect_based_targeting_utils.h"
6
7 #include "ui/gfx/point.h"
8 #include "ui/gfx/rect.h"
9
10 namespace views {
11
12 namespace {
13
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
16 // 0 is returned.
17 int DistanceToInterval(int pos, int start, int end) {
18   if (pos < start)
19     return start - pos;
20   if (pos > end)
21     return pos - end;
22   return 0;
23 }
24
25 }  // namespace
26
27 bool UsePointBasedTargeting(const gfx::Rect& rect) {
28   return rect.width() == 1 && rect.height() == 1;
29 }
30
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();
36   return rect_1_area ?
37       static_cast<float>(intersect_area) / static_cast<float>(rect_1_area) : 0;
38 }
39
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();
45
46   if (rect.width() > rect.height()) {
47     dx = DistanceToInterval(point.x(),
48                             rect.x() + (rect.height() / 2),
49                             rect.right() - (rect.height() / 2));
50   } else {
51     dy = DistanceToInterval(point.y(),
52                             rect.y() + (rect.width() / 2),
53                             rect.bottom() - (rect.width() / 2));
54   }
55   return (dx * dx) + (dy * dy);
56 }
57
58 }  // namespace views