我有一个简单的功能,可以在特定的方向上移动一个圆圈:
var rad = (a) => Math.PI / 180 * a;
this.x += Math.cos(rad) * this.throttle();
this.y += Math.sin(rad) * this.throttle();
我也在计算与目标的距离:
var distance = (p1, p2) => Math.sqrt( (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) );
this.destination_distance = parseInt(distance( { x: this.x, y: this.y }, { x: x, y: y } ));
我开始研究this.throttle
功能,但我无法理解它。
我想要实现简单的事情,当圈子开始移动时我希望通过某个步骤将速度从min
提高到max
,当它接近目的地时,我希望它开始减速直到它到达min
。
这是我目前的做法:
this.min_speed = 0.1;
this.max_speed = 1.5;
this.current_speed = 0.1;
this.throttle = function() {
if(this.destination_distance > 300) {
this.current_speed += 0.002;
} else {
this.current_speed -= 0.002;
}
if(this.current_speed < this.min_speed) {
this.current_speed = this.min_speed;
}
if(this.current_speed > this.max_speed) {
this.current_speed = this.max_speed;
}
return this.current_speed;
};
这不起作用,因为如果距离小于300
它总是以min
速度加速,所以我想它应该以某种方式与distance
相关联变量。也许有人可以帮我解决这个问题。
答案 0 :(得分:0)
您需要计算目标速度和当前速度之间的差异,然后添加一些特定部分。
this.throttle = function() {
var target_speed = this.destination_distance > 300 ? this.max_speed : this.min_speed;
var diff = target_speed - this.current_speed;
this.current_speed += diff * laziness;
return this.current_speed;
};
laziness
介于0.0001
和1
之间;值越大,速度变化越快。
答案 1 :(得分:0)
您的起始速度是您的最低速度,因此如果起始距离<300,则圆圈将永远不会加速或减速。为什么不让你的起跑速度取决于初始距离?这是一个粗略的实现
private void uploadFile(String selectedFilePath) {
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.show();
Log.d("enter", "Entering" + selectedFilePath);
if (selectedFilePath.equals("null")) {
selectedFilePath = "";
}
int serverResponseCode = 0;
HttpURLConnection connection;
DataOutputStream dataOutputStream;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File selectedFile = new File(selectedFilePath);
String[] parts = selectedFilePath.split("/");
final String fileName = parts[parts.length - 1];
if (!selectedFile.isFile()) {
// dialog.dismiss();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//tvFileName.setText("Source File Doesn't Exist: " + selectedFilePath);
}
});
} else {
try {
FileInputStream fileInputStream = new FileInputStream(selectedFile);
URL url = new URL(SERVER_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);//Allow Inputs
connection.setDoOutput(true);//Allow Outputs
connection.setUseCaches(false);//Don't use a cached Copy
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("image", fileName);
connection.setRequestProperty("proof_file", fileName);
//creating new dataoutputstream
dataOutputStream = new DataOutputStream(connection.getOutputStream());
String aID = "10";
Log.e("Sending", "Sending userID");
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"a_id\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes(aID);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
String saID = "1";
Log.e("Sending", "Sending SocietyID");
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"s_id\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes(saID);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes(name);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"mobile\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes("38398393");
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"address\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes("abc");
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"h_type\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
// assign value
dataOutputStream.writeBytes("ty");
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
//..............................
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"proof_file\";filename=\""
+ fileName + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\""
+ fileName + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(String.valueOf(destination));
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
//...................9/6..................
//returns no. of bytes present in fileInputStream
bytesAvailable = fileInputStream.available();
//selecting the buffer size as minimum of available bytes or 1 MB
bufferSize = Math.min(bytesAvailable, maxBufferSize);
//setting the buffer as byte array of size of bufferSize
buffer = new byte[bufferSize];
//reads bytes from FileInputStream(from 0th index of buffer to buffersize)
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//loop repeats till bytesRead = -1, i.e., no bytes are left to read
while (bytesRead > 0) {
//write the bytes read from inputstream
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
// Log.i(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);
Log.d("wer", serverResponseMessage);
//response code of 200 indicates the server status OK
if (serverResponseCode == 200) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// tvFileName.setText("File Upload completed.\n\n You can see the uploaded file here: \n\n" + "http://coderefer.com/extras/uploads/"+ fileName);
Toast.makeText(MainActivity.this,"Successfully uploaded to server",Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}
else {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
pd.dismiss();
}
//closing the input and output streams
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "File Not Found", Toast.LENGTH_SHORT).show();
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "URL error!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Cannot Read/Write File!", Toast.LENGTH_SHORT).show();
}
pd.dismiss();
}
}
答案 2 :(得分:0)
为什么不使用总距离的百分比?这样你就可以避免使用300的硬编码阈值,它应该适用于任意长度的距离(通过调整一些速度)。你改变的功能看起来像这样:
this.throttle = function() {
if(this.destination_distance > 0.5 * total_distance) {
this.current_speed += 0.002;
} else {
this.current_speed -= 0.002;
}
if(this.current_speed < this.min_speed) {
this.current_speed = this.min_speed;
}
if(this.current_speed > this.max_speed) {
this.current_speed = this.max_speed;
}
return this.current_speed;
};
这里我认为total_distance是从开始到最终目的地的距离。这样,圆圈将以增加的速度行进大约一半,而另一半以减小的速度行进。如果用let 0.8替换0.5,那么前80%的速度会增加,而路径的最后20%的速度会减少。