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

Side by Side Diff: Source/core/html/canvas/CanvasPathMethods.cpp

Issue 170453004: Add constant twoPiDouble/twoPiFloat to MathExtras.h (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: add constant values to header Created 6 years, 10 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 | « no previous file | Source/core/rendering/shapes/PolygonShape.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 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved. 9 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
10 * 10 *
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 else if (p1 == m_path.currentPoint() || p1 == p2 || !r) 128 else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
129 lineTo(x1, y1); 129 lineTo(x1, y1);
130 else 130 else
131 m_path.addArcTo(p1, p2, r); 131 m_path.addArcTo(p1, p2, r);
132 } 132 }
133 133
134 namespace { 134 namespace {
135 135
136 float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise) 136 float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
137 { 137 {
138 float twoPi = 2 * piFloat;
139 float newEndAngle = endAngle; 138 float newEndAngle = endAngle;
140 /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-el ement.html#dom-context-2d-arc 139 /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-el ement.html#dom-context-2d-arc
141 * If the anticlockwise argument is false and endAngle-startAngle is equal t o or greater than 2pi, or, 140 * If the anticlockwise argument is false and endAngle-startAngle is equal t o or greater than 2pi, or,
142 * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi, 141 * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi,
143 * then the arc is the whole circumference of this ellipse, and the point at startAngle along this circle's circumference, 142 * then the arc is the whole circumference of this ellipse, and the point at startAngle along this circle's circumference,
144 * measured in radians clockwise from the ellipse's semi-major axis, acts as both the start point and the end point. 143 * measured in radians clockwise from the ellipse's semi-major axis, acts as both the start point and the end point.
145 */ 144 */
146 if (!anticlockwise && endAngle - startAngle >= twoPi) 145 if (!anticlockwise && endAngle - startAngle >= twoPiFloat)
147 newEndAngle = startAngle + twoPi; 146 newEndAngle = startAngle + twoPiFloat;
148 else if (anticlockwise && startAngle - endAngle >= twoPi) 147 else if (anticlockwise && startAngle - endAngle >= twoPiFloat)
149 newEndAngle = startAngle - twoPi; 148 newEndAngle = startAngle - twoPiFloat;
150 149
151 /* 150 /*
152 * Otherwise, the arc is the path along the circumference of this ellipse fr om the start point to the end point, 151 * Otherwise, the arc is the path along the circumference of this ellipse fr om the start point to the end point,
153 * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise. 152 * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
154 * Since the points are on the ellipse, as opposed to being simply angles fr om zero, 153 * Since the points are on the ellipse, as opposed to being simply angles fr om zero,
155 * the arc can never cover an angle greater than 2pi radians. 154 * the arc can never cover an angle greater than 2pi radians.
156 */ 155 */
157 /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the s pec does not indicate clearly. 156 /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the s pec does not indicate clearly.
158 * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*M ath.PI, true) to draw circle. 157 * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*M ath.PI, true) to draw circle.
159 * We preserve backward-compatibility. 158 * We preserve backward-compatibility.
160 */ 159 */
161 else if (!anticlockwise && startAngle > endAngle) 160 else if (!anticlockwise && startAngle > endAngle)
162 newEndAngle = startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi)) ; 161 newEndAngle = startAngle + (twoPiFloat - fmodf(startAngle - endAngle, tw oPiFloat));
163 else if (anticlockwise && startAngle < endAngle) 162 else if (anticlockwise && startAngle < endAngle)
164 newEndAngle = startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi)) ; 163 newEndAngle = startAngle - (twoPiFloat - fmodf(endAngle - startAngle, tw oPiFloat));
165 164
166 ASSERT(ellipseIsRenderable(startAngle, newEndAngle)); 165 ASSERT(ellipseIsRenderable(startAngle, newEndAngle));
167 return newEndAngle; 166 return newEndAngle;
168 } 167 }
169 168
170 inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p) 169 inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
171 { 170 {
172 path->lineTo(p.x(), p.y()); 171 path->lineTo(p.x(), p.y());
173 } 172 }
174 173
175 inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta) 174 inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
176 { 175 {
177 return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta)); 176 return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
178 } 177 }
179 178
180 void canonicalizeAngle(float* startAngle, float* endAngle) 179 void canonicalizeAngle(float* startAngle, float* endAngle)
181 { 180 {
182 // Make 0 <= startAngle < 2*PI 181 // Make 0 <= startAngle < 2*PI
183 float twoPi = 2 * piFloat;
184 float newStartAngle = *startAngle; 182 float newStartAngle = *startAngle;
185 if (newStartAngle < 0) 183 if (newStartAngle < 0)
186 newStartAngle = twoPi + fmodf(newStartAngle, -twoPi); 184 newStartAngle = twoPiFloat + fmodf(newStartAngle, -twoPiFloat);
187 else 185 else
188 newStartAngle = fmodf(newStartAngle, twoPi); 186 newStartAngle = fmodf(newStartAngle, twoPiFloat);
189 187
190 float delta = newStartAngle - *startAngle; 188 float delta = newStartAngle - *startAngle;
191 *startAngle = newStartAngle; 189 *startAngle = newStartAngle;
192 *endAngle = *endAngle + delta; 190 *endAngle = *endAngle + delta;
193 ASSERT(newStartAngle >= 0 && newStartAngle < twoPi); 191 ASSERT(newStartAngle >= 0 && newStartAngle < twoPiFloat);
194 } 192 }
195 193
196 /* 194 /*
197 * degenerateEllipse() handles a degenerated ellipse using several lines. 195 * degenerateEllipse() handles a degenerated ellipse using several lines.
198 * 196 *
199 * Let's see a following example: line to ellipse to line. 197 * Let's see a following example: line to ellipse to line.
200 * _--^\ 198 * _--^\
201 * ( ) 199 * ( )
202 * -----( ) 200 * -----( )
203 * ) 201 * )
(...skipping 16 matching lines...) Expand all
220 * ---------- 218 * ----------
221 * ``P 219 * ``P
222 * Angles for P are 0 and Pi in the ellipse coordinates. 220 * Angles for P are 0 and Pi in the ellipse coordinates.
223 * 221 *
224 * To handle both cases, degenerateEllipse() lines to start angle, local maximum points(every 0.5Pi), and end angle. 222 * To handle both cases, degenerateEllipse() lines to start angle, local maximum points(every 0.5Pi), and end angle.
225 * NOTE: Before ellipse() calls this function, adjustEndAngle() is called, so en dAngle - startAngle must be equal to or less than 2Pi. 223 * NOTE: Before ellipse() calls this function, adjustEndAngle() is called, so en dAngle - startAngle must be equal to or less than 2Pi.
226 */ 224 */
227 void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlock wise) 225 void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlock wise)
228 { 226 {
229 ASSERT(ellipseIsRenderable(startAngle, endAngle)); 227 ASSERT(ellipseIsRenderable(startAngle, endAngle));
230 ASSERT(startAngle >= 0 && startAngle < 2 * piFloat); 228 ASSERT(startAngle >= 0 && startAngle < twoPiFloat);
231 ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0)); 229 ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
232 230
233 FloatPoint center(x, y); 231 FloatPoint center(x, y);
234 AffineTransform rotationMatrix; 232 AffineTransform rotationMatrix;
235 rotationMatrix.rotate(rad2deg(rotation)); 233 rotationMatrix.rotate(rad2deg(rotation));
236 // First, if the object's path has any subpaths, then the method must add a straight line from the last point in the subpath to the start point of the arc. 234 // First, if the object's path has any subpaths, then the method must add a straight line from the last point in the subpath to the start point of the arc.
237 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(ra diusX, radiusY, startAngle))); 235 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(ra diusX, radiusY, startAngle)));
238 if ((!radiusX && !radiusY) || startAngle == endAngle) 236 if ((!radiusX && !radiusY) || startAngle == endAngle)
239 return; 237 return;
240 238
241 float halfPiFloat = piFloat * 0.5;
242 if (!anticlockwise) { 239 if (!anticlockwise) {
243 // startAngle - fmodf(startAngle, halfPiFloat) + halfPiFloat is the one of (0, 0.5Pi, Pi, 1.5Pi, 2Pi) 240 // startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat is th e one of (0, 0.5Pi, Pi, 1.5Pi, 2Pi)
244 // that is the closest to startAngle on the clockwise direction. 241 // that is the closest to startAngle on the clockwise direction.
245 for (float angle = startAngle - fmodf(startAngle, halfPiFloat) + halfPiF loat; angle < endAngle; angle += halfPiFloat) 242 for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat) + piOv erTwoFloat; angle < endAngle; angle += piOverTwoFloat)
246 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEl lipse(radiusX, radiusY, angle))); 243 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEl lipse(radiusX, radiusY, angle)));
247 } else { 244 } else {
248 for (float angle = startAngle - fmodf(startAngle, halfPiFloat); angle > endAngle; angle -= halfPiFloat) 245 for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat); angle > endAngle; angle -= piOverTwoFloat)
249 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEl lipse(radiusX, radiusY, angle))); 246 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEl lipse(radiusX, radiusY, angle)));
250 } 247 }
251 248
252 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(ra diusX, radiusY, endAngle))); 249 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(ra diusX, radiusY, endAngle)));
253 } 250 }
254 251
255 } // namespace 252 } // namespace
256 253
257 void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, fl oat endAngle, bool anticlockwise, ExceptionState& exceptionState) 254 void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, fl oat endAngle, bool anticlockwise, ExceptionState& exceptionState)
258 { 255 {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 return; 312 return;
316 313
317 if (!width && !height) { 314 if (!width && !height) {
318 m_path.moveTo(FloatPoint(x, y)); 315 m_path.moveTo(FloatPoint(x, y));
319 return; 316 return;
320 } 317 }
321 318
322 m_path.addRect(FloatRect(x, y, width, height)); 319 m_path.addRect(FloatRect(x, y, width, height));
323 } 320 }
324 } 321 }
OLDNEW
« no previous file with comments | « no previous file | Source/core/rendering/shapes/PolygonShape.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698