Take the 2-minute tour ×

I have a label-point on the line between two other points. Say its 20 pixels in from the first point of the line.

Now i want to move the first point of the line, taking the label-point with me, so i move the first point and redraw the line to the last point of the line. Works fine, but how do i move the label to the new line, putting it again 20px in from the left.

(the length 20px varies as a variable depending on where the label has been put on the line by a user).

I was thinking like using Y=kx+m cant get it right though…

This is what i've written so far:

    //return relative position of label on old line, relatively the first point.
    return [self length]-   sqrt(self.label.center.x*self.label.center.x+self.label.center.y*self.label.center.y);
    //return [self length]-sqrt((self.last.x - self.label.center.x)*(self.last.x -  self.label.center.x) + (self.last.y - self.label.center.y)*(self.last.y -      self.label.center.y));

    NSLog(@"Translating label length:%f", [self length]);
    double m= (self.last.y-self.first.y)/(self.last.x-self.first.x);
    double x= self.first.x+length/(sqrt(1+m*m));
    double y= x*m;
    NSLog(@"Moving label to:%f,%f", x,y);
    [self.label setCenter:CGPointMake(x, y)];


Position of label is meant as returning how far from the first point the label is placed.

Example of movement of the lines, and hence the two dots at the end of the line image

As you can see in the image, an arrow represents a line consisting of first and last dot. the distance between the redpoint and the first dot is preserved during the movement/translation of the arrow. This is however not working using my algorithm

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Use of Y=kx+m form is not a good idea (because of vertical lines), so parametric form is recommended:

enter image description here

Norm = Sqrt((X2-X1)*(X2-X1) + (Y2-Y1)*(Y2-Y1)) // check for 0
Dir_X = (X2 - X1) / Norm
Dir_Y = (Y2 - Y1) / Norm
Label_X = X1 + 20 * Dir_X
Label_Y = Y1 + 20 * Dir_Y
share|improve this answer
does the '-(double)positionOfLabel' look correct? –  David Karlsson Oct 24 '12 at 12:36
Don't sure, because (self.label.center.x) is some position relative to ?????? –  MBo Oct 24 '12 at 12:56
its relative to an origo in point1 in your nice diagram :) –  David Karlsson Oct 24 '12 at 14:17
It seems for me that "positionOfLabel" is distance LabelPoint-POint2. How are you going to use this value? –  MBo Oct 24 '12 at 14:36
Position of label is meant as returning how far from the first point the label is placed. –  David Karlsson Oct 24 '12 at 18:10

Line equation is one of the solution but I found another nice example. Please check this link

share|improve this answer

Your Answer


