Ultrashock Forums > Flash > ActionScript > UltraMath
• draw curve through 3 points

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
draw curve through 3 points
Old 2002-03-18

I wrote some methods to draw a curve through three points. I took the quadratic Bezier equation and solved for the control point, given the two anchor points and time t=0.5.

P(t) = a(1-t)^2 + b(2t(1-t)) + ct^2
P = P(.5) = .25a + .5b + .25c

// solve for b
.5b = P - .25a - .25c
b = 2P - .5(a + c);

=]


Code:
MovieClip.prototype.drawCurve3Pts = function (x1, y1, x2, y2, x3, y3) {
	var cx = 2*x2 - .5*(x1 + x3);
	var cy = 2*y2 - .5*(y1 + y3);
	this.moveTo (x1, y1);
	this.curveTo (cx, cy, x3, y3);
}; // Robert Penner

MovieClip.prototype.curveToThru = function (px, py, ax, ay) {
	var cx = 2*px - .5*(this._xpen + ax);
	var cy = 2*py - .5*(this._ypen + ay);
	this.curveTo (cx, cy, ax, ay);
}; // Robert Penner


// MovieClip.curveToThru() depends on these methods
// which override the built-in ones:
/*
var MCP = MovieClip.prototype;

MCP.f6moveTo = MCP.moveTo;
MCP.moveTo = function (x, y) {
	with (this) {
		f6moveTo (x, y);
		_xpen = _xpenStart = x;
		_ypen = _ypenStart = y;
	}
};

MCP.f6lineTo = MCP.lineTo;
MCP.lineTo = function (x, y) {
	with (this) {
		f6lineTo (x, y);
		_xpen = x;
		_ypen = y;
	}
};

MCP.f6curveTo = MCP.curveTo;
MCP.curveTo = function (cx, cy, ax, ay) {
	with (this) {
		f6curveTo (cx, cy, ax, ay);
		_xpen = ax;
		_ypen = ay;
	}
};
*/
Here is an example for the drawCurve3Pts() function:

Code:
this.onEnterFrame = function () {
	this.clear();
	this.lineStyle(0);
	this.drawCurve3Pts (100, 100, _xmouse, _ymouse, 400, 400);
};
postbit arrow 8 comments | 2160 views postbit arrow Reply: with Quote   
Moderator
robpenner is offline
seperator
Posts: 822
2001-02-13
Age: 31
seperator

Ultrashock Member Comments:
colorblind colorblind is offline Moderator colorblind lives in Austria 2002-03-19 #2 Old  
Pretty nice equation!
Reply With Quote  
dbonates dbonates is offline 2002-04-17 #3 Old  
Very Cool!!! But it only works on MX?? How to do it on Flash 5???
Reply With Quote  
lifaros lifaros is offline 2002-04-23 #4 Old  
hi dbdonates

sure, it can be done using flash5.
- the first step will be to download a flash 5 bezier engine (in the flas section)
- then, you will need to apply the pennerīs function in order to calc the control point. thats all.!!!

cheers
lifaros
Reply With Quote  
reefscape reefscape is offline 2003-08-20 #5 Old  
through n points
Nice function,

Would it also be possible to rewrite this so it can be used for any number of points?

Bob
Reply With Quote  
McLaranium's Avatar McLaranium McLaranium is offline McLaranium lives in Mexico 2003-09-04 #6 Old  
do I can see one example onLine? I mean: the result some site or SWF.. mo!
Reply With Quote  
jassh jassh is offline jassh lives in Malaysia 2003-09-04 #7 Old  
here's an example of the equation online...


http://www.quinlik.com/~jassh/robpenner/


*webspace Courtesy of DavidNetK of Quinlik.Com
Quinlik.Com Genesis Coming Soon!!
Reply With Quote  
EGOLDY's Avatar EGOLDY EGOLDY is offline EGOLDY lives in China 2003-09-04 #8 Old  
Reply With Quote  
jassh jassh is offline jassh lives in Malaysia 2003-09-06 #9 Old  
here's another example with a twist of a jelly..
err.. i mean jelly effects...

http://quinlik.com/~jassh/robpenner/index2.php

tell me what you think eh !!


-jassh

p/s: Mr. Rob, i hope u dont mind me posting the example



*Webspace courtesy of DavidNetK of Quinlik.com
Quinlik.com Genesis Coming Soon!!!
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: