|
@@ -15,24 +15,12 @@ namespace bbiwarg.Utility
|
|
|
|
|
|
class Line2D
|
|
class Line2D
|
|
{
|
|
{
|
|
- public Vector2D P1 { get; private set; }
|
|
|
|
- public Vector2D P2 { get; private set; }
|
|
|
|
|
|
+ public Vector2D PointOnLine { get; private set; }
|
|
public Vector2D Direction { 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);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void setPoints(Vector2D p1, Vector2D p2)
|
|
|
|
- {
|
|
|
|
- //endpoints
|
|
|
|
- P1 = p1;
|
|
|
|
- P2 = p2;
|
|
|
|
-
|
|
|
|
- //direction
|
|
|
|
- Direction = (P1 - P2).normalize();
|
|
|
|
|
|
+ public Line2D(Vector2D pointOnLine, Vector2D direction) {
|
|
|
|
+ PointOnLine = pointOnLine;
|
|
|
|
+ Direction = direction.normalize();
|
|
}
|
|
}
|
|
|
|
|
|
public float getAngleBetween(Line2D line)
|
|
public float getAngleBetween(Line2D line)
|
|
@@ -41,37 +29,11 @@ namespace bbiwarg.Utility
|
|
return Math.Min(angle, 180 - angle);
|
|
return Math.Min(angle, 180 - angle);
|
|
}
|
|
}
|
|
|
|
|
|
- public float getParallelDistanceTo(Line2D line)
|
|
|
|
- {
|
|
|
|
- if (onSide(line.P1) != onSide(line.P2)) return 0;
|
|
|
|
-
|
|
|
|
- Vector2D a1 = projectToLine(line.P1);
|
|
|
|
- Vector2D a2 = projectToLine(line.P2);
|
|
|
|
-
|
|
|
|
- float distanceA1 = a1.getDistanceTo(line.P1);
|
|
|
|
- float distanceA2 = a2.getDistanceTo(line.P2);
|
|
|
|
- return Math.Min(distanceA1, distanceA2);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public float getVerticalDistanceTo(Line2D line)
|
|
|
|
- {
|
|
|
|
- Vector2D a1 = projectToLine(line.P1);
|
|
|
|
- Vector2D a2 = projectToLine(line.P2);
|
|
|
|
-
|
|
|
|
- if (P1.isInBox(a1, a2) || P2.isInBox(a1, a2)) return 0;
|
|
|
|
-
|
|
|
|
- float distanceP1A1 = P1.getDistanceTo(a1);
|
|
|
|
- float distanceP1A2 = P1.getDistanceTo(a2);
|
|
|
|
- float distanceP2A1 = P2.getDistanceTo(a1);
|
|
|
|
- float distanceP2A2 = P2.getDistanceTo(a2);
|
|
|
|
- return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public LineSide onSide(Vector2D point)
|
|
public LineSide onSide(Vector2D point)
|
|
{
|
|
{
|
|
float yPerX = Direction.Y / Direction.X;
|
|
float yPerX = Direction.Y / Direction.X;
|
|
- float xDiff = point.X - P1.X;
|
|
|
|
- float newY = P1.Y + yPerX * xDiff;
|
|
|
|
|
|
+ float xDiff = point.X - PointOnLine.X;
|
|
|
|
+ float newY = PointOnLine.Y + yPerX * xDiff;
|
|
if (newY < point.Y) return LineSide.above;
|
|
if (newY < point.Y) return LineSide.above;
|
|
else if (newY > point.Y) return LineSide.below;
|
|
else if (newY > point.Y) return LineSide.below;
|
|
else return LineSide.onLine;
|
|
else return LineSide.onLine;
|
|
@@ -79,7 +41,7 @@ namespace bbiwarg.Utility
|
|
|
|
|
|
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 = PointOnLine.X, oy = PointOnLine.Y;
|
|
float diffx = px - ox;
|
|
float diffx = px - ox;
|
|
float diffy = py - oy;
|
|
float diffy = py - oy;
|
|
|
|
|
|
@@ -93,12 +55,12 @@ namespace bbiwarg.Utility
|
|
return new Vector2D(newX, newY);
|
|
return new Vector2D(newX, newY);
|
|
}
|
|
}
|
|
|
|
|
|
- public Vector2D intersection(Line2D line)
|
|
|
|
|
|
+ public Vector2D getIntersection(Line2D line)
|
|
{
|
|
{
|
|
- Vector2D p = P1;
|
|
|
|
- Vector2D r = P2 - P1;
|
|
|
|
- Vector2D q = line.P1;
|
|
|
|
- Vector2D s = line.P2 - line.P1;
|
|
|
|
|
|
+ Vector2D p = PointOnLine;
|
|
|
|
+ Vector2D r = Direction;
|
|
|
|
+ Vector2D q = line.PointOnLine;
|
|
|
|
+ Vector2D s = line.Direction;
|
|
|
|
|
|
float r_cross_s = r.cross(s);
|
|
float r_cross_s = r.cross(s);
|
|
float q_p_cross_s = (q - p).cross(s);
|
|
float q_p_cross_s = (q - p).cross(s);
|
|
@@ -110,9 +72,5 @@ namespace bbiwarg.Utility
|
|
return p + t * r;
|
|
return p + t * r;
|
|
}
|
|
}
|
|
|
|
|
|
- public override string ToString()
|
|
|
|
- {
|
|
|
|
- return (int)P1.X + "|" + (int)P1.Y + " --- " + (int)P2.X + "|" + (int)P2.Y;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|