三角形程序

本文介绍如何使用 C# 在 PictureBox 控件上绘制不同类型的三角形数组,包括左三角形、右三角形、倒左三角形、倒右三角形及特定排序方式的三角形,并给出具体实现代码。

写来写去,最后还是回到原点!来介绍点最基础的东西——三角形!

灵活运用三角形是可以解决很多问题的!今天主要介绍如果形成三角形数组:

首先新建一个项目,添加一个PictureBox控件用于绘制三角形。

1、左三角形

private void 左三角形button_Click(object sender, EventArgs e)

{

//i表示X方向,j表示Y方向

int i, j, number = 0;

for (i = 1; i < 10; i++)

{

for (j = i; j < 10; j++)

{

number++;

Graphics gp = pictureBox.CreateGraphics();

Brush drawBrush = new SolidBrush(Color.Blue);

Font font = new Font("Times New Roman", 10, FontStyle.Regular);

gp.DrawString(number.ToString(), font, drawBrush, i * 15, j * 15 );

}

}

}

其结果如下图所示:

图片

2、右三角形

private void 右三角形button_Click(object sender, EventArgs e)

{

//i表示X方向,j表示Y方向

int i, j, number = 0;

for (i = 1; i < 10; i++)

{

for (j = 10 - i; j < 10; j++)

{

number++;

Graphics gp = pictureBox.CreateGraphics();

Brush drawBrush = new SolidBrush(Color.Blue);

Font font = new Font("Times New Roman", 10, FontStyle.Regular);

gp.DrawString(number.ToString(), font, drawBrush, i * 15, j * 15 );

}

}

}

其结果为:

图片

3、倒左三角形

private void 倒左三角形button_Click(object sender, EventArgs e)

{

//i表示X方向,j表示Y方向

int i, j, number = 0;

for (i = 1; i < 10; i++)

{

for (j = 1; j < 10-i; j++)

{

number++;

Graphics gp = pictureBox.CreateGraphics();

Brush drawBrush = new SolidBrush(Color.Blue);

Font font = new Font("Times New Roman", 10, FontStyle.Regular);

gp.DrawString(number.ToString(), font, drawBrush, i * 15, j * 15);

}

}

}

其结果为:

图片

4、倒右三角形

private void 倒右三角形button_Click(object sender, EventArgs e)

{

//i表示X方向,j表示Y方向

int i, j, number = 0;

for (i = 1; i < 10; i++)

{

for (j = 0; j < i ; j++)

{

number++;

Graphics gp = pictureBox.CreateGraphics();

Brush drawBrush = new SolidBrush(Color.Blue);

Font font = new Font("Times New Roman", 10, FontStyle.Regular);

gp.DrawString(number.ToString(), font, drawBrush, i * 15, j * 15);

}

}

}

其结果为:

图片

5、按要求排序

其要求序号如下:

7

4 8

2 5 9

1 3 6 10

private void 按要求排序button_Click(object sender, EventArgs e)

{

//i表示X方向,j表示Y方向

int i, j, number = 0;

for (i = 1; i < 5; i++)

{

for (j = 5 - i; j < 5; j++)

{

number++;

Graphics gp = pictureBox.CreateGraphics();

Brush drawBrush = new SolidBrush(Color.Blue);

Font font = new Font("Times New Roman", 10, FontStyle.Regular);

gp.DrawString(number.ToString(), font, drawBrush, (i+j-5) * 15, j * 15);

}

}

}

其结果如下图所示:

图片

这个变换非常重要,在进行节点编号的时候采用该算法可以得到矩阵的最大带宽最优(即最小)。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值