# How to stretch and rotate an image between two points in cocos2d?

I have an arrow image. I want to stretch this image between two given points. I'm retrieving the points from touch locations, so they aren't fixed. When the user touches the iPad, the arrow image should appear and be shown until the touch ends. So if the user touches point A and is moving his finger to point B, the arrow image will be stretched from point A to point B and so on.

How can I achieve that?

-

## 2 Answers

I'm writeing this code in c++ since I'm working with cocos2d-x but you just have to translate this code to Obj-c:

``````ccPoint a,b;

image->setposition(ccp((a.x + b.x) / 2, (a.y + b.y)/2));
image->setrotation(atan2(a.y-b.y,a.x-b.x));

float dist = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
dist = dist / image->boundingbox().size.width;

image->setscale(dist);
``````
-

First you need to compute the distance between points A and B, then you have stretch the arrow to this value. After that compute the angle between A and B, and rotate arrow with this.

The last operation is positioning the arrow. If origin of the picture is in the center, you must place it at the (A+B)/2 point.

-