2010-03-26 7 views
2

J'ai ce code:Dessiner flèche sur la ligne

CGPoint arrowMiddle = CGPointMake((arrowOne.x + arrowTo.x)/2, (arrowOne.y + arrowTo.y)/2); 

CGPoint arrowLeft = CGPointMake(arrowMiddle.x-40, arrowMiddle.y); 
CGPoint arrowRight = CGPointMake(arrowMiddle.x, arrowMiddle.y + 40); 

[arrowPath addLineToScreenPoint:arrowLeft]; 
[arrowPath addLineToScreenPoint:arrowMiddle]; 
[arrowPath addLineToScreenPoint:arrowRight]; 
[[mapContents overlay] addSublayer:arrowPath]; 
[arrowPath release]; 

avec cette sortie:

http://img517.yfrog.com/img517/7690/schermafbeelding2010032.png

Que dois-je ajouter pour obtenir la gauche et à droite le au même degré de la ligne + 30 °.

Si quelqu'un a l'algorithme de dessiner une flèche sur une ligne, les moyens le donnent. Peu importe ce langage de programmation est ...

Merci

Répondre

3

Voici ce que vous faites. Tout d'abord, prenez le vecteur de la ligne et normalisez-le en le divisant par sa longueur - cela vous donnera un vecteur de longueur 1 pointant dans la direction de la ligne. Ensuite, multipliez-le par la longueur dont vous avez besoin. Tournez-le de 120 ° et -120 ° pour faire la flèche. Enfin, le compenser par les coordonnées où vous voulez qu'il soit. Voici à quoi cela ressemblerait dans le code:

// calculate the position of the arrow 
CGPoint arrowMiddle; 
arrowMiddle.x = (arrowOne.x + arrowTo.x)/2; 
arrowMiddle.y = (arrowOne.y + arrowTo.y)/2; 

// create a line vector 
CGPoint v; 
v.x = arrowTo.x - arrowOne.x; 
v.y = arrowTo.y - arrowOne.y; 

// normalize it and multiply by needed length 
CGFloat length = sqrt(v.x * v.x + v.y * v.y); 
v.x = 40 * (v.x/length); 
v.y = 40 * (v.y/length); 

// turn it by 120° and offset to position 
CGPoint arrowLeft = CGPointApplyAffineTransform(v, CGAffineTransformMakeRotation(3.14 * 2/3)); 
arrowLeft.x = arrowLeft.x + arrowMiddle.x; 
arrowLeft.y = arrowLeft.y + arrowMiddle.y; 

// turn it by -120° and offset to position 
CGPoint arrowRight = CGPointApplyAffineTransform(v, CGAffineTransformMakeRotation(-3.14 * 2/3)); 
arrowRight.x = arrowRight.x + arrowMiddle.x; 
arrowRight.y = arrowRight.y + arrowMiddle.y; 
+0

perrrrrrrrrrrrf !!!! – SpaceDog

1

Merci de répondre! En attendant j'ai trouvé aussi une solution.

Il est comme ça:


double slopy , cosy , siny; 
       double Par = 10.0; //length of Arrow (>) 
       slopy = atan2((arrowOne.y - arrowTo.y), 
           (arrowOne.x - arrowTo.x)); 
       cosy = cos(slopy); 
       siny = sin(slopy); //need math.h for these functions

  CGPoint arrowMiddle = CGPointMake((arrowOne.x + arrowTo.x)/2, (arrowOne.y + arrowTo.y)/2); 
      [arrowPath addLineToScreenPoint:arrowMiddle]; 

      CGPoint arrowLeft = CGPointMake(arrowMiddle.x + round(- Par * cosy - (Par/2.0 * siny)), arrowMiddle.y + round(- Par * siny + (Par/2.0 * cosy))); 
      [arrowPath addLineToScreenPoint:arrowLeft]; 

      CGPoint arrowRight = CGPointMake(arrowMiddle.x + round(- Par * cosy + (Par/2.0 * siny)),arrowMiddle.y - round(Par/2.0 * cosy + Par * siny)); 
      [arrowPath addLineToScreenPoint:arrowRight]; 

      [arrowPath addLineToScreenPoint:arrowMiddle]; 
      [[mapContents overlay] addSublayer:arrowPath]; 
      [arrowPath release]; 

Le seul problème ici est que je dessine comme il est un rmpath (cadre voie-moi) et que la flèche se agrandit/petit lorsque vous effectuez un zoom /en dehors.

Mais merci de répondre, je vais regarder dans quel code est le plus performant.

+0

parfait, c'est ce que je cherchais toute la journée. Merci mec. – Bakhtiyor

Questions connexes