Jarvis algorithm (gift-wrapping) or graham-scan - C#












2














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?










share|improve this question
























  • 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
















2














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?










share|improve this question
























  • 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














2












2








2







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues