Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Last active December 29, 2016 19:27
Show Gist options
  • Select an option

  • Save GER-NaN/2e2788b9065f37af6a22e84a94429d55 to your computer and use it in GitHub Desktop.

Select an option

Save GER-NaN/2e2788b9065f37af6a22e84a94429d55 to your computer and use it in GitHub Desktop.
Point class with misc functions (In Progress)
using System;
/// <summary>Represents a Point in 2D space using double to store coordinates.</summary>
public struct PointD
{
public readonly double X;
public readonly double Y;
public PointD(double x, double y)
{
X = x;
Y = y;
}
public PointD(PointD p)
{
X = p.X;
Y = p.Y;
}
/// <summary>Is this points X coordinate between the X coordinates of the 2 specified Points</summary>
public bool IsHorizontalBetween(PointD left, PointD right)
{
return (X > left.X && X < right.X);
}
/// <summary>Is this points Y coordinate between the Y coordinates of the 2 specified Points</summary>
public bool IsVerticalBetween(PointD bottom, PointD top)
{
return (Y > bottom.Y && Y < top.Y);
}
/// <summary>The horizontal distance between the 2 points. The absolute value of the X coordinates.</summary>
public double HorizontalDistanceFrom(PointD p)
{
return Math.Abs(this.X - p.X);
}
/// <summary>The vertical distance between the 2 points. The absolute value of the Y coordinates.</summary>
public double VerticalDistanceFrom(PointD p)
{
return Math.Abs(this.Y - p.Y);
}
/// <summary>Calculates the (Euclidean) Distance between the point</summary>
public double DistanceFrom(PointD p)
{
return PointD.Distance(this, p);
}
/// <summary>Checks if the points X and Y coordinates are equal to the specified point. A comparison epsilon can be provided depending on the precision that is needed for the comparison.</summary>
public bool IsEqual(PointD compareTo, double comparisonEpsilon = .01)
{
double xDiff = Math.Abs(this.X - compareTo.X);
double yDiff = Math.Abs(this.Y - compareTo.Y);
return (xDiff < comparisonEpsilon && yDiff < comparisonEpsilon);
}
/// <summary>Formatted as (x,y) where x and y are the unformatted double value.</summary>
public override string ToString()
{
return $"({X},{Y})";
}
/// <summary>The (Euclidean) distance between the 2 points</summary>
public static double Distance(PointD p, PointD q)
{
double dx = p.X - q.X;
double dy = p.Y - q.Y;
var dist = Math.Sqrt(dx * dx + dy * dy);
return dist;
}
/// <summary>The midpoint between the two Points.</summary>
public static PointD MidPoint(PointD p, PointD q)
{
double midX = (p.X + q.X) / 2;
double midY = (p.Y + q.Y) / 2;
return new PointD(midX, midY);
}
}
@GER-NaN

GER-NaN commented Dec 29, 2016

Copy link
Copy Markdown
Author

This needs tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment