C# 用相对位置和比例绘制三角形
我的老师问我如何绘制一个三角形,用于用户控制,其中的位置是相对的,而我一直在使用fillPolygon并获取窗口的实际大小。他给了我一个公式,但我不明白我需要如何应用它。我很感激你的帮助,我太迷路了。多谢各位 老师的公式: 这是我的错误代码,正如你所看到的,公式不适用:C# 用相对位置和比例绘制三角形,c#,C#,我的老师问我如何绘制一个三角形,用于用户控制,其中的位置是相对的,而我一直在使用fillPolygon并获取窗口的实际大小。他给了我一个公式,但我不明白我需要如何应用它。我很感激你的帮助,我太迷路了。多谢各位 老师的公式: 这是我的错误代码,正如你所看到的,公式不适用: using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using Syst
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MisControles
{
public partial class ControlVolumen: UserControl
{
int ancho;
int alto;
Color fondo;
Color color1;
Color color2;
Color color3;
public ControlVolumen()
{
InitializeComponent();
valor = 0;
fondo = Color.Empty;
color1 = Color.Green;
color2 = Color.Yellow;
color3 = Color.Red;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Brush b = new SolidBrush(fondo);
Point p0 = new Point(0, 0);
Point p1 = new Point(this.Width);
Point p2 = new Point(this.Height);
g.FillPolygon(fondo, new Point[] {p0,p1,p2});
}
}
}
我在下面给出了一个修改过的代码,希望它能有所帮助。 需要从用户输入中获取alto和ancho的百分比。 需要设置变量fondo的颜色,以便图形可见。 此外,还需要根据点结构的x、y坐标设置点
public部分类ControlVolumen:UserControl
{
double valor=0.65;//需要从用户输入中获取
彩色方多;
颜色1;
颜色2;
颜色3;
公共控制卷()
{
初始化组件();
//英勇=0;
fondo=颜色。蓝色;
color1=Color.Green;
color2=颜色。黄色;
color3=颜色。红色;
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
图形g=e.图形;
笔刷b=新的SolidBrush(fondo);
//根据提供的百分比计算宽度和高度
int ancho=(int)((双)this.Width*valor);
int alto=(int)((双倍)this.Height*valor);
p0点=新点(0,alto);
点p1=新点(ancho,0);
p2点=新点(安科、阿尔托);
g、 填充多边形(b,新点[]{p0,p1,p2});
}
}
创建并生成卷用户控件后,您可以修改卷bij并将Valor定义为属性
public partial class VolumeControl : UserControl
{
private int valor;
public int Valor
{
get { return valor; }
set { valor = value; this.Refresh(); }
}
public VolumeControl()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var graphics = e.Graphics;
var brush = new SolidBrush(Color.Blue);
//calculate width and height based on percentage provided
int ancho = this.Width * Valor/100;
int alto = this.Height * Valor/100;
// Graphic origin is upper-left corner.
Point p0 = new Point(0, this.Height);
Point p1 = new Point(ancho, this.Height);
Point p2 = new Point(ancho, this.Height-alto);
graphics.FillPolygon(brush, new Point[] { p0, p1, p2 });
}
}
现在将新的numericUpDownControl和VolumeControl添加到WindowsForm。
当numericUpDown更改时更新VolumeControl。
公式是如何定义百分比的。用percantege(也称为比例因子)相乘,然后除以B100F。