Here is a recent example of my using of OpenAI, one of many different possible AI techs available.
The code below opens a dxf file that has lines and arcs in it and finds the intersections of lines and arcs and lines, with the lines projected to infinity, but only returns the intersection points when the points actually cross each other, using the netDXF library in C#. Arcs can be very tricky with their start and end angles and many checks need to be made and duplicates need to be removed from the console output. If I have 2 lines or an arc and line that only cross each other, it will return the coordinates of the intersection and the handle ids of the objects, but not twice with the just the handle strings inverted. I will also know the coordinates of where they would if the line was projected to infinity, but I don't return that data for use atm, but that is there too.
I could have written all of this, but it would have taken me days or weeks relearning the math and getting the code correct and reading about how to go about doing this from others, but yet here it is...working...and doing it memory, which is magnitudes faster than accessing the data graphics to get those intersections (as how typically done) and it doesn't require a drawing engine to do the work and then extract the data. That really matters when working with large datasets pushing 100,000 - 4000,000 entities. The ram on the machines is pushed to the limit with most standard graphics approaches. One would need to check each against every other one. It's an exponential problem.
Please don't let AI drive you crazy. It can. Enjoy the code. It's good stuff.
It starts below at:
/// good!!!
DxfDocument loaded = DxfDocument.Load("C:\\Users\\Dave\\Downloads\\new block\\new\\arcline.dxf");
As some of classes need to be above the code.
public static int FindLineCircleIntersections(Vector3 circleCenter, double radius,
Vector3 lineStart, Vector3 lineEnd,
out Vector3 intersection1, out Vector3 intersection2)
{
double dx, dy, A, B, C, det, t;
dx = lineEnd.X - lineStart.X;
dy = lineEnd.Y - lineStart.Y;
A = dx * dx + dy * dy;
B = 2 * (dx * (lineStart.X - circleCenter.X) + dy * (lineStart.Y - circleCenter.Y));
C = (lineStart.X - circleCenter.X) * (lineStart.X - circleCenter.X) +
(lineStart.Y - circleCenter.Y) * (lineStart.Y - circleCenter.Y) -
radius * radius;
det = B * B - 4 * A * C;
if ((A buffer)
{
// The point is not on the arc's circle.
return false;
}
// Calculate the angle of the point relative to the center of the arc.
double angleOfPoint = Math.Atan2(point.Y - arcCenter.Y, point.X - arcCenter.X) * (180.0 / Math.PI);
angleOfPoint = (angleOfPoint endAngle)
{
// The arc crosses the 0 degree line
return angleOfPoint >= startAngle || angleOfPoint = 0 && dotProduct intersectedLines = new List ();
List intersectedArcs = new List ();
// Iterate through lines in ModelSpace
foreach (Line line in entitiesBlocks.OfType ())
{
// Extend both ends of the line in 2D
//Convert the Vector3 to a Vector2 and extend
Vector2 startPoint2D = new Vector2(line.StartPoint.X, line.StartPoint.Y);
Vector2 endPoint2D = new Vector2(line.EndPoint.X, line.EndPoint.Y);
//Not used atm and not needed, a the line projects to infinity
//startPoint2D = startPoint2D - extensionAmount * new Vector2(line.Direction.X, line.Direction.Y);
//endPoint2D = endPoint2D + extensionAmount * new Vector2(line.Direction.X, line.Direction.Y);
// Update the 3D line endpoints while preserving the original Z value
//Not used atm and not needed, a the line projects to infinity
////line.StartPoint = new Vector3(startPoint2D.X, startPoint2D.Y, line.StartPoint.Z);
////line.EndPoint = new Vector3(endPoint2D.X, endPoint2D.Y, line.EndPoint.Z);
}
// Iterate through arcs in ModelSpace
foreach (Arc myarc in entitiesBlocks.OfType ())
{
// Extend both ends of the arc
//Not used atm, still checking projection via the required estimate of the curve
//myarc.StartAngle -= extensionAmount;
//myarc.EndAngle += extensionAmount;
}
// Lists to store unique intersection details
List uniqueIntersections = new List ();
// This HashSet will store tuples of line and arc handles that have been printed to avoid duplicates
HashSet > printedPairs = new HashSet >();
// Iterate through entities to find intersections
for (int i = 0; i < entitiesBlocks.Count; i++)
{
for (int j = i + 1; j < entitiesBlocks.Count; j++)
{
if (entitiesBlocks[i] is Line line1 && entitiesBlocks[j] is Line line2)
{
// Check for intersection between two lines
Vector2 intersection = MathHelper.FindIntersection(
line1.StartPoint.ToVector2(),
line1.Direction.T
oVector2(),
line2.StartPoint.ToVector2(),
line2.Direction.ToVector2(),
1
e-6);
if (!double.IsNaN(intersection.X) && !double.IsNaN(intersection.Y))
{
// Ensure the intersection is within the bounds of line1
bool isWithinLine1 = intersection.IsBetween(line1.StartPoint.ToVector2(), line1.EndPoint.ToVector2());
// Ensure the intersection is within the bounds of line2
bool isWithinLine2 = intersection.IsBetween(line2.StartPoint.ToVector2(), line2.EndPoint.ToVector2());
// If the intersection is within the bounds of both lines
if (isWithinLine1 && isWithinLine2)
{
// Create a unique intersection identifier
string identifier = $"{intersection.X:F3},{intersection.Y:F3}";
// Check for uniqueness
if (!uniqueIntersections.Contains(identifier))
{
intersectedLines.Add(line1);
<
P>intersectedLines.Add(line2);
uniqueIntersections.Add(identifier);
// Output intersection details for lines
Console.WriteLine($"Intersection at ({intersection.X:F3},{intersection.Y:F3}) between Line: {line1.Handle} and Line: {line2.Handle}");
}
}
}
}
else if (entitiesBlocks[i] is Arc arc && entitiesBlocks[j] is Line line)
{
Vector3 intersection1, intersection2;
int intersections = FindLineCircleIntersections(
new Vector3((float)arc.Center.X, (float)arc.Center.Y, (float)arc.Center.Z),
arc.Radius,
line.StartPoint,
line.EndPoint,
out intersection1, out intersection2);
// Handle the intersections...
for (int k = 1; k <= intersections; k++)
{
Vector2 intersectionPoint = k == 1 ?
new Vector2(intersection1.X, intersection1.Y) :
new Vector2(intersection2.X, intersection2.Y);
// Check if intersection is within arc's angles and on the line segment
if (IsPointOnArc(intersectionPoint, arc.Center.ToVector2(), arc.Radius, arc.StartAngle, arc.EndAngle) &&
intersectionPoint.IsBetween(line.StartPoint.ToVector2(), line.EndPoint.ToVector2()))
{
// Create a unique intersection identifier
string identifier = $"{intersectionPoint.X:F3},{intersectionPoint.Y:F3}";
// Check if we have already printed this pair
if (!uniqueIntersections.Contains(identifier))
{
// If not, print the details and add the identifier to the uniqueIntersections
Console.WriteLine($"Intersection at ({intersectionPoint.X:F3},{intersectionPoint.Y:F3}) between Line: {line.Handle} and Arc: {arc.Handle}");
uniqueIntersections.Add(identifier);
}
}
}
}
<
P>// ... [Any other intersection checks]
}
}
// Save the modified DXF file
loaded.Save("C:\\Users\\Dave\\Downloads\\new block\\new\\1a.dxf");