2011-05-06 63 views
10

我一直在尋找這個掙扎一段時間,需要一些幫助。我只想以14.4度的間隔從顯示屏中心(160,200)畫出25條線。我已經使用這行代碼在for循環中,其中x是14.4度乘數 -核心圖形 - 在一個角度畫一條線

UIImage*backgroundImage = [UIImage imageNamed:@"Primate Background Only.png"]; 
[backgroundImage drawInRect:CGRectMake(0, 0, 320, 480)]; 

//繪製外圓

rect = CGRectMake(0.0, 35.0, 320.0, 320.0);  
CGContextRef contextRef = UIGraphicsGetCurrentContext();    // Get the contextRef 
CGContextSetLineWidth  (contextRef, 0.5);     // Set the border width 
CGContextSetRGBFillColor (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.05f);    // Set the circle fill color to GREEN 
CGContextSetRGBStrokeColor (contextRef, 0.0, 0.0, 0.0, 0.2);   // Set the circle border color to BLACK  
CGContextFillEllipseInRect  (contextRef, rect);     // Fill the circle with the fill color 
CGContextStrokeEllipseInRect (contextRef, rect);     // Draw the circle border 

//繪製內圓

rect = CGRectMake(25.0, 60.0, 270.0, 270.0);      // Get the contextRef 
CGContextSetLineWidth  (contextRef, 0.5);     // Set the border width 
CGContextSetRGBFillColor (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.25f); 
CGContextSetRGBStrokeColor (contextRef, 0.0, 0.0, 0.0, 0.2);   // Set the circle border color to BLACK  
CGContextFillEllipseInRect  (contextRef, rect);     // Fill the circle with the fill color 
CGContextStrokeEllipseInRect (contextRef, rect);  

// Draw Segments

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, 0.0, 0.0);   
    for (x=1; x<26; x++) { 

     CGContextSetLineWidth  (context, 0.5); 
     CGContextSetRGBStrokeColor (context, 0.0, 0.0, 0.0, 0.25); 
     CGContextMoveToPoint  (context, 160, 200);     
     CGContextAddLineToPoint  (context, (160.0 * (cos((x*14.4)*(M_PI/180)))), (160.0 * (sin((x*14.4)*(M_PI/180)))));      // The angle is in degrees 
     CGContextAddLineToPoint  (context, 200, 65); 
     CGContextAddLineToPoint  (context, 160, 200);    
     CGContextStrokePath(context);       // Why is this not showing both line color and infill? 
     CGContextSetFillColorWithColor (context, [UIColor whiteColor].CGColor);  
     CGContextFillPath   (context); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
    }   

(我打算在這裏發佈圖片,但它不會允許我!)

有人可以糾正我的trig函數。這意味着從12:00點到24點順時針畫25條線。相反,它只能回退90度,然後返回,所有線路的長度也是一樣。

非常感謝

以上是用於繪製絲束外界和內部片段的請求的代碼的一個更大的樣品。我會嘗試下一步上傳圖片。

enter image description here

+1

三角法看起來很好,你能告訴我們更多的代碼嗎?迭代'x'的循環等。 – 2011-05-06 16:03:04

+0

古斯塔夫的權利。此外,如果您可以在其他地方上傳圖片並提供鏈接,那麼具有足夠聲譽的人可以編輯您的帖子,以便將其放入內聯。 – Tommy 2011-05-06 16:44:26

+0

我想重新格式化你的代碼,但它太多了...你可以重新格式化它,所以它是可讀的嗎?除非你試圖混淆它故意:) – 2011-05-06 17:32:27

回答

16

你的主要問題是這行代碼:

CGContextAddLineToPoint(context, (160.0 * (cos((x*14.4)*(M_PI/180)))), 
           (160.0 * (sin((x*14.4)*(M_PI/180))))); 

它應該是:

CGContextAddLineToPoint(context, 160 + (160.0 * (cos((x*14.4)*(M_PI/180)))), 
           200 + (160.0 * (sin((x*14.4)*(M_PI/180))))); 

當你最初寫它的品系相對於0繪製,0但你想繪製相對於你的中心點(160,200)。

接下來的2線也有點可疑:

 CGContextAddLineToPoint  (context, 200, 65); 
     CGContextAddLineToPoint  (context, 160, 200); 

目前尚不清楚對我有什麼你想在這裏做,但是這最終會從每個臂的末端畫一條線一個固定點(200,65),並從那裏回到你的中心點,這幾乎肯定不是你想要的!嘗試註釋掉這些行,確認「武器」被正確地繪製,並從那裏......

希望這是明確的

如果你把這些更正你的屏幕看起來是這樣的: Screenshot

+0

@Frank,歡迎來到stackoverflow!你提出的編輯應該只是對這個答案的評論,或者對你的問題發表評論。 :)編輯他人的問題和答案是好的,但是如果他們澄清問題,糾正錯別字等,這很好。通過編輯進行對話很快就會導致無法閱讀的內容。 (曾嘗試閱讀[世界上第一個wiki](http://c2.com/cgi/wiki?WelcomeVisitors)?難以理解。:) – sarnold 2011-05-07 04:03:16