user3002180
user3002180

Reputation: 431

Find center of line which has drawn by multiple points

I need to find the center of a line, lines may be in any shapes but the lines are constructed by points. As the Image shows red dots are points to draw line. What I want is a formula to find the center point of the line which has constructed by multiple points. enter image description here

Upvotes: 0

Views: 414

Answers (1)

Toon Krijthe
Toon Krijthe

Reputation: 53366

If you want to find the half way point of a segmented line, you can follow these steps:

  • Find the total length of all line segments.
  • Store the half length in a variable.
  • Start at the first line segment and check if its length exceeds the stored value. If not, subtract the length from the value and proceed to the next line segment. If it does, you know the relative position on the line (variable/segment length). You can now calculate the point.

Notes

  • Beware of segments with length 0.
  • Beware of circular lines (polygons).

Finding a point on a line

  • You have a line L from P1 to P2.
  • For each point on L, P = a (P2-P1) + P1, where a in (0,1).
  • Using a for the relative position, you can find point P.

Finding the length of a line segment

  • You have a line L from P1 to P2.
  • The length = sqrt ( sqr(X2-X1) + sqr(Y2-Y1)).

Upvotes: 2

Related Questions