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

Side by Side Diff: src/core/SkPoint.cpp

Issue 785933003: change SkPoint::setLength to set itself to (0,0) if it starting length is degenerate (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years 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 | « src/core/SkPath.cpp ('k') | tests/PointTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2008 The Android Open Source Project 3 * Copyright 2008 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkMathPriv.h" 10 #include "SkMathPriv.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 float *lengthSquared) { 93 float *lengthSquared) {
94 *lengthSquared = getLengthSquared(dx, dy); 94 *lengthSquared = getLengthSquared(dx, dy);
95 return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero); 95 return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
96 } 96 }
97 97
98 SkScalar SkPoint::Normalize(SkPoint* pt) { 98 SkScalar SkPoint::Normalize(SkPoint* pt) {
99 float x = pt->fX; 99 float x = pt->fX;
100 float y = pt->fY; 100 float y = pt->fY;
101 float mag2; 101 float mag2;
102 if (isLengthNearlyZero(x, y, &mag2)) { 102 if (isLengthNearlyZero(x, y, &mag2)) {
103 pt->set(0, 0);
103 return 0; 104 return 0;
104 } 105 }
105 106
106 float mag, scale; 107 float mag, scale;
107 if (SkScalarIsFinite(mag2)) { 108 if (SkScalarIsFinite(mag2)) {
108 mag = sk_float_sqrt(mag2); 109 mag = sk_float_sqrt(mag2);
109 scale = 1 / mag; 110 scale = 1 / mag;
110 } else { 111 } else {
111 // our mag2 step overflowed to infinity, so use doubles instead. 112 // our mag2 step overflowed to infinity, so use doubles instead.
112 // much slower, but needed when x or y are very large, other wise we 113 // much slower, but needed when x or y are very large, other wise we
(...skipping 27 matching lines...) Expand all
140 * We have to worry about 2 tricky conditions: 141 * We have to worry about 2 tricky conditions:
141 * 1. underflow of mag2 (compared against nearlyzero^2) 142 * 1. underflow of mag2 (compared against nearlyzero^2)
142 * 2. overflow of mag2 (compared w/ isfinite) 143 * 2. overflow of mag2 (compared w/ isfinite)
143 * 144 *
144 * If we underflow, we return false. If we overflow, we compute again using 145 * If we underflow, we return false. If we overflow, we compute again using
145 * doubles, which is much slower (3x in a desktop test) but will not overflow. 146 * doubles, which is much slower (3x in a desktop test) but will not overflow.
146 */ 147 */
147 bool SkPoint::setLength(float x, float y, float length) { 148 bool SkPoint::setLength(float x, float y, float length) {
148 float mag2; 149 float mag2;
149 if (isLengthNearlyZero(x, y, &mag2)) { 150 if (isLengthNearlyZero(x, y, &mag2)) {
151 this->set(0, 0);
150 return false; 152 return false;
151 } 153 }
152 154
153 float scale; 155 float scale;
154 if (SkScalarIsFinite(mag2)) { 156 if (SkScalarIsFinite(mag2)) {
155 scale = length / sk_float_sqrt(mag2); 157 scale = length / sk_float_sqrt(mag2);
156 } else { 158 } else {
157 // our mag2 step overflowed to infinity, so use doubles instead. 159 // our mag2 step overflowed to infinity, so use doubles instead.
158 // much slower, but needed when x or y are very large, other wise we 160 // much slower, but needed when x or y are very large, other wise we
159 // divide by inf. and return (0,0) vector. 161 // divide by inf. and return (0,0) vector.
(...skipping 16 matching lines...) Expand all
176 return true; 178 return true;
177 } 179 }
178 180
179 bool SkPoint::setLengthFast(float length) { 181 bool SkPoint::setLengthFast(float length) {
180 return this->setLengthFast(fX, fY, length); 182 return this->setLengthFast(fX, fY, length);
181 } 183 }
182 184
183 bool SkPoint::setLengthFast(float x, float y, float length) { 185 bool SkPoint::setLengthFast(float x, float y, float length) {
184 float mag2; 186 float mag2;
185 if (isLengthNearlyZero(x, y, &mag2)) { 187 if (isLengthNearlyZero(x, y, &mag2)) {
188 this->set(0, 0);
186 return false; 189 return false;
187 } 190 }
188 191
189 float scale; 192 float scale;
190 if (SkScalarIsFinite(mag2)) { 193 if (SkScalarIsFinite(mag2)) {
191 scale = length * sk_float_rsqrt(mag2); // <--- this is the difference 194 scale = length * sk_float_rsqrt(mag2); // <--- this is the difference
192 } else { 195 } else {
193 // our mag2 step overflowed to infinity, so use doubles instead. 196 // our mag2 step overflowed to infinity, so use doubles instead.
194 // much slower, but needed when x or y are very large, other wise we 197 // much slower, but needed when x or y are very large, other wise we
195 // divide by inf. and return (0,0) vector. 198 // divide by inf. and return (0,0) vector.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 252
250 if (uDotV <= 0) { 253 if (uDotV <= 0) {
251 return v.lengthSqd(); 254 return v.lengthSqd();
252 } else if (uDotV > uLengthSqd) { 255 } else if (uDotV > uLengthSqd) {
253 return b.distanceToSqd(*this); 256 return b.distanceToSqd(*this);
254 } else { 257 } else {
255 SkScalar det = u.cross(v); 258 SkScalar det = u.cross(v);
256 return SkScalarMulDiv(det, det, uLengthSqd); 259 return SkScalarMulDiv(det, det, uLengthSqd);
257 } 260 }
258 } 261 }
OLDNEW
« no previous file with comments | « src/core/SkPath.cpp ('k') | tests/PointTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698