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:
-(double)positionOfLabel{
//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));
}
-(void)labelTranslate:(double)length{
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
Use of Y=kx+m form is not a good idea (because of vertical lines), so parametric form is recommended: