

map() ๋ฉ์๋๋ ํ ๋ฒ์(Range) ๋ด์ ๊ฐ์ ๋ค๋ฅธ ๋ฒ์ ๋ด์ ๊ฐ์ผ๋ก ์ ํ์ ์ผ๋ก ๋ณํ(Scaling)ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์ ๋๋ ์ดํฐ๋ธ ์ํธ์์๋ ๋ง์ฐ์ค ์์น์ ๊ฐ์ ์
๋ ฅ ๊ฐ์ ์์ ๊ฐ์ด๋ ํฌ๊ธฐ ๊ฐ ๋ฑ ์ํ๋ ์ถ๋ ฅ ๋ฒ์๋ก ์ฝ๊ฒ ๋ณํํ ๋ ํ์์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
map(value, start1, stop1, start2, stop2)
value: ๋ณํํ๋ ค๋ ์ ๋ ฅ ๊ฐ (์: ๋ง์ฐ์ค ์์น)start1,stop1: ์ ๋ ฅ ๊ฐ(value)์ด ์ํ ์๋์ ๋ฒ์start2,stop2: ๋ณํ๋ ๊ฐ์ ๋งตํํ ๋ฒ์
map() ํจ์๋ ๋ด๋ถ์ ์ผ๋ก ๋ค์ ์ํ ๊ณต์์ ์ฌ์ฉํ์ฌ ์ ํ ๋ณํ์ ์ํํฉ๋๋ค
๋ง์ฐ์ค์ X ์ขํ(0๋ถํฐ width๊น์ง)๋ฅผ ํ์์กฐ ๋ฐฐ๊ฒฝ์(0๋ถํฐ 255๊น์ง)์ผ๋ก ๋ณํํ๋ ์์ ์
๋๋ค.
function setup() {
createCanvas(400, 400);
noStroke();
}
function draw() {
// mouseX (0 ~ width) ๊ฐ์ grayscale (0 ~ 255) ๊ฐ์ผ๋ก ๋ณํ
let colorValue = map(mouseX, 0, width, 0, 255);
// ๋ณํ๋ ๊ฐ์ ๋ฐฐ๊ฒฝ์์ผ๋ก ์ค์
background(colorValue);
// (Optional) ํ๋ฉด ์ค์์ ์ ๋ณด ํ์
fill(255 - colorValue); // ๋ฐฐ๊ฒฝ์๊ณผ ๋๋น๋๋ ์์ผ๋ก ์ค์
textSize(20);
textAlign(CENTER, CENTER);
text("Color: " + round(colorValue), width / 2, height / 2);
}
constrain() ๋ฉ์๋๋ ์ด๋ค ๊ฐ์ ๋ฒ์๋ฅผ ํน์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ ์ฌ์ด๋ก ์ ํ(Clamping)ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์ฆ, ๊ฐ์ด ์ง์ ๋ ๋ฒ์๋ฅผ ๋ฒ์ด๋์ง ์๋๋ก ๊ฐ์ ํฉ๋๋ค.
constrain(value, min, max)
- value : ์ ํํ๋ ค๋ ์ ๋ ฅ ๊ฐ
- min : ํ์ฉ๋๋ ์ต์ ๊ฐ (Minimum Value)
- max : ํ์ฉ๋๋ ์ต๋ ๊ฐ (Maximum Value)
value๊ฐ min๋ณด๋ค ์์ผ๋ฉด (value < min), min์ ๋ฐํํฉ๋๋ค.value๊ฐ max๋ณด๋ค ํฌ๋ฉด (value > max), max๋ฅผ ๋ฐํํฉ๋๋ค.value๊ฐ min๊ณผ max ์ฌ์ด์ ์์ผ๋ฉด, value ๊ทธ ์์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.map() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ณ์ฐ๋ ๊ฐ์ด ์๋์น ์๊ฒ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๊ฒฝ์ฐ, ์ด ๊ฐ์ ํน์ ๋ฒ์๋ก ์ ํํ๋ ์์ ์
๋๋ค.
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// ๋ง์ฐ์ค ์์น์ ๋ฐ๋ผ ์์ ํฌ๊ธฐ๋ฅผ (0~100)์ผ๋ก ๋ณํ
let mappedSize = map(mouseX, 0, width, 0, 100);
// mappedSize์ 150์ ๋ํ ๊ฐ์ ์๋ก์ด ํฌ๊ธฐ ํ๋ณด๋ก ์ฌ์ฉ (250๊น์ง ์ปค์ง ์ ์์)
let newSize = mappedSize + 150;
// constrain()์ ์ฌ์ฉํ์ฌ ์ต์ข
ํฌ๊ธฐ๋ฅผ 100๊ณผ 200 ์ฌ์ด๋ก ์ ํ
// ์๋ฌด๋ฆฌ ๋ง์ฐ์ค๋ฅผ ์์ง์ฌ๋ ์์ ํฌ๊ธฐ๋ 100๋ณด๋ค ์์ด์ง๊ฑฐ๋ 200๋ณด๋ค ์ปค์ง์ง ์์
let finalSize = constrain(newSize, 100, 200);
fill(30, 190, 170);
ellipse(width / 2, height / 2, finalSize, finalSize);
// (Optional) ํ๋ฉด ์ค์์ ํฌ๊ธฐ ์ ๋ณด ํ์
fill(0);
textSize(13);
textAlign(CENTER, CENTER);
text("Final Size: " + round(finalSize), width / 2, height / 2);
}
// ๋ํ์ ์ต์/์ต๋ ํฌ๊ธฐ ์ค์
const MIN_SIZE = 300;
const MAX_SIZE = 700;
// ๋ค๊ฐํ์ ์ต์/์ต๋ ์ ์ ์ ์ค์ (3~128 ์ฌ์ด)
const MIN_VERTICES = 3;
const MAX_VERTICES = 128;
function setup() {
createCanvas(800, 800);
angleMode(DEGREES); // ๊ฐ๋ ๋จ์๋ฅผ Degree(๋)๋ก ์ค์
noStroke();
}
function draw() {
background(25); // ์ด๋์ด ๋ฐฐ๊ฒฝ
// 1. ํ๋ฉด ์ค์์ ๊ธฐ์ค์ผ๋ก ๋ง์ฐ์ค ์์น ๊ณ์ฐ
// centerDeltaX: ๋ง์ฐ์ค X์ขํ์ ์ค์ฌ X์ขํ ๊ฐ์ ์ฐจ์ด (-width/2 ~ +width/2)
let centerDeltaX = mouseX - width / 2;
// centerDeltaY: ๋ง์ฐ์ค Y์ขํ์ ์ค์ฌ Y์ขํ ๊ฐ์ ์ฐจ์ด (-height/2 ~ +height/2)
let centerDeltaY = mouseY - height / 2;
// --- ๋งคํ ๋ฐ ์ ํ ---
// 2. X์ถ ์์น๋ฅผ '์์ ๊ฐ (Hue)'์ผ๋ก ๋งคํ ๋ฐ ์ ํ
// centerDeltaX ๋ฒ์: -width/2 ~ width/2
// ๋ชฉํ ์์ ๋ฒ์ (HUE): 0 ~ 360 (์์ํ ์ ์ฒด)
let mappedHue = map(centerDeltaX, -width / 2, width / 2, 0, 360);
// map() ํจ์๋ stop1, stop2๋ฅผ ๋ฒ์ด๋ ๊ฐ๋ ๋น๋ก์ ์ผ๋ก ๊ณ์ฐํ๋ฏ๋ก,
// ์ํ์ง ์๋๋ค๋ฉด constrain์ผ๋ก ๋ฒ์๋ฅผ ๋ช
ํํ ์ ํํฉ๋๋ค. (์ต์
)
let finalHue = constrain(mappedHue, 0, 360);
// 3. Y์ถ ์์น๋ฅผ 'ํฌ๊ธฐ'๋ก ๋งคํ ๋ฐ ์ ํ
// centerDeltaY ๋ฒ์: -height/2 ~ height/2
// ๋ชฉํ ํฌ๊ธฐ ๋ฒ์: MIN_SIZE ~ MAX_SIZE
let mappedSize = map(
centerDeltaY,
-height / 2,
height / 2,
MIN_SIZE,
MAX_SIZE
);
let finalSize = constrain(mappedSize, MIN_SIZE, MAX_SIZE);
// 4. ๋ง์ฐ์ค์ ์ค์ ์ฌ์ด์ '๊ฑฐ๋ฆฌ'๋ฅผ '์ ์ ์'๋ก ๋งคํ ๋ฐ ์ ํ
// dist() ํจ์๋ก ๋ง์ฐ์ค์ ์ค์(width/2, height/2) ์ฌ์ด์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ (0 ~ max_diagonal)
let distFromCenter = dist(mouseX, mouseY, width / 2, height / 2);
let maxDist = dist(0, 0, width / 2, height / 2); // ์ต๋ ๊ฑฐ๋ฆฌ (์ฝ๋๊น์ง)
// ๊ฑฐ๋ฆฌ ๋ฒ์: 0 ~ maxDist
// ๋ชฉํ ์ ์ ์ ๋ฒ์: MIN_VERTICES ~ MAX_VERTICES
let mappedVertices = map(
distFromCenter,
0,
maxDist,
MIN_VERTICES,
MAX_VERTICES
);
// ์ ์ ์๋ ์ ์์ฌ์ผ ํ๋ฏ๋ก, mappedVertices๋ฅผ ์ ์ํ (round, floor, ceil ์ค ํ 1)
let rawVertices = round(mappedVertices);
// ๋ค๊ฐํ ์ ์ ์๋ฅผ 3~128 ์ฌ์ด๋ก ์ต์ข
์ ํ
let finalVertices = constrain(rawVertices, MIN_VERTICES, MAX_VERTICES);
// --- ๋ํ ๊ทธ๋ฆฌ๊ธฐ ---
// HSB(Hue, Saturation, Brightness) ์ปฌ๋ฌ ๋ชจ๋ ์ค์
colorMode(HSB, 360, 100, 100);
fill(finalHue, 100, 100); // ๋งคํ๋ HUE ๊ฐ์ผ๋ก ์ฑ์ฐ๊ธฐ
// ๋ํ์ ํ๋ฉด ์ค์์ ์์น์ํต๋๋ค.
push();
translate(width / 2, height / 2);
// ๋ค๊ฐํ ๊ทธ๋ฆฌ๊ธฐ ํจ์ ํธ์ถ
drawPolygon(0, 0, finalSize / 2, finalVertices); // finalSize๋ฅผ ์ง๋ฆ์ผ๋ก ์ฌ์ฉ
pop();
// --- ์ ๋ณด ํ์ ---
colorMode(RGB); // ๋ค์ ๊ธฐ๋ณธ RGB ๋ชจ๋๋ก ์ ํ
fill(255);
textAlign(CENTER, TOP);
textSize(20);
text(`Hue (X): ${round(finalHue)}`, width / 2, height /2 - 20);
text(`Size (Y): ${round(finalSize)}`, width / 2, height / 2 );
text(`Vertices (Dist): ${finalVertices}`, width / 2, height / 2 + 20);
}
/**
* ์ง์ ๋ ์์น์ ์ ๋ค๊ฐํ์ ๊ทธ๋ฆฌ๋ ํจ์
* @param x x ์ขํ
* @param y y ์ขํ
* @param radius ๋ฐ์ง๋ฆ
* @param npoints ์ ์ ์
*/
function drawPolygon(x, y, radius, npoints) {
let angle = 360 / npoints; // ๊ฐ ์ ์ ์ฌ์ด์ ๊ฐ๋
beginShape();
for (let a = 0; a < 360; a += angle) {
let sx = x + cos(a) * radius;
let sy = y + sin(a) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
}