`
mapdigit
  • 浏览: 241884 次
文章分类
社区版块
存档分类
最新评论

Silverlight 引路蜂二维图形库示例:动画Beziers曲线

 
阅读更多

类Path为一条几何路径,可以通过直线,二次曲线,三次曲线来构成任意路径。Path提供了moveTo, lineTo, curveTo, curveToCubic, closePath方法来构成路径。

下面的例子动态显示一条Bezier曲线。

/**
* The animation thread.
*/
Thread _thread;
bool _drawn;
/**
 * The random number generator.
 */
readonly Random _random = new Random();
/**
 * The animated path
 */
readonly Path _path = new Path();
/**
 * Red brush used to fill the path.
 */
readonly SolidBrush _brush = new SolidBrush(Color.Red);
const int Numpts = 6;
readonly int[] _animpts = new int[Numpts * 2];
readonly int[] _deltas = new int[Numpts * 2];
 
private void Beziers()
{
 Reset(screenWidth, screenHeight);
 _thread = new Thread(RunBeziers);
 _thread.Start();
}
 
private void RunBeziers()
{
 
 Thread me = Thread.CurrentThread;
 
 if (!_drawn)
 {
  lock (this)
  {
   graphics2D.Clear(Color.White);
   graphics2D.Fill(_brush, _path);
   _drawn = true;
  }
 }
 while (_thread == me)
 {
  DrawDemo(screenWidth, screenHeight);
 }
}
 
/**
* Generates new points for the path.
*/
private void Animate(int[] pts, int[] deltas, int i, int limit)
{
 int newpt = pts[i] + deltas[i];
 if (newpt <= 0)
 {
  newpt = -newpt;
  deltas[i] = (_random.Next() & 0x00000003) + 2;
 }
 else if (newpt >= limit)
 {
  newpt = 2 * limit - newpt;
  deltas[i] = -((_random.Next() & 0x00000003) + 2);
 }
 pts[i] = newpt;
}
 
/**
* Resets the animation data.
*/
private void Reset(int w, int h)
{
 for (int i = 0; i < _animpts.Length; i += 2)
 {
  _animpts[i + 0] = (_random.Next() & 0x00000003) * w / 2;
  _animpts[i + 1] = (_random.Next() & 0x00000003) * h / 2;
  _deltas[i + 0] = (_random.Next() & 0x00000003) * 6 + 4;
  _deltas[i + 1] = (_random.Next() & 0x00000003) * 6 + 4;
  if (_animpts[i + 0] > w / 2)
  {
   _deltas[i + 0] = -_deltas[i + 0];
  }
  if (_animpts[i + 1] > h / 2)
  {
   _deltas[i + 1] = -_deltas[i + 1];
  }
 }
}
 
/**
 * Sets the points of the path and draws and fills the path.
 */
private void DrawDemo(int w, int h)
{
 for (int i = 0; i < _animpts.Length; i += 2)
 {
  Animate(_animpts, _deltas, i + 0, w);
  Animate(_animpts, _deltas, i + 1, h);
 }
 //Generates the new pata data.
 _path.Reset();
 int[] ctrlpts = _animpts;
 int len = ctrlpts.Length;
 int prevx = ctrlpts[len - 2];
 int prevy = ctrlpts[len - 1];
 int curx = ctrlpts[0];
 int cury = ctrlpts[1];
 int midx = (curx + prevx) / 2;
 int midy = (cury + prevy) / 2;
 _path.MoveTo(midx, midy);
 for (int i = 2; i <= ctrlpts.Length; i += 2)
 {
  int x1 = (curx + midx) / 2;
  int y1 = (cury + midy) / 2;
  prevx = curx;
  prevy = cury;
  if (i < ctrlpts.Length)
  {
   curx = ctrlpts[i + 0];
   cury = ctrlpts[i + 1];
  }
  else
  {
   curx = ctrlpts[0];
   cury = ctrlpts[1];
  }
  midx = (curx + prevx) / 2;
  midy = (cury + prevy) / 2;
  int x2 = (prevx + midx) / 2;
  int y2 = (prevy + midy) / 2;
  _path.CurveTo(x1, y1, x2, y2, midx, midy);
 }
 _path.ClosePath();
 // clear the clipRect area before production
 
 graphics2D.Clear(Color.White);
 graphics2D.Fill(_brush, _path);
 
 RefreshBitmap();
 
}

其中实时更新图像需要CompositionTarget.Rendering事件来处理。CompositionTarget.Rendering += CompositionTargetRendering;


private void CompositionTargetRendering(object sender, EventArgs e)
{
 bmp.Invalidate();
}


分享到:
评论

相关推荐

    beziers.py:Python Bezier操作库

    贝塞尔曲线提供了用于构造,操纵和绘制贝塞尔曲线和路径的各种类。 它主要是为字体设计软件设计的,它使您可以在路径上进行连接,拆分,偏移和执行许多其他操作。 这是一个示例会话: from beziers.point import ...

    PyPI 官网下载 | beziers-0.0.7-py2.py3-none-any.whl

    资源来自pypi官网。 资源全名:beziers-0.0.7-py2.py3-none-any.whl

    PitStop_Pro_10使用说明

    Enfocus PitStop Pro是Enfocus公司开发一款功能超强...圆形可以检查或直压((overprint),也能转成 Beziers曲线来修改;转换函数(Transfer function),半色调、CMYK或RGB都能加上去,还能从其他的PDF上搬过来新的物件。

    ELES模型Matlab代码-mixedfem_sample_code:变分表面建模的混合有限元。https://igl.ethz.ch/pr

    get_y_from_beziers.m layers_from_handle.m limitma_trima_file_from_handle.m_mass_trim_file_mass_trim_file_mass_from_handle.m m read_vertices_from_node_file.m triharm_factor_system.m triharm_solve_with_...

    HW2_curve_matlab_bezier_

    the GUI builds curve beziers based on the specified points. You can select the order of the curve and the coordinates of the points in 3d. The program shows parametric curves generatrices of points

Global site tag (gtag.js) - Google Analytics