2012-01-07 8 views
7

Con esta fórmula llegué ángulonúcleo gráfico giran rectángulo

double rotateAngle = atan2(y,x) 

con este código Puedo dibujar un rectángulo

CGRect rect = CGRectMake(x,y , width ,height); 
CGContextAddRect(context, rect); 
CGContextStrokePath(context); 

¿Cómo puedo girar el rectángulo alrededor del ángulo?

Respuesta

27

Así es como usted haría lo siguiente:

CGContextSaveGState(context); 

CGFloat halfWidth = width/2.0; 
CGFloat halfHeight = height/2.0; 
CGPoint center = CGPointMake(x + halfWidth, y + halfHeight); 

// Move to the center of the rectangle: 
CGContextTranslateCTM(context, center.x, center.y); 
// Rotate: 
CGContextRotateCTM(context, rotateAngle); 
// Draw the rectangle centered about the center: 
CGRect rect = CGRectMake(-halfWidth, -halfHeight, width, height); 
CGContextAddRect(context, rect); 
CGContextStrokePath(context); 

CGContextRestoreGState(context); 
+0

THX su ayuda! funcionó bien! – user1125890

+0

¡Ningún problema! ¡Agradecería que marcaras mi respuesta como aceptada! Gracias. – Steve

+4

Perfecto. Un mod debe marcar esto como aceptado, ya que user1125890 aparentemente tiene cosas mejores que hacer. Nunca podría haberlo descubierto yo mismo. ¡Gracias! – Accatyyc

Cuestiones relacionadas