The midpoint
Point is the midpoint of the line segment when it lies on the segment and . Its coordinates are the averages of the endpoints’ coordinates:
function midpoint(a: Point, b: Point): Point {
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
}function midpoint(a, b) {
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
}def midpoint(a: Point, b: Point) -> Point:
return Point((a.x + b.x) / 2, (a.y + b.y) / 2)midpoint- time O(1) space O(1)
Where it’s useful
The midpoint is handy whenever you need the point halfway between two things, like aiming a camera between two players, or putting a hit effect between two objects that bumped into each other. It’s also the centre of a segment, which the chapter on line segments comes back to.
Comments
No comments yet. Questions and corrections are welcome.