카테고리
Course Basiccourse-basic
코드와 해설을 함께 읽는 학습 문서
Code Detail
2D 등고선 데이타 그리기
course/basic/demo-14.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
function noise = perlin2d(width, height, scale)
119 lines
# filename: demo-14.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 2D 등고선 데이타 그리기
#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------
run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));
function noise = perlin2d(width, height, scale)
% 2D Perlin noise 생성
% width: 이미지 너비
% height: 이미지 높이
% scale: 셀의 크기 (default: 10)
if nargin < 3
scale = 10;
end
[X, Y] = meshgrid(1:width, 1:height);
X = X / scale;
Y = Y / scale;
% 랜덤 gradient vectors
grad_x = cos(2 * pi * rand(height + 1, width + 1));
grad_y = sin(2 * pi * rand(height + 1, width + 1));
% 각 셀의 좌표
xi = floor(X);
yi = floor(Y);
% 셀 내부 상대 위치
xf = X - xi;
yf = Y - yi;
% dot products for 4 corners
dot00 = grad_x(sub2ind(size(grad_x), yi+1, xi+1)) .* xf + grad_y(sub2ind(size(grad_y), yi+1, xi+1)) .* yf;
dot10 = grad_x(sub2ind(size(grad_x), yi+1, xi+2)) .* (xf - 1) + grad_y(sub2ind(size(grad_y), yi+1, xi+2)) .* yf;
dot01 = grad_x(sub2ind(size(grad_x), yi+2, xi+1)) .* xf + grad_y(sub2ind(size(grad_y), yi+2, xi+1)) .* (yf - 1);
dot11 = grad_x(sub2ind(size(grad_x), yi+2, xi+2)) .* (xf - 1) + grad_y(sub2ind(size(grad_y), yi+2, xi+2)) .* (yf - 1);
% fade 함수
u = fade(xf);
v = fade(yf);
% 선형 보간
nx0 = lerp(dot00, dot10, u);
nx1 = lerp(dot01, dot11, u);
noise = lerp(nx0, nx1, v);
end
function out = fade(t)
out = t .* t .* t .* (t .* (t * 6 - 15) + 10);
end
function out = lerp(a, b, t)
out = a + t .* (b - a);
end
#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------
img = perlin2d(256, 256, 60);
#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------
figured("Size", [960, 960], "Move", [-1280, 0], "Name", mfilename);
ax1 = subplots(2, 2);
imshow(repmat(uint8((img+1.0)/2.0*255), [1, 1, 3]), "Parent", ax1(1, 1));
contourf(ax1(1, 2), img, 16);
contour3(ax1(2, 1), img, 16);
x = size(img, 2);
y = size(img, 1);
[xx, yy] = meshgrid(1:x, 1:y);
scatter3(ax1(2, 2), xx(:), yy(:), img(:), 1, img(:), "filled", "MarkerFaceAlpha", 0.5);
set(ax1(1, 1), "DataAspectRatio", [1, 1, 1]);
set(ax1(1, 2), ...
"DataAspectRatio", [1, 1, 1], ...
"YDir", "reverse", ...
"XTick", [], "YTick", [], ... % 눈금 제거
"XColor", "none", "YColor", "none" % 테두리 제거
);
set(ax1(2, 1), ...
"YDir", "reverse", ...
"XTick", [], "YTick", [], "ZTick", [], ... % 눈금 제거
"XColor", "none", "YColor", "none", "ZColor", "none", ... % 테두리 제거
"View", [15, 51]
);
set(ax1(2, 2), ...
"YDir", "reverse", ...
"XTick", [], "YTick", [], "ZTick", [], ... % 눈금 제거
"XColor", "none", "YColor", "none", "ZColor", "none", ... % 테두리 제거
"View", [15, 51]
);
title(ax1(1, 1), "Height Map (Grayscale)");
title(ax1(1, 2), "Height Map (Contour)")
title(ax1(2, 1), "Height Map (3D Contour)");
title(ax1(2, 2), "Height Map (3D Scatter)");
set_axes_layout(ax1, [1, 1], [1, 1]);