如何将一条线分成多条线段?

发布于 2024-10-20 02:23:26 字数 691 浏览 1 评论 0原文

我正在尝试将一条线分成多个线段。因此,我在下面创建了一个函数来尝试获取两点之间的子点。虽然还不太有效,但也差不多了。理想情况下,我想像这样使用它:

subPoint(point1, point2, 5, 10);  // this would return the half way point
subPoint(point1, point2, 1, 10);  // this would return a point 1 tenth towrds point2

这是下面的粗略代码 - 非常欢迎任何提示或指示。

Vector subPoint(Vector startPoint, Vector endPoint, int segment, int totalSegments) {

  int division = (int)(totalSegments / segment);

  PVector divPoint = new PVector();

  int midX=(int)(startPoint.x+((endPoint.x-startPoint.x)/division));
  int midY=(int)(startPoint.y+((endPoint.y-startPoint.y)/division));

  divPoint.set(midX, midY, 0);

  return(divPoint);
}

I'm trying to divide a line into multiple segments. Because of that i've created a function below to try get sub points between two points. It's not quite working, but almost there. Ideally, i'd like to use it something like this:

subPoint(point1, point2, 5, 10);  // this would return the half way point
subPoint(point1, point2, 1, 10);  // this would return a point 1 tenth towrds point2

Here's the rough code below - any tips or pointers very welcome.

Vector subPoint(Vector startPoint, Vector endPoint, int segment, int totalSegments) {

  int division = (int)(totalSegments / segment);

  PVector divPoint = new PVector();

  int midX=(int)(startPoint.x+((endPoint.x-startPoint.x)/division));
  int midY=(int)(startPoint.y+((endPoint.y-startPoint.y)/division));

  divPoint.set(midX, midY, 0);

  return(divPoint);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信愁 2024-10-27 02:23:26

尝试用以下内容替换您的计算:

int midX = (startPoint.x + (int) ((double) (endPoint.x - startPoint.x) / (double) totalSegments) * segment);
int midY = (startPoint.y + (int) ((double) (endPoint.y - startPoint.y) / (double) totalSegments) * segment);

我将值转换为双精度以使计算更准确。您可能需要考虑将值保留为双倍,因为这将使线段的准确性更高。

Try replacing your calculations with the following:

int midX = (startPoint.x + (int) ((double) (endPoint.x - startPoint.x) / (double) totalSegments) * segment);
int midY = (startPoint.y + (int) ((double) (endPoint.y - startPoint.y) / (double) totalSegments) * segment);

I transformed the values to doubles in order to make the calculations a little more accurate. You may want to consider keeping the values as doubles as that will give you better accuracy on line segments.

红尘作伴 2024-10-27 02:23:26

您正在使用整数数学。也许您的 Vector 应该使用浮点数或双精度数?

You're using integer math. Perhaps your Vector should be using floats or doubles instead?

∞琼窗梦回ˉ 2024-10-27 02:23:26

感谢克里斯的帮助。这是完整的 subPoint 函数处理/Java 代码,现在可供其他可能需要它的人使用。

PVector subPoint(PVector startPoint, PVector endPoint, int segment, int totalSegments) {

  PVector p1 = new PVector();

  float midX = (startPoint.x + (int) ((double) (endPoint.x - startPoint.x) / (double) totalSegments) * segment);
  float midY = (startPoint.y + (int) ((double) (endPoint.y - startPoint.y) / (double) totalSegments) * segment);

  p1.set(midX, midY, 0);   

  return(p1);
}

Thanks to Kris for the help. Here's the complete subPoint function Processing/Java code now working for anyone else who might need it.

PVector subPoint(PVector startPoint, PVector endPoint, int segment, int totalSegments) {

  PVector p1 = new PVector();

  float midX = (startPoint.x + (int) ((double) (endPoint.x - startPoint.x) / (double) totalSegments) * segment);
  float midY = (startPoint.y + (int) ((double) (endPoint.y - startPoint.y) / (double) totalSegments) * segment);

  p1.set(midX, midY, 0);   

  return(p1);
}
隱形的亼 2024-10-27 02:23:26

我喜欢这条线

int 除法 = TotalSegments = 段;

但我怀疑你想保留它:-)(不知道是否还有更多错误)。

I like this line

int division = totalSegments = segment;

But I doubt you want to keep it :-) (do not know if there are more errors).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文