我正在撰寫代碼,目的是利用一個完全直角三角形的一個給定點來尋找其余兩個點。在這個練習中,我們假設它是一個這樣的三角形。righttriangle
第一段代碼使用Point2D類來建立左下角的點,就像這樣:
public Triangle(Point2D. Double bottomLeftPoint, double height, double base) {
this.base = base;
this.height = height;
this.bottomLeftPoint = bottomLeftPoint;
}
public Point2D.Double getBottomLeftTrianglePoint(){
return this.bottomLeftPoint;
我知道在數學上,三角形的頂點會有相同的x值,但會有由高度增加的y值。同樣,右下角的點將有相同的y值,但x值會被基數加上。
我的問題是,出于方法上的考慮,我應該如何構建這個結構?
是否可以這樣:
public Point2D.Double getTopTrianglePoint() {
return this.bottomLeftPoint(x, y this.height)。
}
public Point2D.Double getBottomRightTrianglePoint(){
return this.bottomLeftPoint(x this.base, y) 。
}
為了進一步了解情況,我有一個單獨的類,目的是用測驗三角形來測驗這些方法:
public class TriangleTest {
private Triangle triangle01;
public TriangleTest() {
this.triangle01 = new Triangle(new Point2D. Double(1, 2), 5, 3)。)
}
希望得到任何幫助。謝謝!
uj5u.com熱心網友回復:
return this.bottomLeftPoint(x, y this.height);
分解一下,然后你會發現這沒有意義。this.bottomLeftPoint是一個Point2D.Double型別的變數。然后你......試圖以某種方式將其視為一個方法。它不是。這是行不通的。
你想創建一個全新的Point2D.Double。new Point2.Double(x, y)像往常一樣;因此:
return new Point2D. Double(x, y this.height)。
當然,如果你這樣嘗試,編譯器會告訴你這也不行;編譯器不知道x是什么意思。那么,你打算在那里使用什么呢?很明顯,它是你的this.bottomLeftPoint欄位所參考的Point2D.Double物件的x坐標。它有一個.getX()方法。所以:
return new Point2D.Double(bottomLeftPoint.getX(), bottomLeftPoint.getY() height)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/331095.html
標籤:
