2010-11-02 17 views
7

Ho bisogno di calcolare l'angolo tra le linee. Devo calcolare atan. Quindi io sto usando questo codiceaiuto per calcolare correttamente atan2

static inline CGFloat angleBetweenLinesInRadians2(CGPoint line1Start, CGPoint line1End) 
{ 
    CGFloat dx = 0, dy = 0; 

    dx = line1End.x - line1Start.x; 
    dy = line1End.y - line1Start.y; 
    NSLog(@"\ndx = %f\ndy = %f", dx, dy); 

    CGFloat rads = fabs(atan2(dy, dx)); 

    return rads; 
} 

Ma non riesco a oltre 180 gradi ((Dopo 179 gradi che va 178..160..150 e così via.

ho bisogno di ruotare su 360 ° . Come posso fare che cosa c'è che non va

maby questo aiuta:??

//Tells the receiver when one or more fingers associated with an event move within a view or window. 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSArray *Touches = [touches allObjects]; 
    UITouch *first = [Touches objectAtIndex:0]; 

    CGPoint b = [first previousLocationInView:[self imgView]]; //prewious position 
    CGPoint c = [first locationInView:[self imgView]];   //current position 

    CGFloat rad1 = angleBetweenLinesInRadians2(center, b); //first angel 
    CGFloat rad2 = angleBetweenLinesInRadians2(center, c); //second angel 

    CGFloat radAngle = fabs(rad2 - rad1);   //angel between two lines 
    if (tempCount <= gradus) 
    { 
     [imgView setTransform: CGAffineTransformRotate([imgView transform], radAngle)]; 
     tempCount += radAngle; 
    } 

} 

risposta

5

rimuovere la chiamata fabs e semplicemente fanno:

CGFloat rads = atan2(dy, dx); 
+0

Lo provo. Non funziona – yozhik

+0

@yozhik: Forse dovresti ex chiaro cosa non funziona. Qual è il risultato atteso e cosa stai vedendo? – casablanca

+0

Ho un sistema di coordinate del decartatore. Su cui sto proiettando un'immagine. C'è l'imaging zero. Quando prendo e muovo la mia foto in alto di 180 gradi - tutto allrigth. Quando cerco di spostarmi di 180 gradi, ad esempio 190, mi mostra 170 gradi. Ho bisogno di lì dove 190 gradi. Vedete ... – yozhik

7

atan2 restituisce i risultati in [-180.180] (o -pi, pi in radianti) Per ottenere risultati da 0.360 uso.:

float radians = atan2(dy, dx); 
if (radians < 0) { 
    radians = TWO_PI + radians; 
} 

Va notato che è tipico di esprimere rotazioni in [-pi, pi] e questa convenzione si può semplicemente utilizzare il risultato di atan2 senza preoccuparsi del segno.

+0

non funzionano a :( – yozhik

+0

ho modificato la mia domanda, maby aiuta a ottenere che cosa è sbagliato. – yozhik

+0

Che didn' Suppongo che tu abbia sostituito una costante corretta anche per 'TWO_PI'. –

0

Usare questa funzione a Swift. Ciò assicura che l'angolo da "fromPoint" a "toPoint" sia compreso tra 0 e < 360 (escluso 360). Notare che la seguente funzione presuppone che CGPointZero sia nell'angolo in alto a sinistra.

func getAngle(fromPoint: CGPoint, toPoint: CGPoint) -> CGFloat { 
    let dx: CGFloat = fromPoint.x - toPoint.x 
    let dy: CGFloat = fromPoint.y - toPoint.y 
    let twoPi: CGFloat = 2 * CGFloat(M_PI) 
    let radians: CGFloat = (atan2(dy, -dx) + twoPi) % twoPi 
    return radians * 360/twoPi 
} 

Per il caso in cui l'origine è nell'angolo in basso a sinistra

let twoPi = 2 * Float(M_PI) 
let radians = (atan2(-dy, -dx) + twoPi) % twoPi 
let angle = radians * 360/twoPi