使用坐标旋转多边形

时间:2014-06-19 10:54:10

标签: javascript rotation polygon

我正在尝试旋转多边形(3边)(我有边缘的坐标)。 我使用cos和sin来获得每个点的新坐标,但它并不真正有用,我不明白为什么。

http://jsfiddle.net/sh3rlock/gqK23/

(m是多边形的中点,kr是圆的半径)

newX = x +((x - m [0])* c - (y - m [1])* s)* kr;

newY = y +((y - m [1])* c +(x - m [0])* s)* kr;

function drawPlayer(x, y, r, tcolor) {
if (typeof c === "undefined") color = 'rgba(0,0,200,1)';
//canvas.width = canvas.width;

var radians = r * (Math.PI / 180);

var m = getMiddleOfPg([
    [x, y + 5],
    [x, y - 5],
    [x + 15, y]
]); // Mitte des Polygons
var a = [x, y + 5];
var b = [x, y - 5];
var c = [x + 15, y];



drawPolygonFromArray([a, b, c], ctx, 'rgba(0,200,0,1)');

var speed = 1;
var right = Math.cos(radians) * 1;
var up = -(Math.sin(radians) * 1);
var x2 = x;
var y2 = y;

var c = Math.cos(radians);
var s = Math.sin(radians);

x = x2;
y = y2 + 5;

var kr = Math.sqrt((x - m[0]) * (x - m[0]) + (y - m[1]) * (y - m[1]));

var ax = x + ((x - m[0]) * c - (y - m[1]) * s) * kr;
var ay = y + ((y - m[1]) * c + (x - m[0]) * s) * kr;

x = x2;
y = y2 - 5;

kr = Math.sqrt((x - m[0]) * (x - m[0]) + (y - m[1]) * (y - m[1]));
var bx = x + ((x - m[0]) * c - (y - m[1]) * s) * kr;
var by = y + ((y - m[1]) * c + (x - m[0]) * s) * kr;

x = x2 + 15;
y = y2;

kr = Math.sqrt((x - m[0]) * (x - m[0]) + (y - m[1]) * (y - m[1]));
var cx = x + ((x - m[0]) * c - (y - m[1]) * s) * kr;
var cy = y + ((y - m[1]) * c + (x - m[0]) * s) * kr;
x = x2;
y = y2;


var a = [ax, ay];
var b = [bx, by];
var c = [cx, cy];

drawPolygonFromArray([a, b, c], ctx, tcolor);

drawMiddleOfPg([a, b, c], 'rgba(255,0,0,1)');
//drawMiddleOfPg([[x,y],[x,y-5],[x+15,y]], 'rgba(0,255,0,1)');




//alert(right + " " + up);


cr += 20;
if (cr > 255) {
    cb += 20;
    cr = 0;
}
if (cb > 255) {
    cg += 20;
    cb = 0;
    cr = 0;
}
t = setTimeout(function () {
    drawPlayer(x, y, r + 1, 'rgba(' + cr + ',' + cb + ',' + cg + ',1)')
}, 20);
return;

0 个答案:

没有答案