亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何將此 c# 代碼翻譯成 java

我如何將此 c# 代碼翻譯成 java

天涯盡頭無女友 2023-03-17 14:19:17
我正在嘗試測試 De-Casteljau-subdivision 代碼。但是我的示例是在 c# 中,我想在 java 中測試它,因為我不知道 c#。尤其是最后一次回歸給我帶來了問題,因為我做不對。我使用 Vec2D 而不是代表基本二維向量的點。在這種情況下,我用 Vec2D 數組表示點。我有像“getX”這樣的方法來獲取 x 部分,但如果我只是在最后一行更改它,它就會失敗??偠灾?,我用 Vec2D[] 交換了“Point”,用 p1.getX 交換了 p1.X。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[]
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

您正在將p1and聲明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));

    }

}


查看完整回答
反對 回復 2023-03-17
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號