第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用二維數(shù)組來描述形狀來繪制形狀?

如何使用二維數(shù)組來描述形狀來繪制形狀?

C#
慕勒3428872 2022-12-24 10:53:03
我似乎無法弄清楚這段代碼有什么問題。我正在嘗試L使用二維數(shù)組繪制形狀。出于某種原因,代碼正在繪制一個(gè)大盒子而不是一個(gè)L形狀。我逐步完成了代碼,(x, y)位置很好。我不確定我做錯(cuò)了什么。private int[,] matrix = new int[3, 3] {    { 0, 1, 0 },    { 0, 1, 0 },    { 0, 1, 1 }};private void aCanvas_Paint(object sender, PaintEventArgs e) {    var gfx = e.Graphics;    var brush = new SolidBrush(Color.Tomato);    for (var x = 0; x <= matrix.GetLength(0) - 1; x++)         for (var y = 0; y <= matrix.GetLength(1) - 1; y++)           if (matrix[x, y] != 0) {               var rect = new Rectangle(x, y, 30, 30);               gfx.FillRectangle(brush, rect);           }}
查看完整描述

1 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

(30, 30)您當(dāng)前的代碼是在略有不同的位置(從(0, 1)到)繪制 4 個(gè)相同大小的矩形(2, 2),因?yàn)槟皇鞘褂脭?shù)組索引器作為L(zhǎng)ocation坐標(biāo)。


簡(jiǎn)單的解決方案,使用Rectangle.Size您現(xiàn)在顯示的值:


(x, y)將 的值增加Rectangle.Location由矩形Height和定義的偏移量,將矩陣中Width的當(dāng)前位置乘以這些偏移量:( 請(qǐng)注意,索引用于乘以高度偏移量;當(dāng)然與 相反)(x, y)

xy


private int[,] matrix = new int[3, 3] {

    { 0, 1, 0 },

    { 0, 1, 0 },

    { 0, 1, 1 }

};


Size rectSize = new Size(30, 30);

private void aCanvas_Paint(object sender, PaintEventArgs e)

{

    int xPosition = 0;

    int yPosition = 0;

    using (var brush = new SolidBrush(Color.Tomato)) {

        for (var x = 0; x <= matrix.GetLength(0) - 1; x++)

        for (var y = 0; y <= matrix.GetLength(1) - 1; y++)

        {

            xPosition = y * rectSize.Width;

            yPosition = x * rectSize.Height;


            if (matrix[x, y] != 0) {

                var rect = new Rectangle(new Point(xPosition, yPosition), rectSize);

                e.Graphics.FillRectangle(brush, rect);

            }

        }

    }

}

http://img1.sycdn.imooc.com//63a669ae0001b7a301160109.jpg

使用此矩陣:


private int[,] matrix = new int[3, 3] {

    { 0, 1, 0 },

    { 0, 1, 0 },

    { 1, 1, 1 }

};

你得到這個(gè):

http://img1.sycdn.imooc.com//63a669b9000136dd01100106.jpg

查看完整回答
反對(duì) 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 100 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)