Line Segment Calculator

Line Segment Calculator

Emerging AI Skill PRO

Enter Calculation

Results

Length L
10
units
Horizontal change Δx (x₂ − x₁) 6
Vertical change Δy (y₂ − y₁) 8
Midpoint x 4
Midpoint y 6
Slope (Δy divided by Δx) 1.333333
Direction angle of P1 to P2 53.130102°
End point x₂ 7
End point y₂ 10

Formula

L=(x2−x1)2+(y2−y1)2,M=(x1+x22,y1+y22)L = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2},\; M = \left(\frac{x_1 + x_2}{2},\ \frac{y_1 + y_2}{2}\right)
Usage stats

Advertisement

Report an error on this page

What this calculator does

A line segment is the straight piece of a line between two end points P1 (x₁, y₁) and P2 (x₂, y₂) in the plane, including both ends. This calculator works in the plane (two coordinates per point). It gives the length of the segment, the signed changes Δx and Δy from P1 to P2, the midpoint, the slope and the direction angle; or, from a start point, a length and a direction, it finds the end point.

Derivation of the formulas

Length:

$$ L = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} $$

Midpoint:

$$ M = \left(\frac{x_1 + x_2}{2},\ \frac{y_1 + y_2}{2}\right) $$

Slope:

$$ m = \frac{y_2 - y_1}{x_2 - x_1}\quad (x_2 \ne x_1) $$

Direction angle:

$$ \theta = \operatorname{atan2}(\Delta y, \Delta x),\quad -180^\circ < \theta \le 180^\circ $$

End point from length and direction:

$$ x_2 = x_1 + L\cos\theta,\quad y_2 = y_1 + L\sin\theta $$

The changes Δx = x₂ − x₁ and Δy = y₂ − y₁ are the legs of a right triangle whose hypotenuse is the segment, so the Pythagorean theorem gives L = √(Δx² + Δy²). The midpoint is the average of the two end points, because moving half of Δx and half of Δy from P1 lands halfway along the segment. The slope is the rise over the run, Δy/Δx; a vertical segment has Δx = 0 and no slope. The direction angle θ is the angle from the positive x-axis to the segment, counted counterclockwise: cos θ = Δx/L and sin θ = Δy/L, which the two-argument arctangent atan2(Δy, Δx) solves in every quadrant, from −180° to 180°. Turning these two relations around gives the end point from a start point, a length and an angle: x₂ = x₁ + L cos θ and y₂ = y₁ + L sin θ. Angles that are whole multiples of 90° use the exact values 0, 1 and −1 of the cosine and sine.

Solving from what you know

Choose what you know: Both end points P1 and P2; Start point P1, length and direction angle. The calculator first finds the sizes that define the curve and then computes every other value from the formulas above. Each length is accepted from 0.000001 to 1,000,000,000; anything outside is refused with a sentence that names the range. Each coordinate is accepted from -1,000,000,000 to 1,000,000,000 (negative values and 0 included), and the direction angle from -360 to 360 degrees.

Worked example

With the defaults P1 (1, 2) and P2 (7, 10): Δx = 6, Δy = 8, so L = √(36 + 64) = 10; the midpoint is (4, 6), the slope is 8/6 ≈ 1.333333 and the direction angle is atan2(8, 6) ≈ 53.130102°.

Results are accurate to at least 9 significant digits for ordinary inputs. When a result depends on the small difference of two nearly equal entries (closer than one part in a million), fewer digits are reliable. They are shown with up to 6 decimal places; a value below 0.000001 or of 10¹⁵ and more is shown as its plain 12-digit number instead. The unit you pick is a label: lengths are shown in that unit and areas in its square.

Advertisement
A line segment from (1, 2) to (7, 10), with horizontal change 6, vertical change 8, length 10, and midpoint (4, 6).
The horizontal and vertical changes form a right triangle. Its hypotenuse is the segment length, and the marked point is the midpoint.

Frequently asked questions

Why can Δx, Δy and the coordinates be negative?

They are signed: Δx is negative when P2 lies to the left of P1, and Δy is negative when P2 lies below P1. The length is always positive, because it squares both changes.

What is the slope of a vertical segment?

A vertical segment has Δx = 0, so the rise over the run would divide by zero: its slope is undefined, and the calculator says so instead of showing a number.

How is the direction angle measured?

From the positive x-axis to the direction from P1 to P2, counterclockwise positive, between −180° and 180°. An angle you enter outside that span, such as 270°, is shown as the same direction inside it (−90°).

How accurate are the results?

Results are accurate to at least 9 significant digits for ordinary inputs. When a result depends on the small difference of two nearly equal entries (closer than one part in a million), fewer digits are reliable.

References

D. Zwillinger (ed.), CRC Standard Mathematical Tables and Formulas, 33rd edition, CRC Press, 2018

Last updated: