实现三维的连线

master
dai_48k 2024-03-19 15:23:10 +08:00
parent 9afa60fb71
commit 7911c7ce5e
5 changed files with 205 additions and 16 deletions

View File

@ -12,7 +12,7 @@ public class NFrame extends JFrame {
setLayout(null);
setLocationRelativeTo(null);
NPanel nPanel = new NPanel();
NPanel2 nPanel = new NPanel2();
nPanel.setLocation(45,45);
add(nPanel);

View File

@ -0,0 +1,126 @@
package top.dreamcenter.noise.ui;
import top.dreamcenter.noise.util.Ball;
import top.dreamcenter.noise.util.Dot;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* 500 x 500
* 10 x 10
*/
public class NPanel2 extends JPanel {
private static final int SCALE = 500;
public NPanel2() {
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 = 20;
// 绘制辅助线 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);
// 依据函数获取点矩阵 CHANGE!!
// 二维梯度
int dots = 30;
double[][] balls = new double[dots][dots];
List<Ball> list = new ArrayList<>();
for (int i = 0; i < dots; i++) {
for (int j = 0; j < dots; j++) {
balls[i][j] = Math.random() * 1;
Ball ball = new Ball(i, j, balls[i][j], cpt, grid);
ball.toR();
list.add(ball);
}
}
// 插值运算
for (int i = 0; i < dots - 1; i++) {
for (int j = 0; j < dots - 1; j++) {
// 获得初始点
Dot raw = new Dot(i,j, cpt, grid);
raw.toR();
// 差值
for (int m = 0; m < grid; m++) {
for (int n = 0; n < grid; n++) {
// <i + m, j + n>
// 水平A
double A = noise(
new Dot(j,balls[i][j],cpt, grid),
new Dot(j + 1,balls[i][j + 1],cpt, grid),
n * 1.0 / grid + j);
// 水平B
double B = noise(
new Dot(j,balls[i + 1][j],cpt, grid),
new Dot(j + 1,balls[i + 1][j + 1],cpt, grid),
n * 1.0 / grid + j);
// 竖直C
double C = noise(
new Dot(i, A, cpt, grid),
new Dot(i + 1, B,cpt, grid),
m * 1.0 / grid + i);
Ball ball = new Ball(i + m * 1.0 / grid, j + n * 1.0 / grid, C, cpt, grid);
ball.toR();
ball.x = raw.x + m;
ball.y = raw.y - n;
list.add(ball);
}
}
}
}
// draw
for (Ball ball : list) {
Ball.BallInt ballInt = ball.toInt();
g.setColor(new Color(60,80,80, (int)(ball.z * 255)));
ballInt.x -= grid * 14;
ballInt.y += grid * 14;
g.drawLine(ballInt.x, ballInt.y,ballInt.x, ballInt.y);
}
}
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,71 @@
package top.dreamcenter.noise.util;
public class Ball {
public double x;
public double y;
public double z;
public int cpt;
public int grid;
public Ball(double x, double y, double z, int cpt, int grid) {
this.x = x;
this.y = y;
this.z = z;
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 BallInt toInt(){
return new BallInt((int) x, (int) y, (int) z);
}
public static class BallInt{
public int x;
public int y;
public int z;
public BallInt(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
@Override
public String toString() {
return "Ball{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}

View File

@ -13,6 +13,13 @@ public class Dot {
this.grid = grid;
}
public Dot(double x, double y) {
this.x = x;
this.y = y;
cpt = 0;
grid = 0;
}
/**
*
*/

View File

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