Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: ui/gfx/geometry/quad_f.cc

Issue 458113002: Optimize QuadF's IsCounterClockwise function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/geometry/quad_f.h" 5 #include "ui/gfx/geometry/quad_f.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 10
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 39
40 bool QuadF::IsCounterClockwise() const { 40 bool QuadF::IsCounterClockwise() const {
41 // This math computes the signed area of the quad. Positive area 41 // This math computes the signed area of the quad. Positive area
42 // indicates the quad is clockwise; negative area indicates the quad is 42 // indicates the quad is clockwise; negative area indicates the quad is
43 // counter-clockwise. Note carefully: this is backwards from conventional 43 // counter-clockwise. Note carefully: this is backwards from conventional
44 // math because our geometric space uses screen coordiantes with y-axis 44 // math because our geometric space uses screen coordiantes with y-axis
45 // pointing downards. 45 // pointing downards.
46 // Reference: http://mathworld.wolfram.com/PolygonArea.html 46 // Reference: http://mathworld.wolfram.com/PolygonArea.html
47 47
48 float tmp1 = p2_.y() - p4_.y();
danakj 2014/08/11 14:18:49 how about naming this p24 or something more descri
kui.zheng 2014/08/12 04:20:55 Done.
49 float tmp2 = p3_.y() - p1_.y();
danakj 2014/08/11 14:18:49 ditto
kui.zheng 2014/08/12 04:20:55 Done.
50
48 // Up-cast to double so this cannot overflow. 51 // Up-cast to double so this cannot overflow.
49 double determinant1 = static_cast<double>(p1_.x()) * p2_.y() 52 double determinant1 = static_cast<double>(p1_.x()) * tmp1;
Ian Vollick 2014/08/11 14:39:09 If I'm understanding things, determinant1 here is
kui.zheng 2014/08/12 04:20:55 Right, This patch just refactoring the computation
50 - static_cast<double>(p2_.x()) * p1_.y(); 53 double determinant2 = static_cast<double>(p2_.x()) * tmp2;
51 double determinant2 = static_cast<double>(p2_.x()) * p3_.y() 54 double determinant3 = static_cast<double>(p3_.x()) * tmp1;
52 - static_cast<double>(p3_.x()) * p2_.y(); 55 double determinant4 = static_cast<double>(p4_.x()) * tmp2;
53 double determinant3 = static_cast<double>(p3_.x()) * p4_.y()
54 - static_cast<double>(p4_.x()) * p3_.y();
55 double determinant4 = static_cast<double>(p4_.x()) * p1_.y()
56 - static_cast<double>(p1_.x()) * p4_.y();
57 56
58 return determinant1 + determinant2 + determinant3 + determinant4 < 0; 57 return determinant1 + determinant2 < determinant3 + determinant4;
59 } 58 }
60 59
61 static inline bool PointIsInTriangle(const PointF& point, 60 static inline bool PointIsInTriangle(const PointF& point,
62 const PointF& r1, 61 const PointF& r1,
63 const PointF& r2, 62 const PointF& r2,
64 const PointF& r3) { 63 const PointF& r3) {
65 // Compute the barycentric coordinates (u, v, w) of |point| relative to the 64 // Compute the barycentric coordinates (u, v, w) of |point| relative to the
66 // triangle (r1, r2, r3) by the solving the system of equations: 65 // triangle (r1, r2, r3) by the solving the system of equations:
67 // 1) point = u * r1 + v * r2 + w * r3 66 // 1) point = u * r1 + v * r2 + w * r3
68 // 2) u + v + w = 1 67 // 2) u + v + w = 1
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 return result; 113 return result;
115 } 114 }
116 115
117 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { 116 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) {
118 QuadF result = lhs; 117 QuadF result = lhs;
119 result -= rhs; 118 result -= rhs;
120 return result; 119 return result;
121 } 120 }
122 121
123 } // namespace gfx 122 } // namespace gfx
OLDNEW
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698