C#GDI的问题,请问大家这种的线性符号,C#应该怎么画呢?谢谢大家了!
1个回答
展开全部
将当成一截,写好画一截的函数,然后完整的波形就是不断的重复就好了
下面这段代码直接调用DrawWave.DrawSquareWave(10,10);就可以画出下面这样一段波了
DrawOneWave就是画一截的函数,一截由一横、一竖、一横、一竖组成
class DrawWave
{
/// <summary>
/// 画方波
/// </summary>
/// <param name="startPosition">起始位置</param>
/// <param name="sideLength">边长</param>
/// <param name="number">波长数</param>
public static Image DrawSquareWave(int sideLength, int number)
{
if (number <= 0)
{
number = 1;
}
Image image = new Bitmap(sideLength * number * 2 + 10, sideLength + 10);
Graphics graphics = Graphics.FromImage(image);
Point startPosition = new Point(5, sideLength + 5);
for (int i = 0; i < number; i++)
{
DrawOneWave(graphics, startPosition, sideLength);
startPosition.Offset(sideLength * 2, 0);
}
return image;
}
private static void DrawOneWave(Graphics g, Point startPosition, int sideLength)
{
Point point1, point2;
Pen pen = new Pen(Color.Black, 1);
{
point1 = startPosition;
point2 = new Point(point1.X + sideLength, point1.Y);
g.DrawLine(pen, point1, point2);
}
{
point1 = point2;
point2 = new Point(point1.X, point1.Y - sideLength);
g.DrawLine(pen, point1, point2);
}
{
point1 = point2;
point2 = new Point(point1.X + sideLength, point1.Y);
g.DrawLine(pen, point1, point2);
}
{
point1 = point2;
point2 = new Point(point1.X, point1.Y + sideLength);
g.DrawLine(pen, point1, point2);
}
}
}
追问
恩恩,先谢谢你!可是你这样无法实现当我在窗体上随便点击几个点的时候,如果这条线是折现的话,在再画成这样的形状(长城),因为折线的拐点处会出线断裂或者很不规则的地方。
追答
哦,你要画非水平垂直方向上的方波的话,对各个顶点的位置计算就要复杂些了,得算出斜率然后分别算x、y
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询