-
-
Notifications
You must be signed in to change notification settings - Fork 361
Add Jarvis March in C#. #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leios
merged 7 commits into
algorithm-archivists:master
from
june128:addJarvisMarchCSharpPR
May 6, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d9419b
Add Jarvis March in C#.
june128 66a44a9
Make JarvisMarch non-static. Simplify RunJarvisMarch's name to Run. A…
june128 570e493
Make Vector a struct and make fields readonly. Overload operator for …
june128 b91141c
Refactor and simplify code. Update mention. Resolve compiler warnings.
june128 8f0d3af
Fix mention.
june128 7e57903
Simplify code. Improve comments. Remove redundant operators.
june128 2919001
Merge branch 'master' into addJarvisMarchCSharpPR
leios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
chapters/computational_geometry/gift_wrapping/jarvis_march/code/cs/JarvisMarch.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// submitted by Julian Schacher (jspp) with great help by gustorn | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace JarvisMarch | ||
{ | ||
public struct Vector | ||
{ | ||
public readonly int x; | ||
public readonly int y; | ||
|
||
public Vector(int xValue, int yValue) | ||
{ | ||
this.x = xValue; | ||
this.y = yValue; | ||
} | ||
|
||
public override bool Equals(object obj) => obj is Vector v && this.x == v.x && this.y == v.y; | ||
public override int GetHashCode() => (17 * 23 + this.x) * 23 + this.y; | ||
|
||
public static bool operator==(Vector a, Vector b) => a.Equals(b); | ||
public static bool operator!=(Vector a, Vector b) => !(a == b); | ||
} | ||
|
||
public class JarvisMarch | ||
{ | ||
public List<Vector> Run(List<Vector> points) | ||
{ | ||
var convexHull = new List<Vector>(); | ||
|
||
// Set the intial pointOnHull to the point of the list, where the x-position is the lowest. | ||
var pointOnHull = points.Aggregate((leftmost, current) => leftmost.x < current.x ? leftmost : current); | ||
|
||
// Continue searching for the next pointOnHull until the next pointOnHull is equal to the first point of the convex hull. | ||
do | ||
{ | ||
convexHull.Add(pointOnHull); | ||
|
||
// Search for the next pointOnHull by looking which of the points is the next most outer point. | ||
pointOnHull = points.Aggregate((potentialNextPointOnHull, current) => | ||
{ | ||
// Returns true, if potentialNextPointOnHull is equal to the current pointOnHull or if the current point is left of the line defined by pointOnHull and potentialNextPointOnHull. | ||
if (potentialNextPointOnHull == pointOnHull || IsLeftOf(pointOnHull, potentialNextPointOnHull, current)) | ||
return current; | ||
return potentialNextPointOnHull; | ||
}); | ||
|
||
// Check if the gift wrap is completed. | ||
} while (pointOnHull != convexHull[0]); | ||
|
||
return convexHull; | ||
} | ||
|
||
// Returns true, if p is left of the line defined by a and b. | ||
private bool IsLeftOf(Vector a, Vector b, Vector p) => (b.x - a.x) * (p.y - a.y) > (p.x - a.x) * (b.y - a.y); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
chapters/computational_geometry/gift_wrapping/jarvis_march/code/cs/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// submitted by Julian Schacher (jspp) with great help by gustorn | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace JarvisMarch | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
System.Console.WriteLine("JarvisMarch"); | ||
// Example list of points. | ||
// The points are represented by vectors here, but that doesn't really matter. | ||
var points = new List<Vector>() | ||
{ | ||
new Vector(1, 3), | ||
new Vector(2, 4), | ||
new Vector(4, 0), | ||
new Vector(1, 0), | ||
new Vector(0, 2), | ||
new Vector(2, 2), | ||
new Vector(3, 4), | ||
new Vector(3, 1), | ||
}; | ||
var jarvisMarch = new JarvisMarch(); | ||
var giftWrap = jarvisMarch.Run(points); | ||
|
||
// Print the points of the gift wrap. | ||
foreach (var point in giftWrap) | ||
System.Console.WriteLine($"{point.x}, {point.y}"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Public fields should be Pascal case