find the slope between two points in csharp

To find the slope between two points in C#, you can use the following formula:

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

Here's an example code snippet that calculates the slope between two points:

main.cs
double x1 = 2;
double y1 = 4;
double x2 = 5;
double y2 = 9;

double slope = (y2 - y1) / (x2 - x1);

Console.WriteLine("The slope between ({0},{1}) and ({2},{3}) is {4}", x1, y1, x2, y2, slope);
194 chars
9 lines

This code will output:

main.cs
The slope between (2,4) and (5,9) is 1.66666666666667
54 chars
2 lines

Note that if the two points have the same x-coordinate, the slope will be undefined (since division by zero is not allowed). In such cases, you may want to handle the special case separately.

gistlibby LogSnag