如何从形成直线的一组点中获取曲线的控制点? [英] How to get control points for a curve from a set of points which make a straight line?

查看:79
本文介绍了如何从形成直线的一组点中获取曲线的控制点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组n点组成一条线,但我想用一条曲线代替一条线.要在加工过程中做曲线,需要一些控制点,我想知道如何从n个点的集合中获取控制点.另外,我还会拖拽曲线,因此我一直都想找到新的控制点?

I have a set of n points which make a line, but I would like to have a curve instead of a line.For doing curves in processing, there are control points which are needed, I would like to know how can I get control points from a set on n points. Also I would be dragging curves, hence I would need to find new control points all the time I presume?

推荐答案

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

Well, you can basically just parse the array of coordinates and use them to create a curveVertex() shape in Processing like this:

// 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();
}

只需添加一些鼠标位置识别和鼠标交互功能即可拖动这些点.

Just add some mouse position recognition and mouse interaction to drag the points around.

这篇关于如何从形成直线的一组点中获取曲线的控制点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆