根据度/弧度绘制一边偏斜的矩形

时间:2012-06-07 02:59:14

标签: actionscript-3 flash math actionscript geometry

我试图取一个矩形的一边,并根据角度/角度倾斜一边。

enter image description here

3 个答案:

答案 0 :(得分:4)

我为你准备了一些代码 有问题请问。

import flash.geom.Matrix;

var temp_matrix = new Matrix();

var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x000000);
square.graphics.drawRect(0,0,200,100);
square.graphics.endFill();

var angle:Number = -10; // the angle of degrees
temp_matrix.b = Math.PI * 2 * angle / 360;// y skew
//temp_matrix.c = Math.PI * 2 * angle / 360;// x skew

var sourceMatrix:Matrix = square.transform.matrix;// get existing matrix
sourceMatrix.concat(temp_matrix); // apply skew to existing matrix
square.transform.matrix = temp_matrix;// assign the new skew

square.x = 100
square.y = 100

[第二轮]

var trapezium:Sprite = new Sprite();
addChild(trapezium);
trapezium.x = 100;
trapezium.y = 100;

var dir:Boolean = true;
var side:Boolean = true;
var angle:Number = 0; // the angle of degrees
var w:Number = 300;
var h:Number = 80;


var timer:Timer = new Timer(16);
timer.addEventListener( TimerEvent.TIMER, tick );
timer.start();

function tick(e:TimerEvent):void{
    var radians:Number = Math.PI/180*angle;
    trapezium.graphics.clear();
    trapezium.graphics.beginFill(0x000000)
    if( side){
        // long side is right side
        trapezium.graphics.lineTo(w,0);
        trapezium.graphics.lineTo(w,radians*w+h);
        trapezium.graphics.lineTo(0,h);
        trapezium.graphics.lineTo(0,0);
    }else{
        trapezium.graphics.lineTo(w,0);
        trapezium.graphics.lineTo(w,h);
        trapezium.graphics.lineTo(0,radians*w+h);
        trapezium.graphics.lineTo(0,0);
    }
    trapezium.graphics.endFill();
    if(angle>=10){
        dir = false;
    }
    if(angle<=0){
        dir = true;
    }
    if(dir){
        angle = angle+.2;
    }else{
        angle = angle-.2;
    }
    if( Math.floor(angle*10) <= 0 ){
        side = !side;
    }
}

答案 1 :(得分:3)

取角度的正切并乘以矩形的宽度,得到底轴的delta y,这样就可以了

[x1,y1]作为矩形的原点(永不改变)

[x1 + length,y1 + deltaY]作为右下角

答案 2 :(得分:1)

不知道AS,但在编辑之后,这看起来像带顶点的填充多边形:

P0 =(X0, Y0)
P1 = (X1, Y0)
if Angle >= 0 then
  P2 = (X1, Y1)
  P3 = (X0, Y1 + (X1-X0) * Tan(Angle))
else
  P2 = (X1, Y1 - (X1-X0) * Tan(Angle))
  P3 = (X0, Y1)