find the slope between two points in typescript

To find the slope of a line between two points, we will use the formula:

slope = (y2 - y1) / (x2 - x1)
30 chars
2 lines

In TypeScript, we can create a function that takes in two points as arguments and returns the slope:

interface Point {
  x: number;
  y: number;

function getSlope(point1: Point, point2: Point): number {
  const slope = (point2.y - point1.y) / (point2.x - point1.x);
  return slope;
186 chars
10 lines

This function takes in two points of type Point, which are interfaces containing x and y coordinates. The function then calculates the vertical change (y2 - y1) and the horizontal change (x2 - x1) between the two points and divides them to obtain the slope.

We can use this function as follows:

const point1: Point = { x: 1, y: 2 };
const point2: Point = { x: 3, y: 4 };

const slope = getSlope(point1, point2);
console.log(slope); // Output: 1
150 chars
6 lines

In this example, we create two points with coordinates (1, 2) and (3, 4) and pass them into the getSlope function. The function returns a slope of 1, which means the line connecting these two points has a slope of 1.

gistlibby LogSnag