三角形を使ってシリンダー:3Dの回転
/**
* Copyright _wonder ( http://wonderfl.net/user/_wonder )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hthP
*/
// forked from _wonder's 三角形を使って立方体:3Dの回転
// forked from _wonder's 三角形を使う:3Dの回転
// forked from _wonder's 3Dの回転 平面
// forked from _wonder's 3Dの回転 線だけ2
// forked from _wonder's 3Dの回転 線だけ
// forked from _wonder's 3Dの回転 + 線
// forked from _wonder's 3Dの回転
// forked from _wonder's 複数のボール跳ね返り3D
// forked from _wonder's 跳ね返り3D
// forked from _wonder's ボールを動かす3D
// forked from _wonder's 遠ざかるボール
// forked from _wonder's base
package {
import flash.display.Sprite;
import flash.events.Event;
public class Triangles2 extends Sprite {
private var points:Array;
private var triangles:Array;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var numFaces:uint = 40;
public function Triangles2() {
points = new Array();
triangles = new Array();
var index:uint = 0;
for( var i:uint = 0; i < numFaces; i++ ){
var angle:Number = Math.PI * 2 / numFaces * i;
var xpos:Number = Math.cos( angle ) * 200;
var ypos:Number = Math.sin( angle ) * 200;
points[index] = new Point3D( xpos, ypos, -100 );
points[index + 1] = new Point3D( xpos, ypos, 100 );
index += 2;
}
for( i = 0; i < points.length; i++ ){
points[i].setVanishingPoint( vpX, vpY );
points[i].setCenter(0, 0, 200 );
}
index = 0;
for( i = 0; i < numFaces - 1; i++ ){
triangles[index] = new Triangle( points[index], points[index+3], points[index+1], 0x6666cc );
triangles[index+1] = new Triangle( points[index], points[index+2], points[index+3], 0x6666cc );
index += 2;
}
triangles[index] = new Triangle( points[index], points[1], points[index+1], 0x6666cc);
triangles[index+1] = new Triangle( points[index], points[0], points[1], 0x6666cc);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
var angleY:Number = ( mouseX - vpX ) * 0.0002;
var angleX:Number = ( mouseY - vpY ) * 0.0002;
for( var i:uint = 0; i < points.length; i++ ){
var point:Point3D = points[i];
point.rotateX( angleX );
point.rotateY( angleY );
}
graphics.clear();
for( i = 0; i < triangles.length; i++ ){
triangles[i].draw( graphics );
}
}
}
}
import flash.display.Graphics;
class Triangle {
private var pointA:Point3D;
private var pointB:Point3D;
private var pointC:Point3D;
private var color:uint;
public function Triangle ( a:Point3D, b:Point3D, c:Point3D, color:uint ){
pointA = a;
pointB = b;
pointC = c;
this.color = color;
}
public function draw(g:Graphics):void {
g.beginFill( color, 0.5 );
g.lineStyle(0);
g.moveTo( pointA.screenX, pointA.screenY );
g.lineTo( pointB.screenX, pointB.screenY );
g.lineTo( pointC.screenX, pointC.screenY );
g.lineTo( pointA.screenX, pointA.screenY );
g.endFill();
}
}
class Point3D {
public var fl:Number = 250;
private var vpX:Number = 0;
private var vpY:Number = 0;
private var cX:Number = 0;
private var cY:Number = 0;
private var cZ:Number = 0;
public var x:Number = 0;
public var y:Number = 0;
public var z:Number = 0;
public function Point3D( x:Number=0, y:Number=0, z:Number=0 ){
this.x = x;
this.y = y;
this.z = z;
}
public function setVanishingPoint( vpX:Number, vpY:Number ):void {
this.vpX = vpX;
this.vpY = vpY;
}
public function setCenter( cX:Number, cY:Number, cZ:Number=0 ):void {
this.cX = cX;
this.cY = cY;
this.cZ = cZ;
}
public function get screenX():Number {
var scale:Number = fl / ( fl + z + cZ );
return vpX + cX + x * scale;
}
public function get screenY():Number {
var scale:Number = fl / ( fl + z + cZ );
return vpY + cY + y * scale;
}
public function rotateX( angleX:Number ):void {
var cosX:Number = Math.cos( angleX );
var sinX:Number = Math.sin( angleX );
var y1:Number = y * cosX - z * sinX;
var z1:Number = z * cosX + y * sinX;
y = y1;
z = z1;
}
public function rotateY( angleY:Number ):void {
var cosY:Number = Math.cos( angleY );
var sinY:Number = Math.sin( angleY );
var x1:Number = x * cosY - z * sinY;
var z1:Number = z * cosY + x * sinY;
x = x1;
z = z1;
}
public function rotateZ( angleZ:Number ):void {
var cosZ:Number = Math.cos( angleZ );
var sinZ:Number = Math.sin( angleZ );
var x1:Number = x * cosZ - y * sinZ;
var y1:Number = y * cosZ + x * sinZ;
x = x1;
y = y1;
}
}