2012-01-07 16 views
7

Con questa formula sono arrivato angoloGraphics Core ruotano rettangolo

double rotateAngle = atan2(y,x) 

con questo codice posso disegnare un rettangolo

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

Come posso ruotare il rettangolo intorno l'angolo?

risposta

27

Ecco come si farebbe che:

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 il vostro aiuto! ha funzionato bene! – user1125890

+0

Nessun problema! Lo apprezzerei se avessi contrassegnato la mia risposta come accettata! Grazie. – Steve

+4

Perfetto. Un mod dovrebbe contrassegnarlo come accettato, visto che user1125890 apparentemente ha cose migliori da fare. Non l'avrei mai capito da solo. Grazie! – Accatyyc

Problemi correlati