2012-01-07 10 views
7

Z tego wzoru dostałem kątrdzeń graficzny obrócić prostokąt

double rotateAngle = atan2(y,x) 

z tym kodem mogę narysować prostokąt

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

Jak mogę obrócić prostokąt wokół kąta?

Odpowiedz

27

Oto jak chcesz to zrobić:

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 pomoc! działało dobrze! – user1125890

+0

Nie ma problemu! Byłbym wdzięczny, jeśli oznaczyłeś moją odpowiedź jako zaakceptowaną! Dzięki. – Steve

+4

Idealny. Mod powinien oznaczyć to jako zaakceptowane, ponieważ użytkownik1125890 najwyraźniej ma lepsze rzeczy do zrobienia. Nigdy bym tego nie wymyślił. Dziękuję Ci! – Accatyyc