TAIR Galili:
私はと呼ばれるクラスを持っているLine
と私はラインの接線を計算する方法を追加します。- /(×2 - ×1)(x1、y1)と(x2、y2)とその接線がされるので、(Y1、Y2):タンジェントを計算するために、私は2つの点を必要とします。軸) -私は2つの点を持っていると私は、接線(X2 = X1)に定義されていないことを特定の状況があるタンジェントを計算すると仮定し、線は、Yに平行です。私はこの機能を使用できるようにしたいと、それは何の接線を持たない行を取得するときに、プログラムがクラッシュすると、単にエラーが表示されません。どうすればいいのですか?
これは、(それが呼ばれるクラスにある関数ですLine
):
double getTangent() {
// defined only if the line is not perpendicular to the horizontal axis.
return (this.end.getY() - this.start.getY()) / (this.end.getX() - this.start.getX());
}
アブラ:
これは許容できますか?
double getTangent() {
if (this.end.getY() == this.start.getY()) {
return 0; // line is parallel to x-axis
}
else if (this.end.getX() == this.start.getX()) {
return Double.NaN; // line is parallel to y-axis, i.e. undefined
}
else {
return (this.end.getY() - this.start.getY()) / (this.end.getX() - this.start.getX());
}
}