实现噪声函数

master
dai_48k 2024-03-18 09:23:47 +08:00
commit 9afa60fb71
6 changed files with 219 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
out/
*.iml
.idea/

View File

@ -0,0 +1,9 @@
package top.dreamcenter.noise;
import top.dreamcenter.noise.ui.NFrame;
public class Main {
public static void main(String[] args) {
new NFrame();
}
}

View File

@ -0,0 +1,22 @@
package top.dreamcenter.noise.ui;
import javax.swing.*;
import java.awt.*;
public class NFrame extends JFrame {
public NFrame(){
setSize(600,626);
setTitle("perlin噪声算法");
setLayout(null);
setLocationRelativeTo(null);
NPanel nPanel = new NPanel();
nPanel.setLocation(45,45);
add(nPanel);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}

View File

@ -0,0 +1,112 @@
package top.dreamcenter.noise.ui;
import top.dreamcenter.noise.util.Dot;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* 500 x 500
* 10 x 10
*/
public class NPanel extends JPanel {
private static final int SCALE = 500;
public NPanel() {
setSize(SCALE,SCALE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
// 绘制画板 OK
g.setColor(Color.white);
g.fillRect(0,0,500,500);
// 中心点位置 OK OR CHANGE -grid-
int cpt = SCALE>>1;
int grid = 50;
// 绘制辅助线 OK
g.setColor(Color.lightGray);
for (int i = 0; i < cpt / grid + 1; i++) {
// 水平
g.drawLine(0,cpt + grid * i,SCALE, cpt + grid * i);
g.drawLine(0,cpt - grid * i,SCALE, cpt - grid * i);
// 竖直
g.drawLine(cpt - grid * i,0, cpt - grid * i, SCALE);
g.drawLine(cpt + grid * i,0, cpt + grid * i, SCALE);
}
// 绘制坐标轴 OK
g.setColor(Color.gray);
g.fillRect(0, cpt-1,SCALE, 3);
g.fillRect(cpt - 1, 0,3, SCALE);
// 装饰
g.setColor(Color.BLUE);
// g.setFont(new Font("微软雅黑", Font.BOLD, 18));
// g.drawString("平滑函数F(x) = 6*x^5 - 15*x^4 - 10*x^3", 10, 100);
// g.drawString("噪声算法Noise(P) = Y1 + (Y2 - Y1)*F((xp-x1)/(delta_x))", 10, 150);
// 依据函数获取点矩阵 CHANGE!!
g.setColor(Color.RED);
Dot[] raw = new Dot[40]; // 原始采样点
for (int i = 0; i < raw.length; i++) {
Dot tmp = new Dot(i - 20, Math.random() * 2 * (Math.random() > 0.5 ? 1 : -1), cpt, grid);
tmp.toR();
raw[i] = tmp;
}
ArrayList<Dot.DotInt> dots = new ArrayList<>();
for (int i = 0; i < raw.length - 1; i++) {
// 插入原始点
dots.add(raw[i].toInt());
// 插值
Dot fi = raw[i].getV();
Dot se = raw[i + 1].getV();
// 插值
for (int j = 1; j < grid; j++) {
double x = fi.x + j * 1.0 / grid;
Dot ist = new Dot(x, noise(fi, se, x), cpt, grid);
ist.toR();
ist.x = raw[i].x + j;
dots.add(ist.toInt());
}
}
// 绘制函数 OK
Dot.DotInt front = dots.get(0);
g.fillOval(front.x - 1, front.y - 1, 3,3);
for (int i = 1; i < dots.size(); i++) {
Dot.DotInt t = dots.get(i);
// g.fillOval(t.x - 1, t.y - 1, 3,3);
// g.drawLine(t.x - 1, t.y - 1, t.x - 1,cpt);
g.drawLine(front.x, front.y, t.x, t.y);
front = t;
}
}
private double noise(Dot fi, Dot se, double x){
return fi.y + (se.y - fi.y) * func((x - fi.x)/(se.x - fi.x));
}
private double func(double x) {
return 6 * Math.pow(x, 5) - 15 * Math.pow(x, 4) + 10 * Math.pow(x, 3);
}
}

View File

@ -0,0 +1,58 @@
package top.dreamcenter.noise.util;
public class Dot {
public double x;
public double y;
public int cpt;
public int grid;
public Dot(double x, double y, int cpt, int grid) {
this.x = x;
this.y = y;
this.cpt = cpt;
this.grid = grid;
}
/**
*
*/
public void toV() {
x = (x - cpt) / grid;
y = (cpt - y) / grid;
}
public Dot getV() {
Dot tmp = new Dot(x, y , cpt, grid);
tmp.toV();
return tmp;
}
/**
*
*/
public void toR () {
x = x * grid + cpt;
y = cpt - y * grid;
}
public Dot getR() {
Dot tmp = new Dot(x, y , cpt, grid);
tmp.toR();
return tmp;
}
public DotInt toInt(){
return new DotInt((int) x, (int) y);
}
public static class DotInt{
public int x;
public int y;
public DotInt(int x, int y) {
this.x = x;
this.y = y;
}
}
}

View File

@ -0,0 +1,15 @@
package top.dreamcenter.noise.util;
public class Perlin {
public static void generatePerlin(int baseGrids) {
int width = baseGrids * 5;
double[][] map = new double[width][width];
}
}