2011-05-06 12 views
10

Şimdi bir süredir bununla uğraşıyor ve biraz yardıma ihtiyacım var. Ekranımın merkezinden (160,200) 14,4 derece ayrımda 25 satır çizmek istiyorum.Core Graphics - bir açı ile bir çizgi çizme

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 

// Çiz Inner Circle

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);  
Dış Daire Çiz - x 14.4 derecelik çarpanı nerede olduğunu döngü için bu kod satırını kullanıyoruz

// Çizgiler Çizelgesi

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(); 
    }   

(Burada bir resim göndermeyi planladım ama bana izin vermeyecek!)

Birisi lütfen trig işlevlerimi düzeltebilir mi? Bu saat 12:00 ile 24:00 arasında saat yönünde 25 satır çizmektir. Bunun yerine sadece 90 derece geri çekiliyor, sonra geri dönüyor, tüm hatlar aynı zamanda uzunluktadır.

Çok minnettar

Üstü istendiği gibi yedekte dış çevreleri ve iç kesimleri çizmek için kullanılan kod büyük bir örnektir. Bir sonraki fotoğrafı yüklemeyi deneyeceğim.

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

olması gerektiği:

enter image description here

+1

Trigonometri iyi görünüyor, bize daha fazla kod gösterebilir misiniz? X'yi tekrarlayan döngü, vb. –

+0

Gustav haklı. Ayrıca, görüntüyü başka bir yere yükleyebilir ve ona bir bağlantı sağlayabiliyorsanız, yeterli itibara sahip biri, yayınınızı düzenlemek için yayınınızı düzenleyebilir. – Tommy

+0

Kodunuzu yeniden düzenlerim ama çok fazla ... yeniden biçimlendirebilir misiniz lütfen okunabilir mi? Bunun dışında kasıtlı olarak gizlemeye çalışın :) –

cevap

16

asıl sorununuz kod satırı olan özgün olarak yazdığı satırlar 0 göreli çizilebilir edildi gibi

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

, 0 ama orta noktanıza göre çizmek istiyorsunuz (160, 200).

sonraki 2 satır da biraz şüpheli şunlardır: Benim için net değil

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

burada ne yapmaya çalıştığını, ancak bu her kolun ucunda bir çizgi çizmeye sona ereceğini sabit bir nokta (200,65) ve oradan da size merkez noktası, geri döndüğünüz şey kesinlikle değil! Bu düzeltmeler ekran bu gibi bir şey olacaktır yaparsanız bu

açıktır ... "silah" doğru çekiliyor teyit ve oradan götürün, bu satırları yorum

Umut deneyin: Screenshot

+0

@Frank, stackoverflow'a hoş geldiniz!Önerilen düzenlemeniz muhtemelen bu yanıtla ilgili bir yorum veya sorunuzla ilgili bir yorum olmalıdır. :) Diğer insanların sorularını ve cevaplarını düzenlemek gayet iyi, ancak noktaları netleştirmek, yazım hatalarını düzeltmek vb. Çok hoştur. Düzenlemelerle yapılan bir konuşmanın yapılması, okunamayan içeriğe hızlı bir şekilde yol açar. (Hiç [dünyanın ilk wiki'sini] okumaya çalışın (http://c2.com/cgi/wiki?WelcomeVisitors)? Anlaşılmaz. :) – sarnold

İlgili konular