Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Created December 29, 2016 16:12
Show Gist options
  • Save GER-NaN/dbc49abeac145cc404443353ce4f5502 to your computer and use it in GitHub Desktop.
Save GER-NaN/dbc49abeac145cc404443353ce4f5502 to your computer and use it in GitHub Desktop.
Represents a line in the form ax + by = c (in progress)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class LineT
{
public double A;
public double B;
public double C;
public LineT(double a, double b, double c)
{
A = a;
B = b;
C = c;
}
public LineT(Point p1, Point p2)
{
A = p2.Y = p1.Y; // y2 - y1
B = p1.X - p2.X; //x1 - x2
C = A * p1.X + B * p1.Y; //A * x1 + B * y1
}
public override string ToString()
{
return $"{A}x + {B}y = {C}";
}
//Make member function (non-static)
public static bool DoLinesIntersect(Line first, Line second, ref Point intersectionPoint)
{
return DoLinesIntersect(first.A, first.B, first.C, second.A, second.B, second.C, ref intersectionPoint);
}
public static bool DoLinesIntersect(double a1, double b1, double c1, double a2, double b2, double c2, ref Point intersectionPoint)
{
double delta = a1 * b2 - a2 * b1;
if (Math.Abs(delta) < 0.0001)//Tolerance
{
return false;
}
double x = (b2 * c1 - b1 * c2) / delta;
double y = (a1 * c2 - a2 * c1) / delta;
intersectionPoint = new Point((int)x, (int)y);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment