2011-05-06 15 views
10

He estado buscando problemas con esto desde hace un tiempo y necesito ayuda. Solo quiero dibujar 25 líneas desde el centro de mi pantalla (160,200) a una separación de 14.4 grados. He estado usando esta línea de código en un bucle donde x es el multiplicador de 14,4 grados -Gráficos básicos: dibujando una línea en un ángulo

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

// Dibujar Outer Circle

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 

// Dibujar círculo interior

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

// Dibujar segmentos

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

(¡Tenía la intención de publicar una imagen aquí, pero no me lo permite!)

¿Alguien puede por favor corregir mis funciones trigonométricas. Esto tiene como objetivo dibujar 25 líneas en el sentido de las agujas del reloj desde las 12:00 en punto hasta las 24:00. En su lugar, retrocede solo 90 grados y luego regresa, todas las líneas también tienen una longitud de salida.

muy agradecidos

Por encima es una muestra más grande del código utilizado para dibujar círculos exteriores de remolque y los segmentos interiores conforme a lo solicitado. Intentaré y subiré la imagen a continuación.

enter image description here

+1

La trigonometría se ve bien, ¿nos puede mostrar más de su código? El ciclo que itera 'x', etc. –

+0

Gustav tiene razón. Además, si puede cargar la imagen en otro lugar y proporcionarle un enlace, alguien con suficiente reputación puede editar su publicación para ponerla en línea. – Tommy

+0

Reformatearé tu código pero es demasiado ... ¿puedes formatearlo para que sea legible? Excepto que intentas ofuscarlo intencionalmente :) –

Respuesta

16

Su principal problema es esta línea de código:

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

que debe ser:

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

A medida que originalmente había escrito que las líneas eran se dibujado con relación a 0 , 0 pero desea dibujar en relación con su punto central (160, 200).

Los siguientes 2 líneas son también un poco sospechoso:

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

No me queda claro lo que estaba tratando de hacer aquí, pero esto podría terminar dibujando una línea desde el extremo de cada brazo para un punto fijo (200,65) y desde allí de regreso a su punto central, que es casi seguro que no es lo que pretendía. Intente comentar estas líneas, confirman que los "brazos" se dibujan correctamente y a partir de ahí ...

la esperanza que esto está claro

Si haces estas correcciones en su pantalla se verá algo como esto: Screenshot

+0

@Frank, bienvenido a stackoverflow!La edición propuesta probablemente sea solo un comentario sobre esta respuesta o un comentario sobre su pregunta. :) La edición de las preguntas y respuestas de otras personas está bien, pero es bueno si están aclarando puntos, corrigiendo errores tipográficos, etc. Llevar a cabo una conversación con ediciones rápidamente lleva a un contenido ilegible. (¿Alguna vez has intentado leer [la primera wiki del mundo] (http://c2.com/cgi/wiki?WelcomeVisitors)? Incomprensible. :) – sarnold

Cuestiones relacionadas