2014-09-21 28 views
1

我環顧四周,看起來很像它,但它們總是使用變量X1,X2和Y1 Y2,並且im不允許要做到這一點。 對於分配我有2班,讓我們調用這些A和B從點對象獲取值,用作Java中的圓對象的中心

Class A 
    //Punt (x,y) 
    Punt mp1 = new Punt(1.0, 2.0) 
    Punt mp2 = new Punt(3.0, 4.0) 

    //Circle(center, radius) 
    Circle c1 = new Circle(mp1, 1.0) 
    Circle c2 = new Circle(mp1, 1.0) 

現在,在B類,我需要看是否圓重疊,所以我想看看距離beweteen中心點< radius1的+ radius2。我必須公開布爾重疊(Circle that)

Class B 
    private Punt center 
    private double radius 
    public Circle(Punt mp, double ra) 
     center = mp 
     radius = ra 

    public boolean overlap(Circle that) 
     //here I would need to find the distance between the distance of the centers with Pythagorean theorem 
     double sumRadius = this.radius + that.radius //this one works 
    if (distCenter <= sumRadius) 
     return true 
    else 
     return false; 

我試過比我想象的更多,但沒有任何工作,任何提示?

林不允許只是讓X1和X2,開創A級等

+0

編輯您的帖子更清晰......而使用Java語法 – 2014-09-21 14:08:26

回答

0

你Circle類肯定有getRadius()和getCenter信息getX1()()方法,對嗎?得到中心值並計算歐氏距離,然後與半徑之和進行比較。實際上,您甚至不需要getCenter方法,因爲您可以直接訪問中心點,兩個圓圈的Punt字段,this圓圈和that圓圈。需要注意的是歐氏距離是你發現的公式 -

Math.sqrt(deltaX * deltaX + deltaY * deltaY) 

其中DELTAX是兩個圓心X值同樣爲移動deltaY的差異和。

您需要向我們展示您的Punt對象。我必須假設你可以從他們那裏得到x和y的值,這就解決了你的問題。即center.getX()center.getY()

+0

我沒有getRadius()和getCenter()方法(IM甚至不允許)。我知道Eucildian距離,因爲我不是英國本地人,我已經學會了Pythagoras。關於deltaX和deltaY,這是整個點,我似乎無法得到這些X和Y值,我不能用這個減去Punt對象。然後。 – Zouter 2014-09-21 14:21:42

+0

@Zouter:你沒有向我們展示Punt類的代碼,但它肯定有提取它的x和y值的方法。 – 2014-09-21 14:23:35

+0

當然!我爲不同的作業做了平底船班。我想我可以使用它。我會試着去處理這個問題,看看它是否有效。我只在編碼3周,請原諒我的無知。 - 編輯它與this.center.getX()確實在Puntclass中工作! – Zouter 2014-09-21 14:36:01

0

對於初學者,你不能訪問Circle對象中的變量,因爲它們是公共的。您可以創建獲取者或設置正確的visibility

,那麼你可能可以做這樣的事情:

public boolean overlap(Circle other) { 
    Punt otherCenter = other.getPunt(); 
    double distance = Math.sqrt(Math.pow(Math.abs(otherCenter.x - center.x), 2) + 
     Math.pow(Math.abs(otherCenter.y - center.y), 2)); 

    return distance < (radius + other.getRadius()); 
} 

我不能guarantuee這會工作,但我認爲這將至少是指向你到正確的方向。