Skip to content

Instantly share code, notes, and snippets.

@johntholland
Created July 30, 2013 21:38
Show Gist options
  • Save johntholland/6117244 to your computer and use it in GitHub Desktop.
Save johntholland/6117244 to your computer and use it in GitHub Desktop.
A class that illustrates how to determine if a point falls on a line
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