Skip to content

Instantly share code, notes, and snippets.

@amaboura
Last active December 28, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaboura/7511623 to your computer and use it in GitHub Desktop.
Save amaboura/7511623 to your computer and use it in GitHub Desktop.
a c# helper class that contains the basic geometry operations.
using System;
using System.Text;
using System.Windows;
namespace Geometry.NET
{
public static class Basic
{
// a basic struct that represnt a line's equation
public struct Equation
{
public double m;
public double b;
public Equation(double p1, double p2)
{
this.m = p1;
this.b = p2;
}
}
// convert from radian to degree
public static double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
// return the angle between two lines
public static double GetAngle(Point P1 , Point P2 , Point Vertex)
{
float f1 = (float)(GetDistanceInPX(Vertex, P1) * GetDistanceInPX(Vertex, P1) + GetDistanceInPX(Vertex, P2) * GetDistanceInPX(Vertex, P2) - GetDistanceInPX(P1, P2) * GetDistanceInPX(P1, P2));
float f2 = (float)(2 * GetDistanceInPX(Vertex, P1) * GetDistanceInPX(Vertex, P2));
double r = Math.Acos(f1 / f2);
return RadianToDegree(r);
}
// get an equation from a line
public static Equation GetEquation(Line L)
{
float m = (float)((L.Y2 - L.Y1) / (L.X2 - L.X1));
float b = (float)(L.Y1 - m * L.X1);
return new Equation(m,b);
}
// return the intersection point bewteen two lines
public static Point GetIntersection(Equation E1, Equation E2 )
{
double delta = E2.m - E1.m;
if (delta == 0)
throw new ArgumentException("Lines are parallel");
double x = Double.Parse(((E1.b-E2.b)/delta).ToString("0.00"));
double y = Double.Parse(((- E1.m * E2.b + E2.m * E1.b) / delta).ToString("0.00"));
return new Point(x,y);
}
// given the point P , return a new line parallel to L
public static Equation GetParallelLine(Equation E, Point P)
{
double Slope = E.m;
Equation Parallell = new Equation();
Parallell.m = Slope;
Parallell.b = -Slope * P.X + P.Y;
return Parallell;
}
// given a point P , return the perpendicular projection point with the line L
public static Point GetPIntersection(Line L, Point P)
{
Equation E = GetEquation(L);
double Slope = -1 / E.m;
Equation Perpendicular = new Equation();
Perpendicular.m = Slope;
Perpendicular.b = -Slope * P.X + P.Y;
Point r = GetIntersection(E, Perpendicular);
return r;
}
// return the distance between two points in mm
public static float GetDistanceInMM(Point P1, Point P2)
{
float a = (float)(P1.X - P2.X);
float b = (float)(P1.Y - P2.Y);
float distance = (float)(Math.Sqrt(a * a + b * b));
return (float)(distance * 0.264583333);
}
// return the distance between two points in mm
public static double GetDistanceInPX(Point P1, Point P2)
{
double a = (P1.X - P2.X);
double b = (P1.Y - P2.Y);
double r = a * a + b * b;
double distance = Math.Sqrt(r);
return distance;
}
// return the percentage of a given value from a whole value
public static double GetPercentage(float Given, float Whole)
{
return Given * 100 / Whole;
}
// return the midpoint of a given line
public static Point GetMidPoint(Point P1 , Point P2)
{
double X = Double.Parse(((P1.X + P2.X) / 2).ToString("0.00"));
double Y = Double.Parse(((P1.Y + P2.Y) / 2).ToString("0.00"));
return new Point(X,Y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment