게임만드시는 분들은 직선 그릴때 어떤 알고리즘을 사용하시는지 궁금하군요.




public class Bresenham {
    public int x,y; 
   
    private int startX,startY;
    private int temp;
   
    private int endX, endY;
    private int lengthX,lengthY;
       
    private int abs(int n){
        if(n<0)n=-n;
        return n;
    }

    public void setStartPoint(int startX,int startY){
        this.x = this.startX = startX;
        this.y = this.startY = startY;
        setLength();
    }
       
    public void setEndPoint(int endX,int endY){
        this.endX = endX;
        this.endY = endY;
        this.startX = this.x;
        this.startY = this.y;
        setLength();
    }
   
    private void setLength(){
        lengthX = endX - startX;
        lengthY = endY - startY;
    }

    public void move(int speed){
        int i;
        if(lengthX>=0 && lengthY>=0){
            for (i = 0; i < speed; i++) {
                if(abs(lengthX)>abs(lengthY)){
                    x++;
                    temp+=lengthY;
                    if(temp>lengthX/2){
                        y++;
                        temp-=lengthX;
                    }
                }else{
                    y++;
                    temp+=lengthX;
                    if(temp>lengthY/2){
                        x++;
                        temp-=lengthY;
                    }
                }
            }
        }else if(lengthX<=0 && lengthY<=0){
            for (i = 0; i < speed; i++) {
                if(abs(lengthX)>abs(lengthY)){
                    x--;
                    temp-=lengthY;
                    if(temp>lengthX/2){
                        y--;
                        temp+=lengthX;
                    }
                }else{
                    y--;
                    temp-=lengthX;
                    if(temp>lengthY/2){
                        x--;
                        temp+=lengthY;
                    }
                }
            }
        }else if(lengthX>=0 && lengthY<=0){
            for (i = 0; i < speed; i++) {
                if(abs(lengthX)>abs(lengthY)){
                    x++;
                    temp-=lengthY;
                    if(temp>lengthX/2){
                        y--;
                        temp-=lengthX;
                    }
                }else{
                    y--;
                    temp+=lengthX;
                    if(temp>lengthY/2){
                        x++;
                        temp+=lengthY;
                    }
                }
            }
        }else if(lengthX<=0 && lengthY>=0){
            for (i = 0; i < speed; i++) {
                if(abs(lengthX)>abs(lengthY)){
                    x--;
                    temp+=lengthY;
                    if(temp>lengthX/2){
                        y++;
                        temp+=lengthX;
                    }
                }else{
                    y++;
                    temp-=lengthX;
                    if(temp>lengthY/2){
                        x--;
                        temp-=lengthY;
                    }
                }
            }
        }
    }
}