A class that illustrates how to determine if a point falls on a line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace PointOnLine | |
{ | |
class Class1 | |
{ | |
public static void Main() | |
{ | |
PointLine(2,2,4,2,5,2); | |
} | |
private static bool PointLine(float x1, float y1, float x2, float y2, float x, float y) | |
{ | |
bool OnLine = false; //returnValue | |
if (x1 == x2) //this means it is a vertical line, avoid the Divide by Zero error. | |
{ | |
if (Math.Abs(x - x1) <= .001) | |
OnLine = true; | |
else | |
OnLine = false; | |
} | |
else | |
{ | |
//given two points create the linear equation in the form of y=mx+b | |
double m = (y1 - y2) / (x1 - x2); //slope | |
double b = y1 - m * x1; //y-intercept | |
if (Math.Abs(y - (m * x + b)) <= .001) //if the point is within .001 of the line then it is considered On the line | |
OnLine = true; | |
else | |
OnLine = false; | |
} | |
return OnLine; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment