伪代码到java

me *_* me 0 java pseudocode code-conversion bresenham

我在过去一小时左右的时间环顾四周,但我找不到任何关于这个问题的帮助.我试图将这个伪代码转换为java并且无法弄清楚我做错了什么(它曾经打印过任何东西).

function line(x0, x1, y0, y1)
     boolean steep := abs(y1 - y0) > abs(x1 - x0)
     if steep then
         swap(x0, y0)
         swap(x1, y1)
     if x0 > x1 then
         swap(x0, x1)
         swap(y0, y1)
     int deltax := x1 - x0
     int deltay := abs(y1 - y0)
     real error := 0
     real deltaerr := deltay / deltax
     int ystep
     int y := y0
     if y0 < y1 then ystep := 1 else ystep := -1
     for x from x0 to x1
         if steep then plot(y,x) else plot(x,y)
         error := error + deltaerr
         if error ? 0.5 then
             y := y + ystep
             error := error - 1.0
Run Code Online (Sandbox Code Playgroud)

我的转换是:

public static void line(int x0,int x1,int y0,int y1) {
     boolean steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);
     if(steep) {
         swap(x0, y0);
         swap(x1, y1);
     }
     if (x0 > x1) {
         swap(x0, x1);
         swap(y0, y1);
     }
     int deltax = x1 - x0;
     int deltay = Math.abs(y1 - y0);
     float error = 0;
     float deltaerr = deltay / (float)deltax;
     int ystep;
     int y = y0;

     if(y0 < y1) ystep = 1;
     else ystep = -1;

     //for x from x0 to x1
     for(int x = x0; x < x1;x++)
         if (steep) plot(y,x);
         else plot(x,y);
         error = error + deltaerr;
         if (error >= 0.5f) {
             y = y + ystep;
             error = error - 1.0f;
         }
    }

//method plot
    private static void plot(int x, int y) {
        System.out.println(x+":"+y);
    }
//method swap
    private static void swap(int x0, int x1) {
        int copy = x0;           
        x0 = x1;
        x1 = copy;
    }
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Kep*_*pil 5

你不能用s swap()这样的方法int.由于它们是基元,因此它们通过值传递,并且更改方法内的局部变量将不会影响用作参数的变量.而是
直接在line()方法中进行交换.

第二件事是你的for循环看起来不对劲.根据您的缩进,您可能希望在整个块周围使用花括号.