2011-08-27 32 views
0

我正在嘗試創建類似「Big Wheel」的價格。有點兒像這樣:在Android上獲得弧形或橢圓形點數

big wheel

我已經得到了左側的橢圓形,右弧頂部和底部的帖子進行。我迷失在哪裏是如何創造內在的道路而不訴諸一堆複雜的數學。我想,如果我能以某種方式獲得我形狀上的點數,我可以很容易地通過這種方式製作我的線條。然而,我沒有找到任何東西在Android SDK中給我這個。

有沒有什麼辦法可以在Android中獲取繪製對象的點?如果沒有,是否有更簡單的解決方案,我沒有看到?

在此先感謝!

編輯#1:

下面我後採取了重擊在數學和我似乎無法得到它的工作:(

這是我到目前爲止有:

  float a = (leftOval.bottom - leftOval.top)/2; 
      float b = (leftOval.right - leftOval.left)/2; 
      float x1 = (float) getXForOval(a, b, top + 50); 
      float y1 = top + 50; 
      float x2 = x1 + 50; 
      float y2 = y1; 

      Log.d("coords", "compute: " + getXForOval(a, b, top + 50) + ""); 

      Log.d("coords", "leftOval.top: " + leftOval.top + ""); 
      Log.d("coords", "leftOval.bottom: " + leftOval.bottom + ""); 

      Log.d("coords", "leftOval.right: " + leftOval.right + ""); 
      Log.d("coords", "leftOval.left: " + leftOval.left + ""); 

      Log.d("coords", "a: " + a + ""); 
      Log.d("coords", "b: " + b + ""); 
      Log.d("coords", "x1: " + x1 + ""); 
      Log.d("coords", "y1: " + y1 + ""); 
      Log.d("coords", "x2: " + x2 + ""); 
      Log.d("coords", "y2: " + y2 + ""); 

      canvas.drawLine(x1, y1, x2, y2, paint); 

    private double getXForOval(float a, float b, float y) { 
     // sqrt (a^2 * (1 - y^2/b^2)) 

     // @formatter:off 
     return Math.sqrt(
         Math.abs(
          Math.pow(a, 2) * ( 1 - 
                (Math.pow(y, 2)/Math.pow(b, 2)) 
               ) 
           ) 
         ); 

     // @formatter:on 
    } 

但X值是走出來的方法大。我在做什麼錯?

08-27 18:16:56.574: DEBUG/coords(2785): compute: 2743.647207641682 
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.top: 180.0 
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.bottom: 780.0 
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.right: 185.0 
08-27 18:16:56.584: DEBUG/coords(2785): leftOval.left: 135.0 
08-27 18:16:56.584: DEBUG/coords(2785): a: 300.0 
08-27 18:16:56.584: DEBUG/coords(2785): b: 25.0 
08-27 18:16:56.584: DEBUG/coords(2785): x1: 2743.6472 
08-27 18:16:56.584: DEBUG/coords(2785): y1: 230.0 
08-27 18:16:56.584: DEBUG/coords(2785): x2: 2793.6472 
08-27 18:16:56.584: DEBUG/coords(2785): y2: 230.0 

回答

1

數學不是非常複雜。橢圓的公式是x^2/a^2 + y^2/b^2 = 1其中a和b是長軸和短軸的長度,它們是常數。您需要爲sqrt(a^2 *(1 - y^2/b^2))給定的y找到x。這將使您獲得距離左側橢圓中心的x,y偏移量,並且該線的長度保持不變。使用正弦函數來動畫你的y值,它應該看起來不錯。

編輯:

對此深感抱歉,我對A和B註釋應該提到,你有他們逆轉。此外,您必須使用相對於橢圓中心的y座標。

將自己乘以數字比使用Math.pow快於平方時。

 // a should be half the width 
     float a = (leftOval.right - leftOval.left)/2; 
     // and b half the height 
     float b = (leftOval.bottom - leftOval.top)/2; 

     float yCenter = (leftOval.top + leftOval.bottom)/2; 
     float xCenter = (leftOval.right + leftOval.left)/2; 

     float x1 = (float) getXForOval(a, b, top + 150, xCenter, yCenter); 
     float y1 = top + 150; 
     float x2 = x1 + 50; 
     float y2 = y1; 

     Log.d("coords", 
       "compute: " + getXForOval(a, b, top + 50, xCenter, yCenter) 
         + ""); 

     Log.d("coords", "leftOval.top: " + leftOval.top + ""); 
     Log.d("coords", "leftOval.bottom: " + leftOval.bottom + ""); 

     Log.d("coords", "leftOval.right: " + leftOval.right + ""); 
     Log.d("coords", "leftOval.left: " + leftOval.left + ""); 

     Log.d("coords", "a: " + a + ""); 
     Log.d("coords", "b: " + b + ""); 
     Log.d("coords", "x1: " + x1 + ""); 
     Log.d("coords", "y1: " + y1 + ""); 
     Log.d("coords", "x2: " + x2 + ""); 
     Log.d("coords", "y2: " + y2 + ""); 

     canvas.drawLine(x1, y1, x2, y2, paint); 
    } 
} 

private double getXForOval(float a, float b, float y, float xCenter, 
     float yCenter) { 
    // sqrt (a^2 * (1 - y^2/b^2)) 

    // calculation based on values relative to the center 
    float yOffset = y - yCenter; 

    // @formatter:off 
    return Math.sqrt( 
        Math.abs( 
         a * a * ( 1 - 
               ((yOffset * yOffset)/(b * b)) 
              ) 
          ) 
        ) + xCenter; 
    // @formatter:on 
} 
+0

Android用矩形繪製橢圓,這是否意味着a是矩形的高度,b是寬度? +1 – javamonkey79

+0

@ javamonkey一半高度和一半寬度。 – IronMensan

+0

我嘗試了你的建議,我必須錯過一些東西... – javamonkey79

0

我不知道任何可以讓您獲取繪製對象點的調用。

我會寫一個函數來循環繪製多少行需要繪製,並且只需要一些數學運算來找到該行的xStart和xEnd座標。

+0

我該如何獲得起點和終點? – javamonkey79