[英]How do i translate this c# code into java
我正在尝试测试De-Casteljau细分代码。 但是我的例子在c#中,我想在java中测试它,因为我不知道c#。
特别是最后的回报给了我一些问题,因为我做对了。 我使用Vec2D而不是代表基本2d向量的点。 在这种情况下,我用Vec2D数组表示点。 我有类似“ getX”的方法来获取x部分,但是如果我仅在最后一行更改它,它将失败。 总而言之,我将“ Point”与Vec2D []交换,并将p1.X与p1.getX交换。
private void drawCasteljau(List<point> list)
{
Point tmp;
for (double t = 0; t & lt;= 1; t += 0.001) {
tmp = getCasteljauPoint(points.Count - 1, 0, t);
image.SetPixel(tmp.X, tmp.Y, color);
}
}
private Point getCasteljauPoint(int r, int i, double t)
{
if (r == 0) return points[i];
Point p1 = getCasteljauPoint(r - 1, i, t);
Point p2 = getCasteljauPoint(r - 1, i + 1, t);
return new Point((int)((1 - t) * p1.X + t * p2.X), (int)((1
- t) * p1.Y + t * p2.Y));
}
我的尝试:
public Vec2D[] getCasteljauPoint(int r, int i, double t) {
if(r == 0) return new Vec2D[i];
Vec2D[] p1 = getCasteljauPoint(r - 1, i, t);
Vec2D[] p2 = getCasteljauPoint(r - 1, i + 1, t);
return new Vec2D(((1/2) * p1.getX + (1/2) * p2.getX), ((1/2)
* p1.getY + (1/2) * p2.getY));
}
我觉得应该进行一些细微的改动才能使它继续运行,但是我被卡住了。 最后一行的错误消息说-无法解析getX或它不是字段-类型不匹配:无法从Vec2D转换为Vec2D []
您将p1
和p2
声明为Vec2D
数组,并且您的方法定义指定了Vec2D
数组返回类型。 但是,在您的方法内部,您将返回单个Vec2D
对象。
潜在的解决方案:
public class SomeJavaClassName
{
ArrayList<Vec2D> points = new ArrayList<String>();
// Other methods, properties, variables, etc.,
// some of which would populate points
public Vec2D getCasteljauPoint(int r, int i, double t) {
// points[] is declared outside just like in the C# code
if(r == 0) return points.get(i);
Vec2D p1 = getCasteljauPoint(r - 1, i, t);
Vec2D p2 = getCasteljauPoint(r - 1, i + 1, t);
return new Vec2D(((1/2) * p1.getX + (1/2) * p2.getX), ((1/2)
* p1.getY + (1/2) * p2.getY));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.