Jarvis algorithm (gift-wrapping) or graham-scan - C#
I have a list of Shapes and I have to make a convex hull around them. I tried to do a gift-wrapping algorithm (Jarvis). So far I have foud a plenty of pseudocodes and I understand the logic, but I can't program it. Could only do the 1st step - finding the lowest & "leftest" shape.
List<Point> Ob = new List<Point>();
Point p = new Point();
for (int i = 0; i < L.Count - 1; i++)
{
if (L[i].CoordinateX < L[i + 1].CoordinateX) p.X = (int)L[i].CoordinateX;
if (L[i].CoordinateY < L[i + 1].CoordinateY) p.Y = (int)L[i].CoordinateY;
}
Ob.Add(p);
So then I tried to find the smallest angle. My logic was that the smaller angle the smaller the cos of this angle
double angle = 0, angle1 = 0;
int num = 0;
for (int k = 0; k < L.Count - 1; k++)
{
angle = Math.Cos((Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((L[k].CoordinateY - p.Y), 2))));
angle1 = Math.Cos((Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k + 1].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((L[k + 1].CoordinateY - p.Y), 2))));
if (angle < angle1) num = k;
}
Point p1 = new Point((int)L[num].CoordinateX, (int)L[num].CoordinateY);
e.Graphics.DrawLine(new Pen(Color.Black), p, p1);
But this code ended in this added 3 circles first, then added 1 more and so on. Obviously this doesn't make any sense.
And this
Do you know any realizations of Graham or Jarvis to look at?
c# algorithm graphics grahams-scan
add a comment |
I have a list of Shapes and I have to make a convex hull around them. I tried to do a gift-wrapping algorithm (Jarvis). So far I have foud a plenty of pseudocodes and I understand the logic, but I can't program it. Could only do the 1st step - finding the lowest & "leftest" shape.
List<Point> Ob = new List<Point>();
Point p = new Point();
for (int i = 0; i < L.Count - 1; i++)
{
if (L[i].CoordinateX < L[i + 1].CoordinateX) p.X = (int)L[i].CoordinateX;
if (L[i].CoordinateY < L[i + 1].CoordinateY) p.Y = (int)L[i].CoordinateY;
}
Ob.Add(p);
So then I tried to find the smallest angle. My logic was that the smaller angle the smaller the cos of this angle
double angle = 0, angle1 = 0;
int num = 0;
for (int k = 0; k < L.Count - 1; k++)
{
angle = Math.Cos((Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((L[k].CoordinateY - p.Y), 2))));
angle1 = Math.Cos((Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k + 1].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((L[k + 1].CoordinateY - p.Y), 2))));
if (angle < angle1) num = k;
}
Point p1 = new Point((int)L[num].CoordinateX, (int)L[num].CoordinateY);
e.Graphics.DrawLine(new Pen(Color.Black), p, p1);
But this code ended in this added 3 circles first, then added 1 more and so on. Obviously this doesn't make any sense.
And this
Do you know any realizations of Graham or Jarvis to look at?
c# algorithm graphics grahams-scan
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34
add a comment |
I have a list of Shapes and I have to make a convex hull around them. I tried to do a gift-wrapping algorithm (Jarvis). So far I have foud a plenty of pseudocodes and I understand the logic, but I can't program it. Could only do the 1st step - finding the lowest & "leftest" shape.
List<Point> Ob = new List<Point>();
Point p = new Point();
for (int i = 0; i < L.Count - 1; i++)
{
if (L[i].CoordinateX < L[i + 1].CoordinateX) p.X = (int)L[i].CoordinateX;
if (L[i].CoordinateY < L[i + 1].CoordinateY) p.Y = (int)L[i].CoordinateY;
}
Ob.Add(p);
So then I tried to find the smallest angle. My logic was that the smaller angle the smaller the cos of this angle
double angle = 0, angle1 = 0;
int num = 0;
for (int k = 0; k < L.Count - 1; k++)
{
angle = Math.Cos((Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((L[k].CoordinateY - p.Y), 2))));
angle1 = Math.Cos((Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k + 1].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((L[k + 1].CoordinateY - p.Y), 2))));
if (angle < angle1) num = k;
}
Point p1 = new Point((int)L[num].CoordinateX, (int)L[num].CoordinateY);
e.Graphics.DrawLine(new Pen(Color.Black), p, p1);
But this code ended in this added 3 circles first, then added 1 more and so on. Obviously this doesn't make any sense.
And this
Do you know any realizations of Graham or Jarvis to look at?
c# algorithm graphics grahams-scan
I have a list of Shapes and I have to make a convex hull around them. I tried to do a gift-wrapping algorithm (Jarvis). So far I have foud a plenty of pseudocodes and I understand the logic, but I can't program it. Could only do the 1st step - finding the lowest & "leftest" shape.
List<Point> Ob = new List<Point>();
Point p = new Point();
for (int i = 0; i < L.Count - 1; i++)
{
if (L[i].CoordinateX < L[i + 1].CoordinateX) p.X = (int)L[i].CoordinateX;
if (L[i].CoordinateY < L[i + 1].CoordinateY) p.Y = (int)L[i].CoordinateY;
}
Ob.Add(p);
So then I tried to find the smallest angle. My logic was that the smaller angle the smaller the cos of this angle
double angle = 0, angle1 = 0;
int num = 0;
for (int k = 0; k < L.Count - 1; k++)
{
angle = Math.Cos((Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k].CoordinateX - p.X), 2) + Math.Pow((L[k].CoordinateY - p.Y), 2))));
angle1 = Math.Cos((Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((p.Y - L[k + 1].CoordinateY), 2))) / (Math.Sqrt(Math.Pow((L[k + 1].CoordinateX - p.X), 2) + Math.Pow((L[k + 1].CoordinateY - p.Y), 2))));
if (angle < angle1) num = k;
}
Point p1 = new Point((int)L[num].CoordinateX, (int)L[num].CoordinateY);
e.Graphics.DrawLine(new Pen(Color.Black), p, p1);
But this code ended in this added 3 circles first, then added 1 more and so on. Obviously this doesn't make any sense.
And this
Do you know any realizations of Graham or Jarvis to look at?
c# algorithm graphics grahams-scan
c# algorithm graphics grahams-scan
edited Nov 12 '18 at 18:04
asked Nov 12 '18 at 16:47
Moth Lamp
112
112
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34
add a comment |
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266611%2fjarvis-algorithm-gift-wrapping-or-graham-scan-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266611%2fjarvis-algorithm-gift-wrapping-or-graham-scan-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can check this example of Graham scan: github.com/MathBunny/convex-hull-visualizer/blob/master/app/src/…
– MathBunny
Nov 12 '18 at 23:34