06-20-2004, 01:17 AM
|
#1
|
|
member
Join Date: Jul 2003
Location: CA
Posts: 442
|
yin & yang : better/shorter way?
there's gotta be a better and shorter way 2 do this right?
PHP Code:
// Basic YinYang routines - Jim Bumgardner
//
R = Math.sqrt(300*300+300*300)*2;
Rh = R/2;
degToRad = Math.PI/180;
sqrt2 = Math.sqrt(2);
++initCtr;
if (initCtr > 1)
trace("Script called twice");
function drawArc(mc, x,y,radius,bA,eA)
{
var r = radius;
var theta = 45*degToRad;
var cr = radius/Math.cos(theta/2);
var angle = (ba+45)*degToRad;
var cangle = angle-theta/2;
var n = (ea-ba)/45;
for (var i=0;i < n;i++,angle += theta,cangle += theta)
{
var endX = r*Math.cos (angle);
var endY = r*Math.sin (angle);
var cX = cr*Math.cos (cangle);
var cY = cr*Math.sin (cangle);
mc.curveTo(cX+x,cY+y, endX+x,endY+y);
}
}
function drawNegArc(mc, x,y,radius,bA,eA)
{
var r = radius;
var theta = 45*degToRad;
var cr = radius/Math.cos(theta/2);
var angle = (ba+45)*degToRad;
var cangle = angle-theta/2;
var n = (ea-ba)/45;
for (var i=0;i < n;i++,angle += theta,cangle += theta)
{
var endX = r*Math.cos (angle);
var endY = r*Math.sin (angle);
var cX = cr*Math.cos (cangle);
var cY = cr*Math.sin (cangle);
mc.curveTo(x+cX,y-cY, x+endX,y-endY);
}
}
function drawYin(mc, x, y, radius, fC, fA, withHole)
{
var rad2 = radius/2;
mc.moveTo(x+radius,y); // same as x+cos(0)*r, y+sin(0)*r
mc.beginFill(fC,fA);
drawArc(mc, x, y, radius, 0, 180);
drawArc(mc, x-rad2,y, rad2, 180, 360);
drawNegArc(mc, x+rad2,y, rad2, 180, 360);
//
if (withHole) {
var radC = rad2*(sqrt2-1);
mc.moveTo(x+radC-rad2,y);
drawArc(mc, x-rad2, y, radC, 0, 360);
}
mc.endFill();
}
// Unused - use this to fill in the other half of a 2-color design
function drawYang(mc, x, y, radius, fC, fA, withHole)
{
var rad2 = radius/2;
mc.moveTo(x-radius,y); // same as x+cos(0)*r, y+sin(0)*r
mc.beginFill(fC,fA);
drawArc(mc, x, y, radius, 180, 360);
drawArc(mc, x+rad2,y, rad2, 0, 180);
drawNegArc(mc, x-rad2,y, rad2, 0, 180);
if (withHole) {
var radC = rad2*(sqrt2-1);
mc.moveTo(x+rad2+radC,y);
drawArc(mc, x+rad2, y, radC, 0, 360);
}
mc.endFill();
}
function drawCircle(mc, x,y,radius, fC,fA)
{
mc.moveTo(x+radius,y); // same as x+cos(0)*r, y+sin(0)*r
mc.beginFill(fC,fA);
drawArc(mc, x, y, radius, 0, 360);
mc.endFill()
}
function drawCircleOutline(mc, x,y,radius, fC,fA,thick,bA,eA)
{
mc.lineStyle(thick,fC,fA);
mc.moveTo(x+radius*Math.cos(bA*degToRad),y+radius*Math.sin(bA*degToRad)); // same as x+cos(0)*r, y+sin(0)*r
drawArc(mc, x, y, radius, bA, eA);
mc.lineStyle(0);
}
var g_alphas = [100,0];
var g_ratios = [0x0,0xFF];
var radians = 0;
// build our matrix using the "box" method
var g_matrix = { matrixType:"box", x:-R, y:-R, w:R*2, h:R*2, r: radians }
function drawGradCircle(mc, x,y,radius, fC)
{
var colors = [fC,fC];
mc.moveTo(x+radius,y); // same as x+cos(0)*r, y+sin(0)*r
mc.beginGradientFill("radial",colors,g_alphas,g_ratios,g_matrix);
// mc.beginFill(fC,0x1000000 - fC);
drawArc(mc, x, y, radius, 0, 360);
mc.endFill()
}
function drawSprite()
{
var t = getTimer() * .05;
// t = 0;
var red = 128 + 127 * Math.sin((0 + this.cphase + t) * degToRad);
var grn = 128 + 127 * Math.sin((120 + this.cphase + t) * degToRad);
var blu = 128 + 127 * Math.sin((240 + this.cphase + t) * degToRad);
var tint = (red << 16) | (grn << 8) | blu;
this.clear();
drawGradCircle(this,0,0,R,tint); // cicle within Yin
}
function drawYinYang()
{
var sr = Rh*.125;
var sr2 = sr/2;
var x = 0;
var y = 0;
this.clear();
// These are the componenet parts of the yin-yang symbol, use which
// ever ones you want...
drawYin(this, 0, 0, sr, 0x000000, 25,true);
// drawYang(this, 0, 0, sr, 0x0000FF, 50,true);
// drawCircle(this,x-sr2,0,sr2*(sqrt2-1),0x0000FF,50); // cicle within Yin
drawCircle(this,x+sr2,0,sr2*(sqrt2-1),0x000000,25); // circle within Yang
drawCircleOutline(this,0,0,sr,0x000000,25,0,0,360); // outline around whole
}
function updateFrame()
{
this._rotation += 1;
}
function init()
{
trace("Init 1");
if (inited != undefined)
return;
// var h = this.attachMovie("dummy", "yin", 0);
var nbrSprites = 3;
var cont = this.createEmptyMovieClip("container", 0);
cont._x = 150;
cont._y = 150;
// cont._width = 300;
// cont._height = 300;
cont.onEnterFrame = updateFrame;
for (var n = 0; n < nbrSprites; ++n) {
var mc = cont.createEmptyMovieClip("sprite", n);
mc.cphase = (n*360/nbrSprites);
mc._x = Math.sin(mc.cphase * degToRad) * R/2;
mc._y = Math.cos(mc.cphase * degToRad) * R/2;
mc.onEnterFrame = drawSprite;
}
var mc = cont.createEmptyMovieClip("yinyang", nbrSprites);
mc._x = 0;
mc._y = 0;
mc.onEnterFrame = drawYinYang;
// h.Play();
inited = true;
}
init();
__________________
1+1=1;
|
|
|
06-20-2004, 11:37 AM
|
#2
|
|
village halfwit
Join Date: Jul 2001
Location: USA, PA
Posts: 3,328
|
Yes there is. Draw the symbol in the Flash authoring environment and turn it into a MovieClip then give it an onEnterFrame event that does something like "this._rotation += 2".
-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
|
|
|
06-20-2004, 12:14 PM
|
#3
|
|
member
Join Date: Jul 2003
Location: CA
Posts: 442
|
i read ur response & been laughin ever since
that IS a much shorter & easier way, isn't it?
__________________
1+1=1;
|
|
|
06-20-2004, 12:45 PM
|
#4
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
Haha... Same here... I was thinking how many times did the author have to run trial and error to get the shape right... Indeed... Drawing it and making a movieClip out of it is way simpler than those math stuff that gives me headache... Nice effect though...
|
|
|
06-20-2004, 01:06 PM
|
#5
|
|
village halfwit
Join Date: Jul 2001
Location: USA, PA
Posts: 3,328
|
I doubt the person who wrote that code was trying to do it the "easiest" way possible. It was probably done more as a learning experience for personal satisfaction or as an educational tool. I know I've done plenty of things "the hard way" just to see if it could be done.
-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
|
|
|
06-20-2004, 01:09 PM
|
#6
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
Oh... I didn't mean it that way, pixelwit... 
I am really bad at the drawing api as I am with many other things...
I just get amazed to see how people use drawing api to make complicate things with the script so easily... (I am sure there are big trial and error sesseion involved in it...)
That's all I am saying... I wasn't saying what is good or bad way of doing it...
|
|
|
06-20-2004, 01:14 PM
|
#7
|
|
village halfwit
Join Date: Jul 2001
Location: USA, PA
Posts: 3,328
|
I know you didn't mean anything by it CyanBlue. I'm not out to get you.  I was only trying to explain why people sometimes do the things they do.
-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
|
|
|
06-20-2004, 01:18 PM
|
#8
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
How did you know that I have nightmare everynight and some weird shape of poligons chasing me every night yet I don't get to lose anything in my tummy???
|
|
|
06-20-2004, 01:26 PM
|
#9
|
|
village halfwit
Join Date: Jul 2001
Location: USA, PA
Posts: 3,328
|
A comment that's little more on-topic for Hognav2t...
Not sure why you would need a drawNegArc function or why you'd need both a yin and yang function since you could just draw one or the other in separate clips then rotate the clip 180 degrees. Or better yet, just draw one of them over top of a plain circle.
But then again, writing less code may not have been one of the author's goals.
-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
|
|
|
06-21-2004, 02:04 AM
|
#10
|
|
member
Join Date: Jul 2003
Location: CA
Posts: 442
|
this guy actually teaches flash AS, may b u guys should consider it 2. i know a good-
handfull of members in this forum who r well qualified 2 teach AS.
__________________
1+1=1;
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 01:22 AM.
///
|
|