我之前问过这个问题,但我想我写得很糟糕,因此没有得到任何答复。我正在试图制作一个android应用程序,用于绘制抛物体运动中物体的路径。我有方程来完成这个工作,但由于某种原因,当我运行我的程序时,我所得到的只有2条连接线,而不是正确的弧。我一直在盯着这几个小时,任何人都可以告诉我发生了什么,我需要做些什么来解决它?这里是我的代码:在Android中绘制弹道运动路径
float constx = 400;
float consty = 375;
float deltx = (float) ProjectileMotionDrawingActivity.dx;
float delty = (float) ProjectileMotionDrawingActivity.dy;
float maxDrawingHeight;
float totwidth;
float totheight;
float starty;
float ydist;
float cx = canvas.getWidth()/2;
float cy = 210;
boolean limiter;
float vin = (float) ProjectileMotionDrawingActivity.vin;
float vxd = (float) ProjectileMotionDrawingActivity.vxd;
float acc = (float) ProjectileMotionDrawingActivity.ac;
float scaleda;
float scaledv;
float scaledvi;
//Set background color and get paint ready
canvas.drawColor(Color.WHITE);
Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(Color.BLACK);
//Define maxDrawingHeight
if(delty >= 0){
maxDrawingHeight = (float) ProjectileMotionDrawingActivity.mhe;
}else{
maxDrawingHeight = (float) (ProjectileMotionDrawingActivity.mhe + Math.abs(delty));
}
// Determine whether x or y is limiting factor (true=x, false =y) (For future use if needed)
if(Math.abs(maxDrawingHeight/deltx) >=consty/constx){
limiter = false;
}else{
limiter = true;
}
//set width and height of projectile motion
if(limiter){
totwidth = constx;
totheight = constx*maxDrawingHeight/deltx;
scaleda = acc*constx/deltx;
scaledvi = vin*constx/deltx;
scaledv = vxd*constx/deltx;
}else{
totheight = consty;
totwidth = consty*deltx/maxDrawingHeight;
scaleda = acc*consty/maxDrawingHeight;
scaledvi = vin*consty/maxDrawingHeight;
scaledv = vxd*consty/maxDrawingHeight;
}
//height of cliff
ydist = delty*totheight/maxDrawingHeight;
//start height
starty = cy+(totheight/2);
canvas.drawLine(0, starty, totwidth+35, starty, linePaint);
canvas.drawLine(totwidth+35, starty, totwidth+35, starty-ydist, linePaint);
canvas.drawLine(totwidth+35, starty-ydist, 2*cx, starty-ydist, linePaint);
//Parabola
float porabx = 35;
float poraby = starty;
float porabx2 = 35 + totwidth/50;
float poraby2 = (float) (starty - scaledvi*porabx2/scaledv-.5*scaleda*Math.pow(porabx2/scaledv,2));
for(int i=0;i<50;i++){
canvas.drawLine(porabx, poraby, porabx2, poraby2 , linePaint);
porabx = porabx2;
poraby = poraby2;
porabx2 += totwidth/50;
poraby2 = (float) (starty - scaledvi*porabx2/scaledv-.5*scaleda*Math.pow(porabx2/scaledv,2));
}
}
更新(这也吸引了地上,但是这部分似乎工作它包括在内,因为一些用于创建地面的变量也都在电弧中使用。):后看着这一段时间,尝试不同的数字,我相信所画的第一条线是正确的第一条弧(1/50)。出于某种原因,似乎在循环中poraby2变量存在问题。
我对这个问题的前一版本的评论依然存在。如果你想解决这个问题,你必须付出一些努力。 – Beta 2011-01-30 01:59:40