numpy数组到三角形(矩阵)

时间:2016-11-04 01:50:53

标签: python pandas numpy scipy

我有一个数组,我想拆分矩阵(10x10)。 经过几次尝试,我做到了。

<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style>
#canvas {
    position: absolute;
    background-color: #000;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}
</style>
<head>
<body>
        <canvas id="canvas" width="600" height="500"></canvas>
<script>
var canvas, ctx, mouseX=0, mouseY=0;
 canvas = $("#canvas")[0];
 ctx = canvas.getContext("2d");

 mouseX = 0;
 mouseY = 0;

class Paddle
{
    constructor(x, y, w, h, color)
    {
        this.x = canvas.width / 2 - 100 / 2;
        this.y = canvas.height - 60;
        this.w = 100;
        this.h = 10;
        this.color = "#fff";
    }

    draw()
    {
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x, this.y, this.w, this.h); 
    }
}

class Ball {
    constructor (x, y, r, speedX, speedY, color) {
        this.x = canvas.width / 2 - 10 / 2;
        this.y = canvas.height / 2 - 10 / 2;
        this.r = 10;
        this.speedX = 3;
        this.speedY = 3;
        this.color = "#fff";
    }

    draw() {
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
        ctx.fill();
    }

    animate() {
        this.x += this.speedX;
        this.y += this.speedY;
    }

    collision() {
        if(this.x >= canvas.width) {
            this.speedX *= -1;
        }
        if(this.x <= 0) {
            this.speedX *= -1;
        }
        if(this.y >= canvas.height) {
            this.reset();
        }
        if(this.y <= 0) {
            this.speedY *= -1;
        }

        let paddleTop = paddle.y;
        let paddleBottom = paddleTop + paddle.h;
        let paddleLeft = paddle.x;
        let paddleRight = paddle.x + paddle.w;

        if(ball.x >= paddleLeft &&
           ball.x <= paddleRight &&
           ball.y >= paddleTop &&
           ball.y <= paddleBottom) {
            ball.speedY *= -1;
            ballControl();
        }
    }

    reset() {
        this.speedX = 3;
        this.speedY = 3;

        this.x = canvas.width / 2 - 10 / 2;
        this.y = canvas.height / 2 - 10 / 2;
    }
}

class Brick {
    constructor(x, y, w, h, col, row, gap, color) {
        this.x = 0;
        this.y = 0;
        this.w = 100;
        this.h = 50;
        this.col = 5; //# of brick columns
        this.row = 2; //# of brick rows
        this.gap = 2; //gap betweeb each brick
        this.color = "#0000ff";
    }

    draw() {
        for(let brickRow = 0; brickRow < this.row; brickRow++) 
        {
            for(let brickCol = 0; brickCol < this.col; brickCol++) 
            {
                ctx.fillStyle = this.color;
                ctx.fillRect( (this.w+this.gap) * brickCol, (this.h+this.gap) * brickRow, this.w, this.h );
            }
        }
    }
}

let paddle = new Paddle(this.x, this.y, this.w, this.h, this.color);
let ball = new Ball(this.x, this.y, this.r, this.speedX, this.speedY, this.color);
let brick = new Brick(this.x, this.y, this.w, this.h, this.col, this.row, this.gap, this.color);

// START

$(document).ready(() => {
    let fps = 120;
    setInterval(init, 1000 / fps);

    $(canvas).bind("mousemove", paddleControl);
})

// INIT
let init = () => {
    draw();
    animate();
    collision();
}

// DRAW
let draw = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    paddle.draw();
    ball.draw();
    brick.draw();
}

// ANIMATE
let animate = () => {
    ball.animate();
}

// COLLISION
let collision = () => {
    ball.collision();
}

// BALL CONTROL
let ballControl = () => {
    let paddleCenter = paddle.x + paddle.w / 2;
    let ballDistFromPaddleCenter = ball.x - paddleCenter;

    ball.speedX = ballDistFromPaddleCenter * 0.15;
}

// PADDLE CONTROL
let paddleControl = (e) => {
    let rect = canvas.getBoundingClientRect();
    let root = document.documentElement;

    mouseX = e.pageX - rect.left - root.scrollLeft;
    mouseY = e.pageY - rect.top - root.scrollTop;

    paddle.x = mouseX;
}
</script>
</body>
</html>

和我希望的结果:

a=np.arange(1,56)
tri = np.zeros((10, 10))
tri[np.triu_indices_from(tri,0)]=a
tri

array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
       [  0.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.],
       [  0.,   0.,  20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.],
       [  0.,   0.,   0.,  28.,  29.,  30.,  31.,  32.,  33.,  34.],
       [  0.,   0.,   0.,   0.,  35.,  36.,  37.,  38.,  39.,  40.],
       [  0.,   0.,   0.,   0.,   0.,  41.,  42.,  43.,  44.,  45.],
       [  0.,   0.,   0.,   0.,   0.,   0.,  46.,  47.,  48.,  49.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,  50.,  51.,  52.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  53.,  54.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  55.]])

我做了几个关系,比如try.T,np.triu,np.tril ......等。

感谢

1 个答案:

答案 0 :(得分:2)

如果这是你的意思,你可以使用rot90()方法将上三角索引矩阵旋转90度,然后将其用作索引来填充数组中的值:

import numpy as np
a=np.arange(1,56)
tri = np.zeros((10, 10))
tri[np.rot90(np.triu(np.ones((10,10), dtype=bool)))] = a

tri
# array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
#        [ 11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,   0.],
#        [ 20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.,   0.,   0.],
#        [ 28.,  29.,  30.,  31.,  32.,  33.,  34.,   0.,   0.,   0.],
#        [ 35.,  36.,  37.,  38.,  39.,  40.,   0.,   0.,   0.,   0.],
#        [ 41.,  42.,  43.,  44.,  45.,   0.,   0.,   0.,   0.,   0.],
#        [ 46.,  47.,  48.,  49.,   0.,   0.,   0.,   0.,   0.,   0.],
#        [ 50.,  51.,  52.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
#        [ 53.,  54.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
#        [ 55.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]])