我正在为我的学士论文设计一个程序,该程序根据 z 位置(电缆中的位置)显示钢丝绳的内部几何形状。
为了测试,我用这段代码“穿过”了一段电缆(sl
是一个链表,已经初始化,工作正常):
Cable c = new Cable(sl);
ImageFrame ts = new ImageFrame(c);
try {
while (location <2 ) {
location = location + 0.01;
Thread.sleep(100);
c.update(location);
ts.repaint();
}
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
电缆功能update
应该重新计算电缆的几何形状。我的类电缆有一个股线列表,这个update
函数的作用只是为所有这些股线调用更新函数:
public void update(double z) {
for(int i=0;i<strandList.size() ;i++) {
strandList.get(i).updateStrand(z);
}
}
该对象Strand
如下所示:
public abstract class Strand {
Punt middenCable;
final Punt nulpoint_midden;
Punt midden;
Double angle;
Double d_to_core;
Image img;
int img_size;
BufferedImage bf;
/**
*
* @param c centre of the cable
* @param p middle of the strand
* @param ang angle at this point
* @param dtc distance to core
* @param i image
*/
public Strand(Punt c,Punt p,Image i) {
nulpoint_midden = p; //location of strand at z=0
middenCable =c;
midden = p; // for drawing
img = i;
img_size = 135; //TODO adapt
bf = new BufferedImage(img_size, img_size, BufferedImage.TRANSLUCENT );
bf.getGraphics().drawImage(img, 0, 0, null);
}
/**
* angle goed zetten tov de z
* @param z
*/
abstract public void updateStrand(Double z);
public void paint(Graphics g){
g.setColor(Color.RED); //cirkels rood
//rotate around itself
int x = (int) (this.midden.getX() - img_size/2 );
int y = (int) (this.midden.getY() - img_size/2 );
int st = (int) this.img_size;
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
AffineTransform at = new AffineTransform();
at.setToRotation(Math.toRadians(angle), midden.getX(), midden.getY());
g2d.setTransform(at);
g2d.drawImage(bf, x, y, null);
g2d.dispose();
}
在绘画功能中,它以一定的角度围绕自身旋转。但问题是抽象函数“更新链”。这应该做的是将平底船 Midden 的坐标更改为它实际所在的位置。该参数不nulpoint_midden
应该改变并且是恒定的,用于计算。对于对象类型外链(扩展链),更新链如下所示:
import java.awt.Image;
public class OuterStrand extends Strand{
private double rotateconstant1 = 10; //tweaken tot perfecte laylength
private double rotateconstant2 = (2*Math.PI/34); //tweaken tot perfecte laylength
public OuterStrand(Punt c,Punt p, Image i) {
super(c, p,i);
}
@Override
public void updateStrand(Double z) {
// midden updaten
double xpoint = nulpoint_midden.getX(); //original point
double ypoint = nulpoint_midden.getY(); //original point
double cCableX = super.middenCable.getX();
double cCableY = super.middenCable.getY();
double dx = nulpoint_midden.getX() - cCableX;
double dy = ypoint - cCableY;
System.out.println(xpoint+ " " +ypoint) ;
double curangle = Math.atan2(dy, dx);
double dist = Math.sqrt(dx*dx + dy*dy);
double dangle = rotateconstant2 * z;
double x1 = cCableX + dist * Math.cos(dangle + curangle);
double y1 = cCableY + dist * Math.sin(dangle + curangle);
super.midden.setX(x1);
super.midden.setY(y1);
// rotate around itself
super.angle = z * Math.PI * rotateconstant1;
}
}
所以 println 给出了 xpoint 和 ypoint,这应该是常数。但实际上,它发生了变化(因此平底船nulpoint_midden
改变了价值)。我不知道为什么这个值会改变。这是我的印刷品:
> 500.0 200.0
> 500.55439838802135 200.00051226305845
> 501.66318759079536 200.00461035702926
> 503.32632406233347 200.0184412864145
> 505.54367148773895 200.0512248625842
> 508.3149156018249 200.11525184075379
> 511.63945065512087 200.22587972200301
> 515.5162375992163 200.40152475227
> 519.9436341271526 200.66364828540742
> 524.9191967987913 201.03673531535162
> 530.439455611731 201.54826262515832
> 536.4996615512757 202.22865365075 (etcetera)
由于这种变化的值,表示是不正确的。由于穿过电缆,它应该以恒定速度旋转,但实际上是在加速。
对于冗长的解释,我很抱歉,但如果有人看到我的错误并告诉我我做错了什么,那就太好了。谢谢!