public Shape CreateCircleApprox(float length, int edges) { MinMax <float> angleRange = new MinMax <float>(0.3f, 1.5f); edges = Math.Max(edges, 3); float[] angles = new float[edges]; //use random angles angles = BuildCircleApproxAngles(edges, angleRange); //create lines List <LineSegment> lines = new List <LineSegment>(); Point2D lastPoint = SwinGame.PointAt(0, 0); float totalAngle = 0; foreach (float angle in angles) { totalAngle += angle; Vector vec = SwinGame.VectorFromAngle(totalAngle, length); lines.Add(SwinGame.CreateLine(lastPoint, lastPoint + vec)); lastPoint = lines.Last().EndPoint; } lines.Add(SwinGame.CreateLine(lastPoint, SwinGame.PointAt(0, 0))); //bounding box List <LineSegment> boundingBox = CreateBoundingBox(lines); Shape shape = new Shape(lines, boundingBox, SwinGame.PointAt(0, 0)); return(shape); }
private void AddBoxes(List <LineSegment> shape, JArray boxesObj, int lineLength) { if (boxesObj == null) { return; } List <Point2D> boxes = boxesObj.ToObject <List <Point2D> >(); foreach (Point2D p in boxes) { Point2D[] corners = BoxCorners(p, lineLength); shape.Add(SwinGame.CreateLine(corners[0], corners[1])); shape.Add(SwinGame.CreateLine(corners[1], corners[2])); shape.Add(SwinGame.CreateLine(corners[2], corners[3])); shape.Add(SwinGame.CreateLine(corners[3], corners[0])); } }
private void AddTriangles(List <LineSegment> shape, JArray trianglesObj, int lineLength) { if (trianglesObj == null) { return; } List <Point2D> triangles = trianglesObj.ToObject <List <Point2D> >(); foreach (Point2D p in triangles) { Point2D[] corners = BoxCorners(p, lineLength); LineSegment bottom = SwinGame.CreateLine(corners[3], corners[2]); LineSegment left = SwinGame.CreateLine(corners[2], SwinGame.LineMidPoint(SwinGame.CreateLine(corners[0], corners[1]))); LineSegment right = SwinGame.CreateLine(corners[3], SwinGame.LineMidPoint(SwinGame.CreateLine(corners[0], corners[1]))); shape.Add(bottom); shape.Add(left); shape.Add(right); } }
//build bounding box for collision mask private List <LineSegment> CreateBoundingBox(List <LineSegment> lines) { float xMin = 0, xMax = 0, yMin = 0, yMax = 0; foreach (LineSegment l in lines) { FindFromPoint2D(l.StartPoint); FindFromPoint2D(l.EndPoint); } void FindFromPoint2D(Point2D linePos) { if (linePos.X < xMin) { xMin = linePos.X; } if (linePos.X > xMax) { xMax = linePos.X; } if (linePos.Y < yMin) { yMin = linePos.Y; } if (linePos.Y > yMax) { yMax = linePos.Y; } } return(new List <LineSegment>() { SwinGame.CreateLine(xMin, yMin, xMax, yMin), SwinGame.CreateLine(xMax, yMin, xMax, yMax), SwinGame.CreateLine(xMax, yMax, xMin, yMax), SwinGame.CreateLine(xMin, yMax, xMin, yMin) }); }
////////////////////// // drawing linesegment ////////////////////// public static void Draw(this LineSegment l, Color clr, Point2D offset) { LineSegment result = SwinGame.CreateLine(l.StartPoint.Add(offset), l.EndPoint.Add(offset)); SwinGame.DrawLine(clr, result); }
/// <summary> /// Multiply point by an int /// </summary> public static LineSegment Multiply(this LineSegment l, int x) { return(SwinGame.CreateLine(l.StartPoint.Multiply(x), l.EndPoint.Multiply(x))); }