To find the slope of a line between two points, we will use the formula:
index.ts30 chars2 lines
In TypeScript, we can create a function that takes in two points as arguments and returns the slope:
index.ts186 chars10 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:
index.ts150 chars6 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