Skip to content
This repository

Support curveTo #1

Merged
merged 1 commit into from over 1 year ago

3 participants

Lee Thomason Jonathan Pace Robert Silverton
Lee Thomason

A simple, initial implementation of curveTo(). A couple areas of improvement are in the notes; I'm happy to tune the function, but wanted to get unblocked from the API not being present at all.

Jonathan Pace

Great! Don't suppose you fancy tackling cubicCurveTo while you're there? :)

One suggestion - is it worth passing the 'numDivisions' as an optional parameter, with a default value of 8? That way if people need to tweak it, there's no need to open up the class.

Robert Silverton
Owner

Thanks Lee, your contributions are obviously very welcome! :D

Lee Thomason

@jonathanrpace, Good reminder on the cubics. Beginning of the week I'll add the cubics, and also add a proper error metric for the quadratics so it can compute a reasonable number of "numDivisions" for you. That will be much better than the current hardcoded value, and if we stiff need tuning that can be added later.

@robsilv You are welcome - glad to see this getting momentum!

Jonathan Pace jonathanrpace merged commit 0e711d3 into from October 08, 2012
Jonathan Pace jonathanrpace closed this October 08, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 05, 2012
Lee Thomason Add support for curveTo 4c2fae0
This page is out of date. Refresh to see the latest.
27  extension/src/starling/display/Graphics.as
@@ -14,6 +14,8 @@ package starling.display
14 14
 	{
15 15
 		private var _currentFillColor	:uint;
16 16
 		private var _currentFillAlpha	:Number;
  17
+		private var _currentX			:Number;
  18
+		private var _currentY			:Number;
17 19
 		
18 20
 		private var _strokeThickness	:Number
19 21
 		private var _strokeColor		:uint;
@@ -43,6 +45,8 @@ package starling.display
43 45
 				child.dispose();
44 46
 				_container.removeChildAt(0);
45 47
 			}
  48
+			_currentX = NaN;
  49
+			_currentY = NaN;
46 50
 		}
47 51
 		
48 52
 		public function beginFill(color:uint, alpha:Number = 1.0):void
@@ -179,6 +183,27 @@ package starling.display
179 183
 					_currentFill.addVertex(x, y, _currentFillColor, _currentFillAlpha );
180 184
 				}
181 185
 			}
  186
+			_currentX = x;
  187
+			_currentY = y;
  188
+		}
  189
+		
  190
+		public function curveTo(cx:Number, cy:Number, a2x:Number, a2y:Number):void
  191
+		{
  192
+			// A couple of improvements to this code:
  193
+			// - Should be unified with 'lineTo' so there is prefix & postfix code, but the main code calls "addVertex" directly.
  194
+			// - "8" subdivisions should be experimented with: 2-16 is about right, depending on the screen size of the curve.
  195
+			
  196
+			for ( var j:int = 1; j <= 8; ++j ) {
  197
+				var t:Number = Number(j) / 8.0;
  198
+				var oneMinusT:Number = (1.0 - t);
  199
+				var bx:Number = oneMinusT * oneMinusT * _currentX + 2.0 * oneMinusT * t * cx + t * t * a2x;
  200
+				var by:Number = oneMinusT * oneMinusT * _currentY + 2.0 * oneMinusT * t * cy + t * t * a2y;
  201
+				
  202
+				lineTo( bx, by );
  203
+			}			
  204
+			
  205
+			_currentX = a2x;
  206
+			_currentY = a2y;
182 207
 		}
183 208
 		
184 209
 		public function moveTo(x:Number, y:Number):void
@@ -202,6 +227,8 @@ package starling.display
202 227
 					_currentFill.addVertex(x, y, _currentFillColor, _currentFillAlpha );
203 228
 				}
204 229
 			}
  230
+			_currentX = x;
  231
+			_currentY = y;
205 232
 		}
206 233
 		
207 234
 		private function beginStroke():void
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.