public bool PolygonAndLineContact(PolygonShape polygon, LineShape line) { var next = 0; for (var current = 0; current < polygon.Points.Length; current++) { next = current + 1; if (next == polygon.Points.Length) { next = 0; } var hit = LineAndLineContact(line, new LineShape { PointA = polygon.Points[current], PointB = polygon.Points[next] }); if (hit) { return(true); } } return(false); }
public bool LineAndCircleContact(LineShape line, CircleShape circle) { var inside1 = PointAndCircleContact(line.PointA, circle); var inside2 = PointAndCircleContact(line.PointB, circle); if (inside1 || inside2) { return(true); } var distX = line.PointA.x - line.PointB.x; var distY = line.PointA.y - line.PointB.y; var len = (float)Math.Sqrt(distX * distX + distY * distY); var dot = ((circle.Center.x - line.PointA.x) * (line.PointB.x - line.PointA.x) + (circle.Center.y - line.PointA.y) * (line.PointB.y - line.PointA.y)) / (float)Math.Pow(len, 2); var closestX = line.PointA.x + dot * (line.PointB.x - line.PointA.x); var closestY = line.PointA.y + dot * (line.PointB.y - line.PointA.y); var onSegment = LineAndPointContact(line, new Vector2(closestX, closestY)); if (!onSegment) { return(false); } distX = closestX - circle.Center.x; distY = closestY - circle.Center.y; var distance = (float)Math.Sqrt(distX * distX + distY * distY); return(distance <= circle.Radius); }
public LineShape CloneLine(LineShape original) { return(new LineShape { PointA = new Vector2(original.PointA.x, original.PointA.y), PointB = new Vector2(original.PointB.x, original.PointB.y), Center = new Vector2(original.Center.x, original.Center.y) }); }
public bool LineAndPointContact(LineShape line, Vector2 point) { var d1 = Vector2.Distance(point, line.PointA); var d2 = Vector2.Distance(point, line.PointB); var lineLen = Vector2.Distance(line.PointA, line.PointB); const float buffer = 0.1f; return(d1 + d2 >= lineLen - buffer && d1 + d2 <= lineLen + buffer); }
public bool LineAndLineContact(LineShape lineA, LineShape lineB) { var uA = ((lineB.PointB.x - lineB.PointA.x) * (lineA.PointA.y - lineB.PointA.y) - (lineB.PointB.y - lineB.PointA.y) * (lineA.PointA.x - lineB.PointA.x)) / ((lineB.PointB.y - lineB.PointA.y) * (lineA.PointB.x - lineA.PointA.x) - (lineB.PointB.x - lineB.PointA.x) * (lineA.PointB.y - lineA.PointA.y)); var uB = ((lineA.PointB.x - lineA.PointA.x) * (lineA.PointA.y - lineB.PointA.y) - (lineA.PointB.y - lineA.PointA.y) * (lineA.PointA.x - lineB.PointA.x)) / ((lineB.PointB.y - lineB.PointA.y) * (lineA.PointB.x - lineA.PointA.x) - (lineB.PointB.x - lineB.PointA.x) * (lineA.PointB.y - lineA.PointA.y)); return(uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1); }
public void RotateLine(ref LineShape line, float deltaAngle) { deltaAngle = (float)Math.PI * deltaAngle / 180f; var cos = (float)Math.Cos(deltaAngle); var sin = (float)Math.Sin(deltaAngle); var xa = line.PointA.x - line.Center.x; var ya = line.PointA.y - line.Center.y; var xb = line.PointB.x - line.Center.x; var yb = line.PointB.y - line.Center.y; line.PointA.x = line.Center.x + xa * cos - ya * sin; line.PointA.y = line.Center.y + xa * sin + ya * cos; line.PointB.x = line.Center.x + xb * cos - yb * sin; line.PointB.y = line.Center.y + xb * sin + yb * cos; }
public void TranslateLine(ref LineShape line, Vector2 deltaPosition) { line.Center += deltaPosition; line.PointA += deltaPosition; line.PointB += deltaPosition; }