我想在Java Swing应用程序中实现B样条。从4月开始,我几乎尝试了所有的事情并花费了我的空闲时间的每一分钟,但是我没有把它们弄好 - 从昨天开始我就头疼:/
为了实现B样条,我使用了wikipedia
中的规范我制作了(与MCVE相关的)演示版,可在此处找到:gist.github.com/soraphis/b-spline
代码显示了问题,它是在swing JPanel drawcomponent方法中调用的。但你可以评论这一行并取消注释71.
其他一些信息:
我需要帮助基础功能,我想知道如何正确建立结矢量""
我感谢各种回复 和thx读这个
答案 0 :(得分:2)
我无法想象一个合适的答案是不包含一段你可以编译和运行的代码(无需插入主方法和周围的GUI ...... * wink *),无论它与你的代码有多么不同。
但是,分配给T阵列的权重似乎很奇怪,基函数的索引似乎是+/- 1。我试图解决这个问题,但它仍然不完全正确,也许你或其他人喜欢继续调试这个。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BSplineTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 1));
BSplineTestPanel bSplineTestPanel = new BSplineTestPanel();
frame.getContentPane().add(bSplineTestPanel);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class BSplineTestPanel extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Bspline bspline = new Bspline();
bspline.calculatePath(g);
}
}
class Bspline
{
double[][] P;
double[] T;
int _k = 3;
public Bspline()
{
P = new double[4][2];
P[0][0] = 100;
P[0][1] = 100;
P[1][0] = 50;
P[1][1] = 50;
P[2][0] = 100;
P[2][1] = 200;
P[3][0] = 50;
P[3][1] = 200;
update();
}
private void update()
{
if (P.length < 2)
return;
T = new double[_k + P.length + 1];
double d = 1.0 / (T.length-1);
for (int i = 0; i<T.length; i++)
{
T[i] = i * d;
}
System.out.println(Arrays.toString(T));
}
private double basisFunc(int i, int k, double t)
{
if (k == 0)
{
if (T[i] <= t && t < T[i + 1])
return 1;
return 0;
}
double a = (t - T[i]) / (T[i + k] - T[i]);
double b = (T[i + k + 1] - t) / (T[i + k + 1] - T[i + 1]);
return a * basisFunc(i, k - 1, t) + b * basisFunc(i + 1, k - 1, t);
}
private double[] DeBoor(double t)
{
double[] V = new double[2];
for (int i = 0; i < P.length; i++)
{
double scale = basisFunc(i, _k, t);
V[0] += P[i][0] * scale;
V[1] += P[i][1] * scale;
}
return V;
}
public void calculatePath(Graphics g)
{
if (P.length < 2)
{
return; // zu wenige punkte um ein pfad zu zeichnen
}
double[] v = null;
double delta = 1 / 32.0;
for (double t = T[2]; t < T[5]; t += delta)
{
double[] p = DeBoor(t);
if (v != null)
{
g.setColor(new Color((int)(t*255), 0, 0));
g.drawLine((int) v[0], (int) v[1], (int) p[0], (int) p[1]);
}
v = p;
}
for (int i = 0; i < P.length; i++)
{
int x = (int)P[i][0];
int y = (int)P[i][1];
g.setColor(Color.RED);
g.fillOval(x-2, y-2, 4, 4);
g.drawString(String.valueOf(i), x, y+15);
}
}
}
(与http://www.ibiblio.org/e-notes/Splines/basis.html比较,“二次B样条(n = 3,k = 3)”)
除此之外,我想知道像deBoors算法这样过于复杂的****如何变得如此“着名”。使用De-Casteljau,您将在几分钟内完成,无需一次调试。
编辑来自http://chi3x10.wordpress.com/2009/10/18/de-boor-algorithm-in-c/
的代码的端口// From http://chi3x10.wordpress.com/2009/10/18/de-boor-algorithm-in-c/
double[] deBoor(int k,int degree, int i, double x, double knots[], double ctrlPoints[][])
{
if( k == 0)
{
i = Math.max(0, Math.min(ctrlPoints.length-1, i));
return ctrlPoints[i];
}
else
{
double alpha = (x-knots[i])/(knots[i+degree+1-k]-knots[i]);
double p0[] = deBoor(k-1,degree, i-1, x, knots, ctrlPoints);
double p1[] = deBoor(k-1,degree, i, x, knots, ctrlPoints);
double p[] = new double[2];
p[0] = p0[0] *(1-alpha ) + p1[0]*alpha;
p[1] = p0[1] *(1-alpha ) + p1[1]*alpha;
return p;
}
}
int WhichInterval(double x, double knot[], int ti)
{
int index = -1;
for(int i = 1; i <= ti - 1; i++)
{
if(x < knot[i]) {
index = i - 1;
break;
}
}
if(x == knot[ti - 1]) {
index = ti - 1;
}
return index;
}
private double[] DeBoor(double t)
{
int i = WhichInterval(t, T, T.length);
return deBoor(_k, 3, i, t, T, P);
}