I have a LineShape
in my form and I need that when clicked a label appears exactly in the middle of this line, what is the math I should do to determinate this middle point?
Asked
Active
Viewed 149 times
0

Mateus Barbosa
- 87
- 1
- 13
1 Answers
2
LineShape
exposes its coordinates with X1
, X2
, Y1
and Y2
. Finding the center of it is basic geometry:
LineShape line = /*...*/;
Label label = /*..*/;
// calculate the center of the line
var center = new Point((line.X1 + line.X2) / 2, (line.Y1 + line.Y2) / 2);
// center the label on the line
label.Top = center.Y - label.Height / 2
label.Left = center.X - label.Width / 2

Xiaoy312
- 14,292
- 1
- 32
- 44
-
1The final step would be to set the label's `Location.X` and `Location.Y` to the values of `x` and `y` that @Xiaoy312 mentioned above. – Lews Therin May 03 '16 at 18:39
-
1@Sylverac Actually, that doesn't center the label on the line, but it centers the top-left corner of the label on the line. – Xiaoy312 May 03 '16 at 18:45
-
Ahh yes, I forgot that the position's origin is at the top-left corner. – Lews Therin May 03 '16 at 18:47
-
I had already centered the label, I just needed the center of the line, the first code was enough but now may help others as well – Mateus Barbosa May 03 '16 at 19:40