using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HeuristicLab.Problems.BinPacking3D.Geometry {
public class Edge3D {
public Edge3D() { }
public Edge3D(PackingPosition start, PackingPosition end) {
Start = new Vector3D(start);
End = new Vector3D(end);
}
public Edge3D(Vector3D start, Vector3D end) {
Start = start;
End = end;
}
public Vector3D Start { get; set; }
public Vector3D End { get; set; }
///
/// Returns true if a given point lies on the edge.
///
///
/// Returns true if a given point lies on the edge.
public bool LiesOn(Vector3D point) {
if (point == null) {
return false;
}
var x = (Start.X <= point.X && point.X <= End.X) || (Start.X >= point.X && point.X >= End.X);
var y = (Start.Y <= point.Y && point.Y <= End.Y) || (Start.Y >= point.Y && point.Y >= End.Y);
var z = (Start.Z <= point.Z && point.Z <= End.Z) || (Start.Z >= point.Z && point.Z >= End.Z);
return x && y && z;
}
///
/// Returns a point where the two edges intersects.
/// It returns null if they don't intersect.
///
///
/// Returns a point where the two edges intersects. Null = no intersection.
public Vector3D Intersects(Edge3D edge) {
return Intersects(this, edge);
}
///
/// Returns a point where two edges are intersecting.
/// It returns null if they don't intersect.
///
///
///
/// Returns a point where the two edges intersects. Null = no intersection.
public static Vector3D Intersects(Edge3D e1, Edge3D e2) {
Line3D l1 = new Line3D(e1.Start, new Vector3D() {
X = e1.Start.X - e1.End.X,
Y = e1.Start.Y - e1.End.Y,
Z = e1.Start.Z - e1.End.Z
});
Line3D l2 = new Line3D(e2.Start, new Vector3D() {
X = e2.Start.X - e2.End.X,
Y = e2.Start.Y - e2.End.Y,
Z = e2.Start.Z - e2.End.Z
});
Vector3D point = l1.Intersect(l2);
if (e1.LiesOn(point) && e2.LiesOn(point)) {
return point;
}
return null;
}
}
}