2011-05-06 60 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