Bonjour, je suis en train de travailler sur un algorithme permettant de savoir si l'utilisateur réalise avec son doigt un cercle. Cependant en fonction du sens de rotation trigonométrique exercé par le doigt, le programme doit réagir différemment. Or je n'arrive pas au résultat escompté car la variable diff peut être négative au lieu de rester positive et vice versa. Je croyais que en transformant les angles en degrés j'allais résoudre le problème mais non...
Je cale depuis une semaine sur ce problème.
Voici mon algorithme en Objective-C:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 -(void) sensFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint { float dist=[self distanceBetweenTwoPoints:fromPoint toPoint:toPoint]; if (dist>20) { self.previousLocation=toPoint; //We verify if we do an arc or a cercle CGFloat rayPrevious=[self distanceBetweenTwoPoints:centerView toPoint:fromPoint]; CGFloat rayNext=[self distanceBetweenTwoPoints:centerView toPoint:toPoint]; //With this value if rayNext=rayPrevious we sure that the result is good and we don't a useless test float rateOfTolerance=0.76f; if (rayNext>rayPrevious) { rateOfTolerance=rayPrevious/rayNext; } else { rateOfTolerance=rayNext/rayPrevious; } //The mouvement looks like a cercle or an arc //NSLog(@"%f",rateOfTolerance); if (rateOfTolerance>0.75f) { //We transform the coordinate with the new system //Now, we needn't to compute opposite and adjacent for the cosinus if(fromPoint.x<self.centerView.x) { fromPoint.x=-(self.centerView.x-fromPoint.x); } else { if (fromPoint.x>self.centerView.x) { fromPoint.x=fromPoint.x-self.centerView.x; } else { fromPoint.x=0; } } if(toPoint.x<self.centerView.x) { toPoint.x=-(self.centerView.x-toPoint.x); } else { if (toPoint.x>self.centerView.x) { toPoint.x=toPoint.x-self.centerView.x; } else { toPoint.x=0; } } //cosinus inverse float hypotenuse=(rayPrevious+rayNext)/2; float degreePrevious=RADIANS_TO_DEGREES(acosf(fromPoint.x/hypotenuse)); float degreeNext=RADIANS_TO_DEGREES(acosf(toPoint.x/hypotenuse)); if (fromPoint.y>self.centerView.y) { degreePrevious=180+(180-degreePrevious); } if (toPoint.y>self.centerView.y) { degreeNext=180+(180-degreeNext); } float diff; if (degreeNext<90 && degreePrevious>270) { diff=1; } else { if(degreePrevious<90 && degreeNext>270) { diff=-1; } else { diff=degreeNext-degreePrevious; } } //FAIRE TELLE ACTION SI DIFF EST NEGATIF OU TELLE AUTRE ACTION SI POSITIF } } }
Partager