使用p5.js制作画布

时间:2018-09-08 16:00:55

标签: javascript p5.js

enter image description here我进行了一个练习,在该练习中,我必须使用p5.js创建一个矩形画布,但是该画布将由小矩形组成,所以我做到了,但是练习中还有1点。我怎样才能用2种不同的颜色得到这些小矩形,但是使用矩阵,这些颜色中的50%必须是绿色,而另一种是红色。 这是代码。

        var matrix = [

        ];

        var ab = 36;

        for (var y = 0; y < ab; y++) {
        matrix.push([])
        for (var x = 0; x < 36; x++) {
        matrix[y][x] = Math.floor(Math.random() * 2)
        }
        }
        console.log(matrix)
        var side = 16;
        function setup() {

        createCanvas(matrix[0].length * side, matrix.length * side);
        background('#acacac');
        frameRate()

        }
        function draw() {
        for (var y = 0; y < matrix.length; y++) {
        for (var x = 0; x < matrix[y].length; x++) {

        if (matrix[y][x] == 0) {
            fill(0, 255, 0)
            rect(y * side, x * side, side, side)
        }
        else if (matrix[y][x] == 1) {
            fill("red")
            rect(y * side, x * side, side, side)
        }



        function Shuffle (arguments) {
for(var k = 0; k < arguments.length; k++){
  var i = arguments[k].length;
  if ( i == 0 ) return false;
  else{
      while ( --i ) {
         var j = Math.floor( Math.random() * ( i + 1 ) );
         var tempi = arguments[k][i];
         var tempj = arguments[k][j];
         arguments[k][i] = tempj;
         arguments[k][j] = tempi;
         }
         return arguments;
         }
         }
         }

2 个答案:

答案 0 :(得分:0)

因此,如评论中所讨论的那样,该问题减少为用一种颜色准确地填充矩阵的一半,而另一种颜色恰好填充另一矩阵。

您的矩阵是二维的,我将在一个维度上给出一个解,这应该很容易扩展为二维-

private SqlConnection CreateTempConnectionIfNeeded()
{
    return _sqlConnection == null ? new SqlConnection(_connectionString) : null;
}

public int BulkInsert<T>(IDataReader dataReader, Dictionary<string, string> columnMappings = null, int timeoutInSeconds = 120)
{
    using (var tempConnection = CreateTempConnectionIfNeeded())
    {
        return BulkInsert<T>(dataReader, tempConnection ?? _sqlConnection, columnMappings, timeoutInSeconds);
    }
}

还有另一种方法

用1填充数组的一半,用0填充一半,然后对数组进行随机排序

您可以非常轻松地使用Google算法进行混洗,

我可以找到一个伪代码

  func connect() {
        if vpnInstance.vpnManager.connection.status != .connected {
            connection.text = "connecting"
            self.rotateView(connectImageView, clockWise: true)
            vpnInstance.connectVPN()
            if(vpnInstance.vpnManager.connection.status == .connected)
            {
                connection.text = locations[finalLocation]
                print("connected")
                self.connectImageView.layer.removeAllAnimations()
                buttonTextEdit.setTitle("DISCONNECT", for: UIControlState.normal)
                connection.text = locations[finalLocation]
                print("connection successfull")
                let y = UserDefaults.standard.object(forKey: "number") as? Int
                UserDefaults.standard.set(y!+1, forKey: "number")


            }

        }

        else if(vpnInstance.vpnManager.connection.status != .connected) {
            connection.text = "Disconnnecting"
            self.rotateView(connectImageView, clockWise: true)
            vpnInstance.connectVPN()
            connectStatus = false
            connection.text = "Not Connected"
            buttonTextEdit.setTitle("CONNECT", for: UIControlState.normal)
            self.connectImageView.layer.removeAllAnimations()
        }


    }

来源:https://www.geeksforgeeks.org/shuffle-a-given-array/

答案 1 :(得分:0)

这是我的问题
                var matrix = [

            ];

            var ab = 36;

            for (var y = 0; y < ab; y++) {
                matrix.push([])
                for(var x = 0 ; x<ab;x++){
                    matrix[y][x] = Math.floor(Math.random()*1)
                }
                for(var x = 0 ; x<ab/2;x++){
                    matrix[y][x] = 1
                }
            }




            var count = 0;
            var arr = [];
            for( var i = 0 ;i < ab;i++){
            arr[i] = 0;
            }
            while(true) {
            var i = Math.floor(Random(ab));
            if(arr[i] !==1) {
                arr[i] = 1;
                count++;
            }  
            if(count === ab/2) break; // assume ab is even
            }

            console.log(arr)
            var side = 16;
            function setup() {

                createCanvas(arr[0].length * side, arr.length * side);
                background('#acacac');
                frameRate()

            }
            function draw() {
                for (var y = 0; y < arr.length; y++) {
                    for (var x = 0; x < arr[y].length; x++) {

                        if (matrix[y][x] == 0) {
                            fill(0, 255, 0)
                            rect(y * side, x * side, side, side)
                        }
                        else if (matrix[y][x] == 1) {
                            fill("red")
                            rect(y * side, x * side, side, side)
                        }
                        else if (matrix[y][x] == 2) {
                            fill(255, 255, 0)
                            rect(y * side, x * side, side, side)
                        }
                        else if (matrix[y][x] == 3) {
                            fill(255, 0, 0)
                            rect(y * side, x * side, side, side)
                        }
                    }
                }
            }