如何将一条线分成多条线段?
我正在尝试将一条线分成多个线段。因此,我在下面创建了一个函数来尝试获取两点之间的子点。虽然还不太有效,但也差不多了。理想情况下,我想像这样使用它:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试用以下内容替换您的计算:
我将值转换为双精度以使计算更准确。您可能需要考虑将值保留为双倍,因为这将使线段的准确性更高。
Try replacing your calculations with the following:
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.
您正在使用整数数学。也许您的 Vector 应该使用浮点数或双精度数?
You're using integer math. Perhaps your Vector should be using floats or doubles instead?
感谢克里斯的帮助。这是完整的 subPoint 函数处理/Java 代码,现在可供其他可能需要它的人使用。
Thanks to Kris for the help. Here's the complete subPoint function Processing/Java code now working for anyone else who might need it.
我喜欢这条线
但我怀疑你想保留它:-)(不知道是否还有更多错误)。
I like this line
But I doubt you want to keep it :-) (do not know if there are more errors).