가상화면에 삼각형을 그려할 일이 생겨.. 삼각형 그리기에 대한 포스팅을 하게 되었다;;
일단 쉽게 그릴수 있는 삼각형에 대해 생각을 하자.
쉽게 그릴수 있는 삼각형이란 한변이 x축이나 y축에 평행한 삼각형이다.
위와 같은 x축에 평행한 변을 삼각형을 그릴때의 알고리즘은 다음과 같다.
그럼 축에 평행한 변이 없는 삼각형은??????????
... 답은 간단하다.. 삼각형을 둘로 쪼갠다~
P4를 구하는 방법은. P3~P1의 직선 방정식을 구한후, y좌표에 P2를 대입한다.
식을 유도하면..
최종적으로 3번 공식으로 구해진 X와 Y2를 P4로 사용하면 되겠다 ^^
일단 쉽게 그릴수 있는 삼각형에 대해 생각을 하자.
쉽게 그릴수 있는 삼각형이란 한변이 x축이나 y축에 평행한 삼각형이다.
위와 같은 x축에 평행한 변을 삼각형을 그릴때의 알고리즘은 다음과 같다.
(P숫자.x = P숫자의 x좌표, P숫자.y = p숫자의 y좌표)
ScreenArray[screen_height][screen_width]
color = //삼각형 색
delta_left = (P3.x - P1.x) / (P3.y - P1.y) // P3 ~ P1의 기울기
delta_right = (P3.x - P2.x) / (P3.y - P2.y) // P3 ~ P1의 기울기
start_x_position = P1.x
end_x_position = P2.x
for( y_position = P1.x ; y_position <= P3.y; y_position++)
{
ScreenArray[screen_height][screen_width]
color = //삼각형 색
delta_left = (P3.x - P1.x) / (P3.y - P1.y) // P3 ~ P1의 기울기
delta_right = (P3.x - P2.x) / (P3.y - P2.y) // P3 ~ P1의 기울기
start_x_position = P1.x
end_x_position = P2.x
for( y_position = P1.x ; y_position <= P3.y; y_position++)
{
for(x_position = start_x_position ; x_position <= end_x_position; x_position++)
{
ScreenArray[y_position][x_position] = color
}
start_x_position += delta_left;
end_x_position -= delta_right;
{
ScreenArray[y_position][x_position] = color
}
start_x_position += delta_left;
end_x_position -= delta_right;
}
그럼 축에 평행한 변이 없는 삼각형은??????????
... 답은 간단하다.. 삼각형을 둘로 쪼갠다~
P4를 구하는 방법은. P3~P1의 직선 방정식을 구한후, y좌표에 P2를 대입한다.
식을 유도하면..
최종적으로 3번 공식으로 구해진 X와 Y2를 P4로 사용하면 되겠다 ^^
이올린에 북마크하기
이올린에 추천하기
