I have an algorithm as follows for finding the center of a line (midpoint).
public DoublePoint getMidPoint() {
return new DoublePoint((origin.x + endPoint.x) / 2, (origin.y + endPoint.y) / 2);
}
It seems to work for any values. But I seem to remember a much more complex algorithm, involving two circles whose radius equals the length of the line and whose center points are the ends of the line. A line drawn from the intersection of those circles would intersect the line segment you are testing, giving the line’s midpoint. I know for sure that algorithm works 100% of the time. Not sure about my simpler algorithm which seems too simple.
What you are remembering is a geometric method of constructing the perpendicular bisector of a line segment using only a compass and straightedge. Consider, for instance:
This was fine for the ancient Greeks, but there are other methods (such as the one you have coded) which work better for computers.