日期:2014-05-18  浏览次数:20752 次

〓 算法求教 〓 三色光栅移动时位置变换的问题
static int iW, iInterval, i,k,n;

  private void button1_Click_1(object sender, EventArgs e)
  {
   
  iInterval = 3;
  i = iInterval;
  iW = 10;
  k = 0;
  n = 0;
  timer2.Interval = 100;
  this.timer2.Start();

  }

  private void timer2_Tick(object sender, EventArgs e)
  {
  if (i < iW)
  {
  if (k == 0)
  twinraster(Color.White, Color.Black,Color.Red, i, 0, 0, this.pictureBox1.Width, 0, iW);
  else if(k==1)
  twinraster(Color.Red, Color.White, Color.Black, i, 0, 0, this.pictureBox1.Width, 0, iW);
  else
  twinraster(Color.Black, Color.Red, Color.White, i, 0, 0, this.pictureBox1.Width, 0, iW);

  i += iInterval;
  }
  else
  {
  i = i - iW + 1;


  if (k == 0)
  {
  k = 1;
  n = 0;
  return;
  }
  if (k == 1)
  {
  if (n == 0)
  k = 2;
  else
  k = 0;
  return;
  }
  if (k == 2)
  {
  k = 1;
  n = 1;
  }
  //twinraster(Color.White, Color.Black, i, 0, 0, this.pictureBox1.Width, 0, iW);
   
  }
  }

private void twinraster(Color cColor1, Color cColor2,Color cColor3, int ist, int iX1, int iY1, int iX2, int iY2, int iWidth)//画三色光栅
  {
  Pen pen1 = new Pen(cColor1, iWidth);
  Pen pen2 = new Pen(cColor2, iWidth);
  Pen pen3 = new Pen(cColor1, ist);
  Pen pen4 = new Pen(cColor3, iWidth);

  Graphics myGraphics = this.pictureBox1.CreateGraphics();

  myGraphics.DrawLine(pen3, iX1, 0, iX2, 0);

  iY1 = ist;
  iY2 = ist;

  for (; iY1 <= this.pictureBox1.Height; iY1 += iWidth, iY2 += iWidth)
  {
  myGraphics.DrawLine(pen2, iX1, iY1, iX2, iY2);
  iY1 += iWidth;
  iY2 += iWidth;
  myGraphics.DrawLine(pen4, iX1, iY1, iX2, iY2);
  iY1 += iWidth;
  iY2 += iWidth;
  myGraphics.DrawLine(pen1, iX1, iY1, iX2, iY2);
  }
  myGraphics.DrawLine(pen2, iX1, iY1, iX2, iY2);
  }

我是这样写的,但是算法的效率肯定很差,而且在移动的时候,有一个地方会出错,颜色没控制得好,我已经弄了好久都没解决这个问题。主要是自己的算法已经一塌糊涂了,望知道的朋友帮忙看一下,谢谢了

------解决方案--------------------
参考如下代码
C# code
private void timer1_Tick(object sender, EventArgs e)
{
    yOffset = (yOffset + speed) % (lineHeight * lineColors.Length);
    pictureBox1.Refresh();
}

private void button1_Click(object sender, EventArgs e)
{
    timer2.Enabled = !timer2.Enabled;
}

/// <summary>
/// 线的高度
/// </summary>
private int lineHeight = 10;
/// <summary>