bresenham 알고리즘을 이용한 직선, 원, 타원 그리기
여기 참고 해서 php용으로 만들었습니다.
그런데 결과물은 php내장함수와 거의 똑같네요.
좀 다를 줄 알았는데......
php 내장함수를 사용하지 않고 원이나 타원 직선을 그릴수 있는 정도에 만족해야 할듯합니다.
알고리즘에 대한 설명은 없습니다.
test.php
-----------------------------------------------------------------------------------------------------------------------------
<?php
//직선 그리기
function imageline_bresenham(& $im, $startX, $startY, $endX, $endY, $color){
//기준점 계산, 작은 쪽이 출발점, 큰쪽이 도착점
if($endX < $startX){
$dx = $startX - $endX;
$tmpX = $endX;
$tmpgoX = $startX;
}
else{
$dx = $endX - $startX;
$tmpX = $startX;
$tmpgoX = $endX;
}
if($endY < $startY){
$dy = $startY - $endY;
$tmpY = $endY;
$tmpgoY = $startY;
}
else{
$dy = $endY - $startY;
$tmpY = $startY;
$tmpgoY = $endY;
}
/* 기울기에 따른 계산*/
if($dx >= $dy) { //기울기가 1보다 작거나 같을때
$d = $dy * 2 - $dx; //d의 값을 초기화
$incrE = $dy * 2; // E로의 이동을 위해서 쓰이는 증가
$incrNE = ($dy - $dx) * 2; // NE로의 이동을 위해서 쓰이는 증가
$start = $tmpX;
$end = $tmpgoX;
}
else { //기울기가 1보다 클때
$d = $dx * 2 - $dy;
$incrE = $dx * 2;
$incrNE = ($dx - $dy) * 2;
$start = $tmpY;
$end = $tmpgoY;
}
//x, y 좌표 초기화
$x = $startX;
$y = $startY;
imagesetpixel($im, $x, $y, $color);//시작점 찍음
while($start < $end) {
if( $d <= 0) { /*D값이 0보다 작으면 E를 선택 하나의 축만 이동*/
$d += $incrE;
if($dx >= $dy) { // 기울기가 1보다 작을때 X좌표만 증가
if($endX < $startX)
$x--;
else
$x++;
}
else { // 기울기가 1보다 클때 Y좌표만 증가
if($endY < $startY)
$y--;
else
$y++;
}
}
else { /*D값이 0보다 클때 NE를 선택 X,Y 를 같이 이동시킨다.*/
$d += $incrNE;
if($endX < $startX)
$x--;
else
$x++;
if($endY < $startY)
$y--;
else
$y++;
}
imagesetpixel($im, $x, $y, $color);
$start ++;
}
}
function imagearc_bresenham(& $im, $centerX, $centerY, $width, $height, $color) {
$w_r = ceil($width / 2);
$h_r = ceil($height / 2);
$x = 0;
$y = $h_r;
$w_r_square = $w_r * $w_r;
$h_r_square = $h_r * $h_r;
$d = (4 * $h_r_square + $w_r_square * (1 - (4 * $h_r))) / 4;
imagesetpixel($im, $x + $centerX, $y + $centerY, $color);
imagesetpixel($im, $centerX - $x, $y + $centerY, $color);
imagesetpixel($im, $x + $centerX, $centerY - $y , $color);
imagesetpixel($im, $centerX - $x, $centerY - $y, $color);
// $x 독립변수 구간
while($h_r_square * $x < $w_r_square * $y){
++$x;
if($d < 0){
$d += $h_r_square * (2 * $x + 1);
}
else {
--$y;
$d += $h_r_square * (2 * $x + 1) - (2 * $w_r_square * $y);
}
imagesetpixel($im, $x + $centerX, $y + $centerY, $color);
imagesetpixel($im, $centerX - $x, $y + $centerY, $color);
imagesetpixel($im, $x + $centerX, $centerY - $y, $color);
imagesetpixel($im, $centerX - $x, $centerY - $y, $color);
}
// $y 독립변수 구간
$x = $w_r;
$y = 0;
$d = (4 * $w_r_square + $h_r_square * (1 - (4 * $w_r))) / 4;
imagesetpixel($im, $x + $centerX, $y + $centerY, $color);
imagesetpixel($im, $centerX - $x, $y + $centerY, $color);
imagesetpixel($im, $x + $centerX, $centerY - $y, $color);
imagesetpixel($im, $centerX - $x, $centerY - $y, $color);
while($h_r_square * $x > $w_r_square * $y){
++$y;
if($d < 0){
$d += $w_r_square * (2 * $y + 1);
}
else {
--$x;
$d += $w_r_square * (2 * $y + 1) - (2 * $h_r_square * $x);
}
imagesetpixel($im, $x + $centerX, $y + $centerY, $color);
imagesetpixel($im, $centerX - $x, $y + $centerY, $color);
imagesetpixel($im, $x + $centerX, $centerY - $y, $color);
imagesetpixel($im, $centerX - $x, $centerY - $y, $color);
}
}
$w = 400;
$h = 800;
$im = imagecreatetruecolor($w, $h);
$red = imagecolorallocate($im, 255, 0, 0);
imageline_bresenham($im, 200, 200, 100, 0, $red);
imageline_bresenham($im, 200, 200, 400, 100, $red);
imageline_bresenham($im, 200, 200, 400, 200, $red);
imageline_bresenham($im, 200, 200, 400, 300, $red);
imagearc_bresenham($im, 200, 200, 180, 360, $red);
imagearc_bresenham($im, 100, 200, 200, 200, $red);
imageline($im, 200, 600, 100, 400, $red);
imageline($im, 200, 600, 400, 500, $red);
imageline($im, 200, 600, 400, 600, $red);
imageline($im, 200, 600, 400, 700, $red);
imagearc($im, 200, 600, 180, 360, 0, 360, $red);
imagearc($im, 100, 600, 200, 200, 0, 360, $red);
header('Content-Type: image/png');
imagepng($im);
?>
샘플 : http://apmusers.com/php_arc.php
게시글 목록
| 번호 | 제목 |
|---|---|
| 13454 | |
| 29442 |
HTML
php 에서 xml 자유롭게 쓰기
4
|
| 13451 | |
| 13448 |
jQuery
자동등록방지코드
2
|
| 13446 | |
| 13445 |
jQuery
ajax modal window (ajax 모달창)
|
| 13442 |
jQuery
간단하게 Round, 그라디언트 처리하기!!
2
|
| 13439 | |
| 13437 | |
| 13434 | |
| 13429 | |
| 13425 | |
| 13423 | |
| 13418 |
jQuery
탭메뉴
4
|
| 13410 | |
| 13408 | |
| 13407 | |
| 13406 |
기타
수학 함수 모음
|
| 29441 |
HTML
변수 함수 모음
|
| 13405 |
JavaScript
URL 함수 모음
|
| 13404 |
기타
디렉토리 함수 모음
|
| 13401 |
JavaScript
파일 함수 모음
2
|
| 13400 |
기타
문자열 함수 모음
|
| 13399 | |
| 13395 |
MySQL
Mysql 날짜관련 함수 모음
3
|
| 13391 |
jQuery
jquery scrolling menu
3
|
| 29435 | |
| 13389 |
JavaScript
정규 표현식 입문서? "손에 잡히는 정규 표현식"
1
|
| 13386 |
정규표현식
정규표현식 검사기
2
|
| 13381 |
jQuery
Jquery 체크박스 사용법
4
|
| 13377 |
Flash
sql 인젝션 복구쿼리
3
|
| 13370 |
JavaScript
페이지 자동이동하기 팁(여기 없는 거)
6
|
| 13367 |
MySQL
mysql 유저 생성, 권한 부여
2
|
| 13366 |
JavaScript
자바스크립트 쿠키 생성, 삭제
|
| 13364 | |
| 13362 | |
| 29432 | |
| 29428 |
HTML
엔터치면 다음칸으로 이동.
3
|
| 13361 |
jQuery
IBM 한국 jQuery 기술자료
|
| 13360 | |
| 13358 | |
| 13354 | |
| 13351 |
JavaScript
이미지 자동으로 사이즈 정해주는 방법은 어떻게 하나요?
2
|
| 29427 | |
| 13346 | |
| 13340 |
jQuery
lightbox 수정 2차 완료
5
|
| 13337 | |
| 13334 | |
| 13328 |
jQuery
선물입니다.
5
|
| 13326 |
JavaScript
virtual hosts
1
|
| 13325 |
기타
asp 글 삭제하기
|
| 13324 |
기타
asp 글 수정하기
|
| 13323 |
기타
asp 글읽기
|
| 29426 |
HTML
asp 목록보기
|
| 13322 |
JavaScript
asp 저장하기
|
| 13321 |
기타
asp 글쓰기
|
| 13315 | |
| 29424 | |
| 13312 |
JavaScript
오픈 api 에 사용할 XML 파서 따끈한거 테스트 부탁드립니다....^^
2
|
| 29422 | |
| 25114 | |
| 13308 | |
| 13307 | |
| 13305 |
JavaScript
if문 축약해 사용하기
1
|
| 13304 |
JavaScript
체크 상자 몇개 이상 채크시 경고창 및 개수이상 클릭못하게하기
|
| 25111 | |
| 29421 |
HTML
테이블 복사
|
| 13301 | |
| 13299 | |
| 13296 |
jQuery
별거는 아니지만
2
|
| 13292 |
JavaScript
[VIM] vim으로 소스코드 범위 설정하여 영역을 접고 펴기
3
|
| 13291 | |
| 13290 |
JavaScript
"웹표준에 관한 타입 선언"- 필요하신 분은 참고하세요...
|
| 13289 | |
| 13288 | |
| 13287 | |
| 13286 | |
| 13285 | |
| 13284 |
기타
아파치 설치문서
|
| 29420 | |
| 13283 |
JavaScript
글자 쪼개서 배열에 넣기
|
| 13277 | |
| 13274 | |
| 13266 |
MySQL
MySQL 상태보기 (MySQL튜닝하기)
7
|
| 13265 | |
| 13262 | |
| 13261 |
jQuery
링크 관련 활용
|
| 13259 |
MySQL
Slow Query Log
1
|
| 13254 |
JavaScript
폼의 첫번째 입력가능한 필드에 FOCUS 주기
4
|
| 13250 | |
| 29419 | |
| 13242 | |
| 13241 | |
| 13236 | |
| 13235 | |
| 29418 |
HTML
레이어 좌표 이동-1
|
| 29417 |
HTML
프레임 제어속성
|
| 13234 | |
| 13233 | |
| 13231 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기