From a0ed1a2a6cb0336108a2bbbf3d82b216e1497544 Mon Sep 17 00:00:00 2001 From: Ron Zuckerman Date: Sat, 17 Feb 2024 13:08:00 -0600 Subject: [PATCH 1/2] Add Convex Hull in Beef --- archive/b/beef/ConvexHull.bf | 180 +++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 archive/b/beef/ConvexHull.bf diff --git a/archive/b/beef/ConvexHull.bf b/archive/b/beef/ConvexHull.bf new file mode 100644 index 000000000..32ee1e154 --- /dev/null +++ b/archive/b/beef/ConvexHull.bf @@ -0,0 +1,180 @@ +using System; +using System.Collections; + +namespace ConvexHull; + +struct Point +{ + public T x; + public T y; + + public this(T xValue, T yValue) + { + x = xValue; + y = yValue; + } + + public override void ToString(String str) + { + str.AppendF("({}, {})", x, y); + } +} + +class PointsList : List> +{ + public this() + { + } + + public this(List xValues, List yValues) + { + for (int i < xValues.Count) + { + this.Add(.(xValues[i], yValues[i])); + } + } + + public override void ToString(String str) + { + for (Point point in this) + { + str.AppendF("{}\n", point); + } + } +} + +class Program +{ + public static void Usage() + { + Console.WriteLine( + """ + Usage: please provide at least 3 x and y coordinates as separate lists (e.g. "100, 440, 210") + """ + ); + Environment.Exit(0); + } + + public static Result ParseInt(StringView str) + where T : IParseable + { + StringView trimmedStr = scope String(str); + trimmedStr.Trim(); + + // T.Parse ignores single quotes since they are treat as digit separators -- e.g. 1'000 + if (trimmedStr.Contains('\'')) + { + return .Err; + } + + return T.Parse(trimmedStr); + } + + public static Result ParseIntList(StringView str, List arr) + where T: IParseable + { + arr.Clear(); + for (StringView item in str.Split(',')) + { + switch (ParseInt(item)) + { + case .Ok(let val): + arr.Add(val); + + case .Err: + return .Err; + } + } + + return .Ok; + } + + // Find Convex Hull using Jarvis' algorithm + // Source: https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/ + public static void ConvexHull(PointsList points, PointsList hullPoints) + where T : IMinMaxValue, operator T - T, operator T * T + where int : operator T <=> T + { + int n = points.Count; + + // Initialize hull points + hullPoints.Clear(); + + // The first point is the leftmost point with the highest y-coord in the + // event of a tie + int l = 0; + for (int i in 1.. points[i].y)) + { + l = i; + } + } + + // Repeat until wrapped around to first hull point + int p = l; + repeat + { + // Store convex hull point + hullPoints.Add(points[p]); + + int q = (p + 1) % n; + for (int j < n) + { + // If point j is more counter-clockwise, then update end point (q) + if (Orientation(points[p], points[j], points[q]) < 0) + { + q = j; + } + } + + p = q; + } + while (p != l); + } + + // Get orientation of three points + // + // 0 = points are in a line + // > 0 = points are clockwise + // < 0 = points are counter-clockwise + public static int Orientation(Point p, Point q, Point r) + where T : operator T - T, operator T * T + where int : operator T <=> T + { + return (q.y - p.y) * (r.x - q.x) <=> (q.x - p.x) * (r.y - q.y); + } + + public static int Main(String[] args) + { + if (args.Count < 2) + { + Usage(); + } + + List xValues = scope .(); + if (ParseIntList(args[0], xValues) case .Err) + { + Usage(); + } + + List yValues = scope .(); + if (ParseIntList(args[1], yValues) case .Err) + { + Usage(); + } + + if (xValues.Count != yValues.Count || xValues.Count < 3) + { + Usage(); + } + + PointsList points = scope .(xValues, yValues); + PointsList hullPoints = scope .(); + ConvexHull(points, hullPoints); + Console.WriteLine(hullPoints); + + return 0; + } +} From 500e76ba7b802f286b4263fd0356e8494c04183a Mon Sep 17 00:00:00 2001 From: Ron Zuckerman Date: Sat, 17 Feb 2024 13:36:58 -0600 Subject: [PATCH 2/2] Remove IMinMaxValue --- archive/b/beef/ConvexHull.bf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archive/b/beef/ConvexHull.bf b/archive/b/beef/ConvexHull.bf index 32ee1e154..6d5f60604 100644 --- a/archive/b/beef/ConvexHull.bf +++ b/archive/b/beef/ConvexHull.bf @@ -92,7 +92,7 @@ class Program // Find Convex Hull using Jarvis' algorithm // Source: https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/ public static void ConvexHull(PointsList points, PointsList hullPoints) - where T : IMinMaxValue, operator T - T, operator T * T + where T : operator T - T, operator T * T where int : operator T <=> T { int n = points.Count;