2010-11-02 15 views
7

Necesito calcular el ángulo entre líneas. Necesito calcular atan. Así que estoy usando dicho códigoayuda para calcular atan2 correctamente

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

Pero no puedo conseguir más de 180 grados ((Después de 179 grados va 178..160..150 y así sucesivamente.

necesito para girar en 360 grados . ¿Cómo puedo hacerlo ¿Qué pasa

maby esto ayuda:??

//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; 
    } 

} 

Respuesta

5

quitar la llamada fabs y simplemente hacen que sea:

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

Lo intento. No trabajo – yozhik

+0

@yozhik: Tal vez deberías ex claro lo que no funciona. ¿Cuál es el resultado esperado y qué estás viendo? – casablanca

+0

Tengo un sistema de coordenadas decarta. En lo que estoy proctuando imagen. Hay imagen cero. Cuando tomo y muevo mi foto en los 180 grados superiores, todo al rededor. Cuando intento mover un ander 180 grados, por ejemplo 190, me muestra 170 grados. Necesito que haya 190 grados. Verá ... – yozhik

7

atan2 devuelve los resultados en [-180,180] (o-pi, pi en radianes) Para obtener los resultados de 0360 su uso.:

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

Cabe señalar que es típica de expresar rotaciones en [-pi, pi] y de ahí que sólo puede utilizar el resultado de atan2 sin preocuparse de la señal.

+0

no funcionan a :( – yozhik

+0

he editado mi pregunta, maby que ayuda a obtener cuál es incorrecto. – yozhik

+0

Lo que didn' ¿trabajo? Supongo que también ha sustituido una constante adecuada para 'TWO_PI' –

0

Utilice esta función en Swift. Esto asegura que el ángulo desde "fromPoint" a "toPoint" aterrice entre 0 a < 360 (sin incluir 360). Tenga en cuenta que la siguiente función asume que CGPointZero se encuentra en la esquina superior izquierda.

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 
} 

Para el caso en que el origen se encuentra en la esquina inferior izquierda

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