게임만드시는 분들은 직선 그릴때 어떤 알고리즘을 사용하시는지 궁금하군요.
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;
}
}
}
}
}
}
네 일반적인 언어차원에서 지원하는 그래픽 라이브러리나 플랫폼SDK에 포함된 함수죠.
canvas의 경우, drawLine 이 있네요ㅎㅎ
openGL 에도 당연히 있겠죠.
당장 필요한게 아니라 찾아보진 못하지만,
속도문제로 인해 공식화된 루틴이 따로 있습니다. 그게 브레센햄이었는지는 가물가물하네요.
이걸로 2D,3D 직곡선 이동좌표 모두 해결 가능할 것입니다.
추가로, 브레센햄 대신 Line 함수를 이용해서 이동좌표를 구할수도 있습니다.
방법의 차이가 있겠지만,
메모리상에 라인을 그리고, 그려진 픽셀값을 순차적으로 접근하면 됩니다.
(효율성은 구현하려는 모델마다 다르므로, 필요에 맞게 선택하여 사용해보시길 바랍니다.)




정점 좌표가 필요한 경우가 아니라면, 그냥 Line 함수 씁니다.