4節リンク機構の置き換え
カム的な何か、に置き換えてみた
/**
* Copyright Nos_lkSsvOhB ( http://wonderfl.net/user/Nos_lkSsvOhB )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7oUh
*/
package {
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.Event;
import flash.*;
public class Linkage extends Sprite {
private var a:Number=80;
private var b:Number=220;
private var c:Number=140;
private var d:Number=240;
private var t:Number=8;
private var theta:Number=0;
private var xa:Number=100;
private var ya:Number=280;
private var xc:Number=xa+d;
private var yc:Number=ya;
private var xd:Number=xa;
private var yd:Number=ya;
private var bx0:Number;
private var by0:Number;
private var bx1:Number;
private var by1:Number;
private var link1:Sprite;
private var link2:Sprite;
private var link3:Sprite;
private var link4:Sprite;
private var trace1:TextField;
private var al1:Number;
private var al2:Number;
private var L:Number;
private var L2:Number;
private var circleA:Sprite;
private var circleB:Sprite;
private var pA:Sprite;
private var pB:Sprite;
private var pC:Sprite;
private var pD:Sprite;
private var p:Number=2;
public function Linkage() {
circleA=new Sprite();
circleA.graphics.beginFill(0x123456);
circleA.graphics.drawCircle(a,0,a);
circleA.graphics.endFill();
circleA.x=xa;
circleA.y=ya;
addChild(circleA);
circleB=new Sprite();
circleB.graphics.beginFill(0x7F1A1A);
circleB.graphics.drawCircle(c,0,c);
circleB.graphics.endFill();
circleB.x=xc;
circleB.y=ya;
addChild(circleB);
link4=new Sprite();
link4.graphics.beginFill(0x9F9F9F);
link4.graphics.moveTo(-t, -t);
link4.graphics.lineTo(d+t, -t);
link4.graphics.lineTo(d+t, t);
link4.graphics.lineTo(-t, t);
link4.graphics.lineTo(-t, -t);
addChild(link4);
link4.x=xd;
link4.y=yd;
link1=new Sprite();
link1.graphics.beginFill(0x46A099);
link1.graphics.moveTo(-t, -t);
link1.graphics.lineTo(a+t, -t);
link1.graphics.lineTo(a+t, t);
link1.graphics.lineTo(-t, t);
link1.graphics.lineTo(-t, -t);
link1.alpha=0.6;
addChild(link1);
link1.x=xa;
link1.y=ya;
link3=new Sprite();
link3.graphics.beginFill(0x46A099);
link3.graphics.moveTo(-t, -t);
link3.graphics.lineTo(c+t, -t);
link3.graphics.lineTo(c+t, t);
link3.graphics.lineTo(-t, t);
link3.graphics.lineTo(-t, -t);
link3.alpha=0.6;
addChild(link3);
link3.x=xc;
link3.y=yc;
pA=new Sprite();
pA.graphics.beginFill(0x630011);
pA.graphics.drawCircle(0,0,p);
pA.graphics.endFill();
pA.x=xa;
pA.y=ya;
addChild(pA);
pD=new Sprite();
pD.graphics.beginFill(0x630011);
pD.graphics.drawCircle(0,0,p);
pD.graphics.endFill();
pD.x=xc;
pD.y=ya;
addChild(pD);
link2=new Sprite();
link2.graphics.beginFill(0x48A046);
link2.graphics.moveTo(-t, -t);
link2.graphics.lineTo(b+t, -t);
link2.graphics.lineTo(b+t, t);
link2.graphics.lineTo(-t, t);
link2.graphics.lineTo(-t, -t);
link2.alpha=0.6;
addChild(link2);
pB=new Sprite();
pB.graphics.beginFill(0x630011);
pB.graphics.drawCircle(0,0,p);
pB.graphics.endFill();
addChild(pB);
pC=new Sprite();
pC.graphics.beginFill(0x630011);
pC.graphics.drawCircle(0,0,p);
pC.graphics.endFill();
addChild(pC);
trace1=CreateTF(10,400,300,50);
addEventListener(Event.ENTER_FRAME,onEnter);
}
private function onEnter(event:Event):void {
L=Math.sqrt(a*a+d*d-2*a*d*Math.cos(theta));
L2=a*a+d*d-2*a*d*Math.cos(theta);
al1=-Math.acos((-a*a+d*d+L2)/(2*d*L));
al2=-Math.acos((-b*b+c*c+L2)/(2*c*L));
link1.rotation=theta*180/Math.PI;
circleA.rotation=theta*180/Math.PI;
bx0=xa+a*Math.cos(theta);
by0=ya+a*Math.sin(theta);
if(by0>=yd){
al1*=-1;
}
bx1=xa+d+c*Math.cos(Math.PI-al1-al2);
by1=ya+c*Math.sin(Math.PI-al1-al2);
link2.x=bx0;
link2.y=by0;
link2.rotation=Math.atan2(by1-by0,bx1-bx0)*180/Math.PI;
link3.rotation=(Math.PI-al1-al2)*180/Math.PI;
circleB.rotation=(Math.PI-al1-al2)*180/Math.PI;
pB.x=bx0;
pB.y=by0;
pC.x=bx1;
pC.y=by1;
if(theta>=2*Math.PI){
theta=0;
}else{
theta-=Math.PI/180*3/2;
}
// trace1.text="L="+L+"\nal1="+al1+"\nal2="+al2;
}
private function CreateTF(a:Number,b:Number,c:Number,d:Number):TextField{
var tf:TextField=new TextField();
tf.x=a;
tf.y=b;
tf.width=c;
tf.height=d;
tf.background=true;
tf.border=true;
addChild(tf);
return tf
}
}
}