我想做android应用来计算android上画布的距离点。基本上我有4个点,(x1,y1)(x2,y2),(x3,y3) and (x4,y4).
我想要做的是计算最大比率长度,D1比最小长度,D2 ..对于D1,我需要计算两点之间的距离(x1,y1)(x2,y2)
对于D2,我需要为点计算另一个点(x3,y3)(x4,y4)
..的距离,它只是用户输入。用户将放置该值并直接计算比率D1 / D2。
有人能帮助我吗?我知道公式,但不知道如何做这个功能。
public class MainActivity5 extends Activity implements OnClickListener {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
EditText editText5;
EditText editText6;
EditText editText7;
EditText editText8;
Button button1;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
// find the elements
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
EditText editText3 = (EditText) findViewById(R.id.editText3);
EditText editText4 = (EditText) findViewById(R.id.editText4);
EditText editText5 = (EditText) findViewById(R.id.editText4);
EditText editText6 = (EditText) findViewById(R.id.editText5);
EditText editText7 = (EditText) findViewById(R.id.editText6);
EditText editText8 = (EditText) findViewById(R.id.editText7);
button1 = (Button) findViewById(R.id.button1);
tvResult = (TextView) findViewById(R.id.tvResult);
// set a listener
button1.setOnClickListener((OnClickListener) this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String x1 = editText1.getText().toString();
String y1 = editText2.getText().toString();
String x2 = editText3.getText().toString();
String y2 = editText4.getText().toString();
String x3 = editText5.getText().toString();
String y3 = editText6.getText().toString();
String x4 = editText7.getText().toString();
String y4 = editText8.getText().toString();
//calculating distance
double dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1- y2, 2));
//what should i put here
}
答案 0 :(得分:0)
您需要解析字符串,然后才能将它们传递给Math
方法。可以使用try-catch
块来检查无效的数字个案。
public void onClick(View arg0) {
try {
double x1 = Double.parseDouble(editText1.getText().toString());
double y1 = Double.parseDouble(editText2.getText().toString());
double x2 = Double.parseDouble(editText3.getText().toString());
double y2 = Double.parseDouble(editText4.getText().toString());
double x3 = Double.parseDouble(editText5.getText().toString());
double y3 = Double.parseDouble(editText6.getText().toString());
double x4 = Double.parseDouble(editText7.getText().toString());
double y4 = Double.parseDouble(editText8.getText().toString());
//calculating distance
double d1 = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1- y2, 2));
double d2 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3- y4, 2));
tvResult.setText(Double.toString(d1/d2));
} catch (NumberFormatException e) {
}
}