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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/quad_f.cc
diff --git a/ui/gfx/geometry/quad_f.cc b/ui/gfx/geometry/quad_f.cc
index dbc50458b3fa1e04aedfdee3c51d5ae00351b46a..1fcb464948007971b66602b58c5b2e57478b27b2 100644
--- a/ui/gfx/geometry/quad_f.cc
+++ b/ui/gfx/geometry/quad_f.cc
@@ -43,19 +43,23 @@ bool QuadF::IsCounterClockwise() const {
// counter-clockwise. Note carefully: this is backwards from conventional
// math because our geometric space uses screen coordiantes with y-axis
// pointing downards.
- // Reference: http://mathworld.wolfram.com/PolygonArea.html
+ // Reference: http://mathworld.wolfram.com/PolygonArea.html.
+ // The equation can be written:
+ // Signed area = determinant1 + determinant2 + determinant3 + determinant4
+ // In practise, Refactoring the computation of adding determinants so that
+ // reducing the number of operations. The equation is:
+ // Signed area = element1 + element2 - element3 - element4
+
+ float p24 = p2_.y() - p4_.y();
+ float p31 = p3_.y() - p1_.y();
// Up-cast to double so this cannot overflow.
- double determinant1 = static_cast<double>(p1_.x()) * p2_.y()
- - static_cast<double>(p2_.x()) * p1_.y();
- double determinant2 = static_cast<double>(p2_.x()) * p3_.y()
- - static_cast<double>(p3_.x()) * p2_.y();
- double determinant3 = static_cast<double>(p3_.x()) * p4_.y()
- - static_cast<double>(p4_.x()) * p3_.y();
- double determinant4 = static_cast<double>(p4_.x()) * p1_.y()
- - static_cast<double>(p1_.x()) * p4_.y();
-
- return determinant1 + determinant2 + determinant3 + determinant4 < 0;
+ double element1 = static_cast<double>(p1_.x()) * p24;
+ double element2 = static_cast<double>(p2_.x()) * p31;
+ double element3 = static_cast<double>(p3_.x()) * p24;
+ double element4 = static_cast<double>(p4_.x()) * p31;
+
+ return element1 + element2 < element3 + element4;
}
static inline bool PointIsInTriangle(const PointF& point,
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698