简体   繁体   English

绘制具有相对位置和比例的三角形

[英]Drawing a triangle with relative positions and scale

My teacher is asking me how to draw a triangle for user control in which the positions are relative, and I'm getting stuck using fillPolygon and taking the real size of the window.我的老师问我如何为用户控件绘制一个三角形,其中的位置是相对的,我在使用 fillPolygon 并获取 window 的实际大小时遇到了问题。 He gave me a formula but I don't understand how I need to apply it.他给了我一个公式,但我不明白我需要如何应用它。 I will appreciate some help, I'm so lost.我会很感激一些帮助,我很迷茫。 Thank you谢谢

The teacher's formula: Formula老师的公式:公式

Heres my wrong code, as you can see the formula is not applied:这是我的错误代码,您可以看到公式未应用:

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});
        }
    }
}

After you created and build the Volume UserControl you can modify the volume bij define the Valor as a property.创建并构建 Volume UserControl 后,您可以修改 volume 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 });
    }
}

Now add a new numericUpDownControl and the VolumeControl to a WindowsForm.现在将新的 numericUpDownControl 和 VolumeControl 添加到 WindowsForm。 在此处输入图像描述

Update VolumeControl when numericUpDown changes.当 numericUpDown 改变时更新 VolumeControl。 在此处输入图像描述

I have given a modified code below and hope it helps.我在下面给出了修改后的代码,希望对您有所帮助。 Need to get the percentages for alto and ancho from the user input.需要从用户输入中获取 alto 和 ancho 的百分比。 The color for the variable fondo need to be set so that figure is visible.需要设置变量 fondo 的颜色,以便该图形可见。 Also the points need to be set based on the x,y coordinates for the Point struct.此外,需要根据 Point 结构的 x,y 坐标设置点。

 public partial class ControlVolumen : UserControl
    {
        double valor = 0.65;  // This need to be fetched from User input
        Color fondo;
        Color color1;
        Color color2;
        Color color3;
        public ControlVolumen()
        {
            InitializeComponent();
            // valor = 0;
            fondo = Color.Blue;
            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);

            //calculate width and height based on percentage provided
            int ancho = (int)((double)this.Width * valor);
            int alto = (int)((double)this.Height * valor);

            Point p0 = new Point(0, alto);
            Point p1 = new Point(ancho, 0);
            Point p2 = new Point(ancho, alto);
            g.FillPolygon(b, new Point[] { p0, p1, p2 });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM