【问题标题】:Drawing a single line in different Color用不同的颜色画一条线
【发布时间】:2014-01-22 08:16:26
【问题描述】:

我正在尝试绘制一条可以分成不同段的线(在 C# winforms 中)。

然后,每个段将具有不同的颜色,并在其上显示一个名称。

我现在做的是:

 int startXPosition = 100;
 int startYPosition = 50;
 int numSegment = 5;
 int endXPosition = startXPosition;
 int endYPosition = this.Height / numSegment;

 Pen blackPen = new Pen(Color.Black, 5);
 e.Graphics.DrawLine(blackPen, new       Point(startXPosition, startYPosition), new      Point(endXPosition, endYPosition));

这将允许我使用黑色基于表单的高度/5(段数)绘制一条线。

我如何从这里继续,以便我能够绘制其余部分 (4),其中它将具有不同的颜色?

我怎样才能做到这一点,我不需要定义颜色并且代码可以自动为每个不同的段分配颜色?

【问题讨论】:

  • 将线条分成几部分,用不同的颜色绘制

标签: c# winforms colors drawing lines


【解决方案1】:

这可以通过线性代数轻松解决 (http://en.wikipedia.org/wiki/Linear_algebra)

你需要用向量形式来表示你的开始和结束

start + (end - start) * n,其中 n 为 [0..1]

由于这里没有使用向量,所以需要分别拆分 x 和 y

你的起始位置是start,等于n=0,你的最终位置(第五段)是end,等于n = 1。中间的每个位置都是@的一部分987654324@.

现在你需要画5条线,我们将使用上面的公式

  1. 从 n = 0 绘制到 n = 1/5
  2. 从 n = 1/5 绘制到 n = 2/5
  3. 从 n = 2/5 绘制到 n = 3/5
  4. ..
  5. 从 n = 4/5 绘制到 n = 5/5 (=1)

每个都有不同的颜色

【讨论】:

    【解决方案2】:

    如果段是相连的,那么您可以将整个图形表示为一个点数组。第一个点连接到第二个,第二个到第三个等等。

    如果你需要Color's,那么你可以将它作为一个单独的数组,或者作为 segment 类型的一部分(这样更有意义,参数越多你需要)。

    根据您的要求,在您的情况下,以某种方式定义 Color 的数组和 line 就足够了(可以使用 y=kx+b 表达式或起点/终点)。然后,在绘制时,您可以将线段分割(通过使用几何公式)并用自己的颜色绘制。

    举个例子,

    Color[] colors = new Color[] {Color.Red, Color.Black, Color.Green, Color.Blue, Color.Purple};
    var k = 1;
    var b = 0; // y = x, diagonal line
    
    for(int i = 0; i < colors.Lengh; i++)
    {
        // calculate line coords
        var y1 = this.Height / colors.Length * i;
        var x1 = (y1 - b) / k;  // x = (y - b) / k
        var y2 = this.Height / colors.Length * (i + 1);
        var x2 = (y2 - b) / k;
    
        using(var pen = new Pen(colors[i]))
            e.Graphics.DrawLine(pen, (int)x1, (int)y1, (int)x2, (int)y2);
    }
    

    注意,这不准确,因为您必须使用减少 1 个点的ClientRectangle(而不是形成Height)。否则你会在 non-client 区域绘制(这不是问题,但你不会看到任何绘制的东西)。

    获取kb 用于行的给定起点/终点是一项微不足道的任务,这里不发布。

    【讨论】:

      【解决方案3】:

      这应该对您有用,我尽我所能编写代码,请随时询问有关我所做的事情的问题。

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
      
          namespace WindowsFormsApplication1
          {
              public partial class Form1 : Form
              {
                  Bitmap buffer;  //used to draw the lines and txt on first then make the Forms     Background image = buffer
                  public Form1()
                  {
                      InitializeComponent();
                      //set the buffer to the same size as the form
                      buffer = new Bitmap(Width, Height);
      
                      //calls the method below
                      DrawLines(100,50,5,Graphics.FromImage(buffer));
      
                      //sets the background image to = buffer
                  BackgroundImage = buffer; 
      
              }
      
              public void DrawLines(int startX, int startY, int segments, Graphics g)
              {
                  // this needs to be looked at since it pushes the lines off the screen
                  //perhaps you need to indicate a total line length then use it here instead of this.Height
      
                  //int TotalLength=500;
                  //int segmentLength = TotalLength / segments; 
      
                  int segmentLength=this.Height/segments; 
      
                  //I have created a array containing 5 Colors, this way I can reference them from within the For Loop below
                  //You can use whichever colors you wish
      
                  Color[] Colors = new Color[] { Color.Black, Color.Red,Color.Orange,Color.Yellow,Color.Green };
      
                  //Loop through each of the segments
      
                  for (int y = 0; y < segments; y++)
                  {
                      //the using statements ensures your new p is disposed of properly when you are finished.  You could also use
                      // Pens.Red, or Pens.Black, which do not need to be disposed of instead of creating a new one
                      using (Pen p= new Pen(Colors[y]))
                          g.DrawLine(p,new Point(startX,startY+(y*segmentLength)),new Point(startX,startY+((y+1)*segmentLength)));
                      //same thing for Pens also applies to Brush...  Brushes.Red, Brushes.Black etc...
                      using (Brush b = new SolidBrush(Colors[y]))
                          g.DrawString(Colors[y].Name, Font, b, new Point(startX + 5, startY + (y * segmentLength) + (segmentLength / 2)));
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多