C# 用不同的颜色画一条线
我试图画一条可以分成不同部分的线(在C#winforms中) 然后,每个片段将具有不同的颜色,并在其上显示名称 我现在做的是:C# 用不同的颜色画一条线,c#,winforms,colors,drawing,lines,C#,Winforms,Colors,Drawing,Lines,我试图画一条可以分成不同部分的线(在C#winforms中) 然后,每个片段将具有不同的颜色,并在其上显示名称 我现在做的是: int startXPosition = 100; int startYPosition = 50; int numSegment = 5; int endXPosition = startXPosition; int endYPosition = this.Height / numSegment; Pen blackPen = new Pen(Color
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)的其他部分,其中它将是不同的颜色
我如何做到不需要定义颜色,代码可以自动为每个不同的段分配颜色?这可以通过线性代数() 你需要用向量的形式来表示你的开始和结束 开始+(结束-开始)*n,其中n为[0..1] 因为这里不使用向量,所以需要分别拆分x和y 您的起始位置是
start
,等于n=0,您的最终位置(第五段)是end
,等于n=1。中间的每个位置都是n
的一小部分
现在你需要画5条线,我们将使用上面的公式
每个图形都有不同的颜色如果连接了线段,则可以将整个图形表示为点阵列。第一点连接到第二点,第二点连接到第三点,等等 如果需要使用
颜色
,则可以将其作为一个单独的数组,或作为段类型的一部分(这样做更有意义,需要的参数越多)
根据您的要求,在您的情况下,以某种方式定义Color
的数组和线条就足够了(可以使用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);
}
Color[]colors=新颜色[]{Color.Red,Color.Black,Color.Green,Color.Blue,Color.Purple};
var k=1;
变量b=0;//y=x,对角线
对于(int i=0;i
注意,这是不准确的,因为您必须使用ClientRectangle
减少1点(而不是形式高度
)。否则,您将在非客户机区域上绘制(这不是问题,但您将看不到绘制的任何内容)
对于给定的行的起始点/结束点,获取
k
和b
是一项琐碎的任务,而不是将其张贴在这里。这应该可以为您做些什么,我尽我所能对代码进行评论,请随意询问我所做的事情
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)));
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
位图缓冲区;//用于先绘制线条和txt,然后使窗体背景图像=缓冲区
公共表格1()
{
初始化组件();
//将缓冲区设置为与窗体相同的大小
缓冲区=新位图(宽度、高度);
//调用下面的方法
绘图线(100,50,5,图形。从图像(缓冲区));
//将背景图像设置为=缓冲区
背景图像=缓冲区;
}
公共空白绘制线(int-startX、int-startY、int-segments、图形g)
{
//这需要仔细观察,因为它会将线条从屏幕上移开
//也许你需要指出一条线的总长度,然后在这里用它来代替这个。高度
//int总长度=500;
//int段长度=总长度/段;
int segmentLength=this.Height/segments;
//我已经创建了一个包含5种颜色的数组,这样我就可以从下面的For循环中引用它们
//你可以用任何你想要的颜色
颜色[]颜色=新颜色[]{Color.Black,Color.Red,Color.橙色,Color.Yellow,Color.Green};
//循环通过每个段
对于(int y=0;y
将线拆分为多个部分,并使用不同的颜色绘制