【问题标题】:How to get control points for a curve from a set of points which make a straight line?如何从一组形成直线的点中获取曲线的控制点?
【发布时间】:2013-11-25 16:19:42
【问题描述】:

我有一组 n 点组成一条线,但我想要一条曲线而不是一条线。为了在处理过程中做曲线,需要一些控制点,我想知道我该怎么做从 n 个点的集合中获取控制点。另外我会拖动曲线,因此我需要一直找到新的控制点?

【问题讨论】:

  • 曲线是否必须经过每个点,或者只是接近? “拖动曲线”是什么意思?
  • 曲线必须经过每个点?我在曲线上选择一个点,然后从该点拖动曲线。

标签: java graphics processing bezier


【解决方案1】:

好吧,您基本上可以只解析坐标数组并使用它们在 Processing 中创建一个 curveVertex() 形状,如下所示:

// create an array of coordinates in x, y, x, y... format
int[] points = {  
  34, 163,
  67, 345,
  474, 84,
  682, 234,
  495, 396,
  174, 379,
  275, 574
};

void setup() {
  size(800, 600);
  smooth();  
  noFill();
}

void draw() {
  background(255);

  draw_curve_from_points(points);  // draw the curve
  draw_handles_on_points(points, 6, 126);  // draw the handles

}

// a function to draw the curve
void draw_curve_from_points(int[] _points) { 
  noFill();
  stroke(0);
  strokeWeight(1);

  int len = _points.length;
  beginShape();
  curveVertex(_points[0], _points[1]);  // the first point is duplicated to be used as control point
  for (int i = 0; i < len; i +=2) {
    curveVertex(_points[i], _points[i+1]);
  }
  curveVertex(_points[len-2], _points[len-1]);  // idem for last point
  endShape();
}

// draw handles on the points
void draw_handles_on_points(int[] _points, float _size, int _gray) {
  int len = _points.length;
  pushStyle();
  noStroke();
  fill(_gray);
  for (int i = 0; i < len; i +=2) {
    ellipse(_points[i], _points[i+1], _size, _size);
  }
  popStyle();
}

只需添加一些鼠标位置识别和鼠标交互来拖动点。

【讨论】:

    【解决方案2】:

    最容易将每条曲线视为由两个点和两个向量表示(每个向量从一个端点到其对应的控制点)。曲线将与端点处的向量平行,因此如果想要避免两条曲线之间的某个点出现“扭结”,使得与该点相关的第一条曲线的向量与第二条曲线的向量相反。根据您的点序列可能表示的形状类型,有多种方法可以根据上述标准确定向量的方向和长度。一种简单的方法是计算与一个点相关联的向量应该平行于在它两侧的点之间绘制的一条线,并且与曲线相关联的向量的长度应该与该曲线的长度成正比(玩转用比例因子看看你喜欢什么)。请注意,向量越长,曲线将偏离端点之间的线越远,但如果向量太长,曲线可能会变得“循环”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多