计算 canvas 点在 android 中的距离
calculate the distance of point of canvas in android
我想做 android 个应用程序来计算 android 上 canvas 的距离点。基本上我有 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
}
您需要先解析字符串,然后才能将它们传递给 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) {
}
}
我想做 android 个应用程序来计算 android 上 canvas 的距离点。基本上我有 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
}
您需要先解析字符串,然后才能将它们传递给 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) {
}
}