簡體   English   中英

我如何將此C#代碼轉換為Java

[英]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 []

您將p1p2聲明為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM