|
@@ -6,7 +6,8 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace bbiwarg.Utility
|
|
namespace bbiwarg.Utility
|
|
{
|
|
{
|
|
- public enum LineSide {
|
|
|
|
|
|
+ public enum LineSide
|
|
|
|
+ {
|
|
onLine = 0,
|
|
onLine = 0,
|
|
above = 1,
|
|
above = 1,
|
|
below = 2
|
|
below = 2
|
|
@@ -14,40 +15,34 @@ namespace bbiwarg.Utility
|
|
|
|
|
|
class Line2D
|
|
class Line2D
|
|
{
|
|
{
|
|
- private Vector2D p1;
|
|
|
|
- private Vector2D p2;
|
|
|
|
- private Vector2D direction;
|
|
|
|
- private float length;
|
|
|
|
- private int numCombinedLines;
|
|
|
|
- public Vector2D P1 { get { return p1; } private set { p1 = value; } }
|
|
|
|
- public Vector2D P2 { get { return p2; } private set { p2 = value; } }
|
|
|
|
- public Vector2D Direction { get { return direction; } private set { direction = value; } }
|
|
|
|
- public float Length { get { return length; } private set { length = value; } }
|
|
|
|
- public int NumCombinedLines { get { return numCombinedLines; } private set { numCombinedLines = value; } }
|
|
|
|
-
|
|
|
|
- public Line2D(Vector2D p1, Vector2D p2) {
|
|
|
|
|
|
+ public Vector2D P1 { get; private set; }
|
|
|
|
+ public Vector2D P2 { get; private set; }
|
|
|
|
+ public Vector2D Direction { get; private set; }
|
|
|
|
+ public float Length { get { return P1.getDistanceTo(P2); }}
|
|
|
|
+
|
|
|
|
+ public Line2D(Vector2D p1, Vector2D p2)
|
|
|
|
+ {
|
|
setPoints(p1, p2);
|
|
setPoints(p1, p2);
|
|
- NumCombinedLines = 1;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void setPoints(Vector2D p1, Vector2D p2) {
|
|
|
|
|
|
+ private void setPoints(Vector2D p1, Vector2D p2)
|
|
|
|
+ {
|
|
//endpoints
|
|
//endpoints
|
|
P1 = p1;
|
|
P1 = p1;
|
|
P2 = p2;
|
|
P2 = p2;
|
|
|
|
|
|
//direction
|
|
//direction
|
|
- direction = (P1-P2).normalize();
|
|
|
|
-
|
|
|
|
- //length
|
|
|
|
- Length = P1.getDistanceTo(P2);
|
|
|
|
|
|
+ Direction = (P1 - P2).normalize();
|
|
}
|
|
}
|
|
|
|
|
|
- public float getAngleBetween(Line2D line) {
|
|
|
|
- float angle = direction.getAngleBetween(line.Direction);
|
|
|
|
- return Math.Min(angle, 180-angle);
|
|
|
|
|
|
+ public float getAngleBetween(Line2D line)
|
|
|
|
+ {
|
|
|
|
+ float angle = Direction.getAngleBetween(line.Direction);
|
|
|
|
+ return Math.Min(angle, 180 - angle);
|
|
}
|
|
}
|
|
|
|
|
|
- public float getParallelDistanceTo(Line2D line) {
|
|
|
|
|
|
+ public float getParallelDistanceTo(Line2D line)
|
|
|
|
+ {
|
|
if (onSide(line.P1) != onSide(line.P2)) return 0;
|
|
if (onSide(line.P1) != onSide(line.P2)) return 0;
|
|
|
|
|
|
Vector2D a1 = projectToLine(line.P1);
|
|
Vector2D a1 = projectToLine(line.P1);
|
|
@@ -58,7 +53,8 @@ namespace bbiwarg.Utility
|
|
return Math.Min(distanceA1, distanceA2);
|
|
return Math.Min(distanceA1, distanceA2);
|
|
}
|
|
}
|
|
|
|
|
|
- public float getVerticalDistanceTo(Line2D line) {
|
|
|
|
|
|
+ public float getVerticalDistanceTo(Line2D line)
|
|
|
|
+ {
|
|
Vector2D a1 = projectToLine(line.P1);
|
|
Vector2D a1 = projectToLine(line.P1);
|
|
Vector2D a2 = projectToLine(line.P2);
|
|
Vector2D a2 = projectToLine(line.P2);
|
|
|
|
|
|
@@ -66,13 +62,14 @@ namespace bbiwarg.Utility
|
|
|
|
|
|
float distanceP1A1 = P1.getDistanceTo(a1);
|
|
float distanceP1A1 = P1.getDistanceTo(a1);
|
|
float distanceP1A2 = P1.getDistanceTo(a2);
|
|
float distanceP1A2 = P1.getDistanceTo(a2);
|
|
- float distanceP2A1 = p2.getDistanceTo(a1);
|
|
|
|
- float distanceP2A2 = p2.getDistanceTo(a2);
|
|
|
|
|
|
+ float distanceP2A1 = P2.getDistanceTo(a1);
|
|
|
|
+ float distanceP2A2 = P2.getDistanceTo(a2);
|
|
return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
|
|
return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
|
|
}
|
|
}
|
|
|
|
|
|
- public LineSide onSide(Vector2D point) {
|
|
|
|
- float yPerX = direction.Y / direction.X;
|
|
|
|
|
|
+ public LineSide onSide(Vector2D point)
|
|
|
|
+ {
|
|
|
|
+ float yPerX = Direction.Y / Direction.X;
|
|
float xDiff = point.X - P1.X;
|
|
float xDiff = point.X - P1.X;
|
|
float newY = P1.Y + yPerX * xDiff;
|
|
float newY = P1.Y + yPerX * xDiff;
|
|
if (newY < point.Y) return LineSide.above;
|
|
if (newY < point.Y) return LineSide.above;
|
|
@@ -80,7 +77,8 @@ namespace bbiwarg.Utility
|
|
else return LineSide.onLine;
|
|
else return LineSide.onLine;
|
|
}
|
|
}
|
|
|
|
|
|
- public Vector2D projectToLine(Vector2D point) {
|
|
|
|
|
|
+ public Vector2D projectToLine(Vector2D point)
|
|
|
|
+ {
|
|
float px = point.X, py = point.Y, dx = Direction.X, dy = Direction.Y, ox = P1.X, oy = P1.Y;
|
|
float px = point.X, py = point.Y, dx = Direction.X, dy = Direction.Y, ox = P1.X, oy = P1.Y;
|
|
float diffx = px - ox;
|
|
float diffx = px - ox;
|
|
float diffy = py - oy;
|
|
float diffy = py - oy;
|
|
@@ -108,7 +106,7 @@ namespace bbiwarg.Utility
|
|
if (r_cross_s == 0.0)
|
|
if (r_cross_s == 0.0)
|
|
return null;
|
|
return null;
|
|
|
|
|
|
- float t = q_p_cross_s / r_cross_s;
|
|
|
|
|
|
+ float t = q_p_cross_s / r_cross_s;
|
|
return p + t * r;
|
|
return p + t * r;
|
|
}
|
|
}
|
|
|
|
|