demo-14
2D 등고선 데이타 그리기
course/basic/demo-14.m
함수 시그니처
function noise = perlin2d(width, height, scale) 전체 코드
전체 코드를 복사해서 Octave에서 바로 실행할 수 있습니다.
# 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]); 코드 해설
목적
2D 등고선 데이타 그리기
입력
- 파라미터: width
- 파라미터: height
- 파라미터: scale
출력
- 반환값: noise
- 그래프/figure 출력
- 콘솔 텍스트 출력
실행 흐름
- 초기화
- 데이터 준비
- 데이터 연산
- 그래프 그리기
핵심 함수/주제
ax1sizesub2indgrad_xgrad_ylerpMapset
실습 과제
- 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
- 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
- 핵심 함수 ax1의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.
학습 팁
같은 카테고리의 다른 코드
- colored
course/basic/colored.m - demo-00
course/basic/demo-00.m - demo-01
course/basic/demo-01.m - demo-02
course/basic/demo-02.m - demo-03a
course/basic/demo-03a.m - demo-03b
course/basic/demo-03b.m - demo-04
course/basic/demo-04.m - demo-05
course/basic/demo-05.m - demo-06
course/basic/demo-06.m - demo-07
course/basic/demo-07.m