#if ACAD24
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
#elif BCAD
using Bricscad.EditorInput;
using Teigha.Geometry;
using Teigha.DatabaseServices;
using Teigha.Colors;
#endif
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Boprs
{
///
/// Non=specific utility functions.
///
internal static class Utils
{
///
/// Largest difference between two doubles before they are considered equal.
///
internal const double DOUBLEPREC = 1e-8;
///
/// Signed area of the triangle (pt1, pt2, pt3).
///
/// Point 1
/// Point 2
/// Point 3
/// Signed area of the triangle (pt1, pt2, pt3).
internal static double SignedArea(Point2d pt1, Point2d pt2, Point2d pt3)
{
return (pt1.X - pt3.X) * (pt2.Y - pt3.Y) - (pt2.X - pt3.X) * (pt1.Y - pt3.Y);
}
///
/// Are the two doubles close enough to be almost equal?
///
/// First double to compare.
/// Second double to compare.
/// true if doubles are almost equal, false if doubles are not equal.
internal static bool DoubleEquals(double a, double b)
{
return Math.Abs(a - b) < DOUBLEPREC;
}
///
/// Get the value Y at a given coordinate X for line going through stPt and endPt.
///
/// Start point of the line.
/// End point of the line.
/// X param.
/// Y value at X param for line going through the 2 points.
internal static double YatX(Point2d stPt, Point2d endPt, double x)
{
double m = (endPt.Y - stPt.Y) / (endPt.X - stPt.X);
double b = stPt.Y - (m * stPt.X);
return m * x + b;
}
///
/// Insert a new vertex inbto a sorted list of vertices such that it remains sorted.
///
/// Sorted list of vertices.
/// Vertex to insert.
internal static void InsertVertexSorted(List vertices, SweepVertex newElement)
{
int i = 0;
// Go through all the vertices, find my index for insertion.
for (; i < vertices.Count; i++)
{
// If we are left of the current edge, that is the correct index.
if (newElement.CompareTo(vertices[i]) < 0)
{
break;
}
// If we are to the right of the last vertex, set inserttion index after it.
else if (i == vertices.Count - 1)
{
i++;
}
}
// Insert the vertex to the correct place.
vertices.Insert(i, newElement);
}
internal static bool Overlaps(this LineSegment2d me, LineSegment2d other)
{
if (!me.IsColinearTo(other))
{
return false;
}
Line2d commonLine = me.GetLine();
double a = commonLine.GetParameterOf(me.StartPoint);
double b = commonLine.GetParameterOf(me.EndPoint);
double c = commonLine.GetParameterOf(other.StartPoint);
double d = commonLine.GetParameterOf(other.EndPoint);
if(DoubleEquals(b, c) || DoubleEquals(a, d))
{
return false;
}
if (b < c || d < a)
{
return false;
}
return true;
}
///
/// Convert a 3d point to 2d.
///
internal static Point2d To2d(this Point3d pt)
{
return new Point2d(pt.X, pt.Y);
}
///
/// Convert a 2d point to 3d.
///
internal static Point3d To3d(this Point2d pt)
{
return new Point3d(pt.X, pt.Y, 0);
}
}
}