OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/gfx/quad_f.h" | |
6 | |
7 #include <cmath> | |
8 #include <limits> | |
9 | |
10 #include "base/stringprintf.h" | |
11 | |
12 namespace gfx { | |
13 | |
14 QuadF::QuadF() { | |
15 } | |
16 | |
17 QuadF::QuadF(const PointF& p1, | |
18 const PointF& p2, | |
19 const PointF& p3, | |
20 const PointF& p4) | |
21 : p1_(p1), | |
22 p2_(p2), | |
23 p3_(p3), | |
24 p4_(p4) { | |
25 } | |
26 | |
27 QuadF::QuadF(const RectF& rect) | |
28 : p1_(rect.x(), rect.y()), | |
29 p2_(rect.right(), rect.y()), | |
30 p3_(rect.right(), rect.bottom()), | |
31 p4_(rect.x(), rect.bottom()) { | |
32 } | |
33 | |
34 QuadF::~QuadF() { | |
35 } | |
36 | |
37 void QuadF::operator=(const QuadF& quad) { | |
38 p1_ = quad.p1_; | |
39 p2_ = quad.p2_; | |
40 p3_ = quad.p3_; | |
41 p4_ = quad.p4_; | |
42 } | |
43 | |
44 void QuadF::operator=(const RectF& rect) { | |
45 p1_ = PointF(rect.x(), rect.y()); | |
46 p2_ = PointF(rect.right(), rect.y()); | |
47 p3_ = PointF(rect.right(), rect.bottom()); | |
48 p4_ = PointF(rect.x(), rect.bottom()); | |
49 } | |
50 | |
51 std::string QuadF::ToString() const { | |
52 return base::StringPrintf("%s;%s;%s;%s", | |
53 p1_.ToString().c_str(), | |
54 p2_.ToString().c_str(), | |
55 p3_.ToString().c_str(), | |
56 p4_.ToString().c_str()); | |
57 } | |
58 | |
59 static inline bool WithinEpsilon(float a, float b) | |
60 { | |
sky
2012/11/02 15:49:43
{ on previous line.
danakj
2012/11/02 15:55:00
Done.
| |
61 return std::abs(a - b) < std::numeric_limits<float>::epsilon(); | |
62 } | |
63 | |
64 bool QuadF::IsRectilinear() const { | |
65 return (WithinEpsilon(p1_.x(), p2_.x()) && WithinEpsilon(p2_.y(), p3_.y()) && | |
66 WithinEpsilon(p3_.x(), p4_.x()) && WithinEpsilon(p4_.y(), p1_.y())) | |
67 || (WithinEpsilon(p1_.y(), p2_.y()) && WithinEpsilon(p2_.x(), p3_.x()) && | |
sky
2012/11/02 15:49:43
|| on previous line (wrap if you need to).
danakj
2012/11/02 15:55:00
Done.
| |
68 WithinEpsilon(p3_.y(), p4_.y()) && WithinEpsilon(p4_.x(), p1_.x())); | |
69 } | |
70 | |
71 bool QuadF::IsCounterClockwise() const { | |
72 // Compute if the first three points turn counter-clockwise. If the quad is | |
73 // convex, this will match the result for all other triples of points as well. | |
74 return CrossProduct(p2_ - p1_, p3_ - p2_) < 0; | |
75 } | |
76 | |
77 static inline bool PointIsInTriangle(const PointF& point, | |
78 const PointF& r1, | |
79 const PointF& r2, | |
80 const PointF& r3) { | |
81 // Compute the barycentric coordinates of |point| relative to the triangle | |
82 // (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time | |
83 // Collision Detection. | |
84 Vector2dF v0 = r2 - r1; | |
85 Vector2dF v1 = r3 - r1; | |
86 Vector2dF v2 = point - r1; | |
87 | |
88 double dot00 = DotProduct(v0, v0); | |
89 double dot01 = DotProduct(v0, v1); | |
90 double dot11 = DotProduct(v1, v1); | |
91 double dot20 = DotProduct(v2, v0); | |
92 double dot21 = DotProduct(v2, v1); | |
93 | |
94 double denom = dot00 * dot11 - dot01 * dot01; | |
95 | |
96 double v = (dot11 * dot20 - dot01 * dot21) / denom; | |
97 double w = (dot00 * dot21 - dot01 * dot20) / denom; | |
98 double u = 1 - v - w; | |
99 | |
100 // Use the barycentric coordinates to test if |point| is inside the | |
101 // triangle (r1, r2, r2). | |
102 return (v >= 0) && (w >= 0) && (u >= 0); | |
103 } | |
104 | |
105 bool QuadF::Contains(const PointF& point) const { | |
106 return PointIsInTriangle(point, p1_, p2_, p3_) | |
107 || PointIsInTriangle(point, p1_, p3_, p4_); | |
108 } | |
109 | |
110 RectF QuadF::BoundingBox() const { | |
111 float rl = std::min(std::min(p1_.x(), p2_.x()), std::min(p3_.x(), p4_.x())); | |
112 float rr = std::max(std::max(p1_.x(), p2_.x()), std::max(p3_.x(), p4_.x())); | |
113 float rt = std::min(std::min(p1_.y(), p2_.y()), std::min(p3_.y(), p4_.y())); | |
114 float rb = std::max(std::max(p1_.y(), p2_.y()), std::max(p3_.y(), p4_.y())); | |
115 return RectF(rl, rt, rr - rl, rb - rt); | |
116 } | |
117 | |
118 void QuadF::Scale(float x_scale, float y_scale) { | |
119 p1_ = p1_.Scale(x_scale, y_scale); | |
120 p2_ = p2_.Scale(x_scale, y_scale); | |
121 p3_ = p3_.Scale(x_scale, y_scale); | |
122 p4_ = p4_.Scale(x_scale, y_scale); | |
123 } | |
124 | |
125 void QuadF::operator+=(const Vector2dF& rhs) { | |
126 p1_ += rhs; | |
127 p2_ += rhs; | |
128 p3_ += rhs; | |
129 p4_ += rhs; | |
130 } | |
131 | |
132 void QuadF::operator-=(const Vector2dF& rhs) { | |
133 p1_ -= rhs; | |
134 p2_ -= rhs; | |
135 p3_ -= rhs; | |
136 p4_ -= rhs; | |
137 } | |
138 | |
139 QuadF operator+(const QuadF& lhs, const Vector2dF& rhs) { | |
140 QuadF result = lhs; | |
141 result += rhs; | |
142 return result; | |
143 } | |
144 | |
145 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { | |
146 QuadF result = lhs; | |
147 result -= rhs; | |
148 return result; | |
149 } | |
150 | |
151 } // namespace gfx | |
OLD | NEW |