Question

GIFT WRAPPING ALGORITHM OF JARVIS MARCH In mathematics, the convex hull of a set of points is the smallest convex set that coSTEPS TO FOLLOW: 1. Create a Point struct which keeps the x and y positions of a point 2. Find the leftmost point and lets c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define pi 3.14159
typedef struct v1
{
int x, y;
}vertex;
vertex p0;
int process_vertices( int n, vertex *v)
{
int i;
for(i=0;i<n;i++) scanf(" %d %d",&v[i].x,&v[i].y);
return n;
}
void swap(vertex *v1, vertex *v2)
{
vertex temp = *v1;
*v1 = *v2;
*v2 = temp;
}
int orientation(vertex p, vertex q, vertex r)
{
int val = (int)(q.y - p.y) * (r.x - q.x) - ( int)(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
int distSq(vertex p1, vertex p2)
{
return (int)(p1.x - p2.x)*(p1.x - p2.x) + ( int)(p1.y - p2.y)*(p1.y - p2.y);
}
int compare(const void *vp1, const void *vp2)
{
vertex *p1 = (vertex *)vp1;
vertex *p2 = (vertex *)vp2;


int o = orientation(p0, *p1, *p2);
if (o == 0)
return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;

return (o == 2)? -1: 1;
}
vertex * Convex_Hull(vertex *v, int *count)
{
int n = *count, ymin = v[0].y, min = 0, i,m;vertex *stack;
for(i = 1; i < n; i++)
{
if((v[i].y < ymin) || ((v[i].y == ymin) && (v[i].x < v[min].x)))
{
ymin = v[i].y;
min = i;
}
}
swap(&v[0], &v[min]);
p0 = v[0];
if(n > 1)
qsort(&v[1], n - 1, sizeof(vertex), compare);
m = 1;
for(i = 1; i < n; i++)
{
while((i < n - 1) && orientation(v[0], v[i], v[i + 1]) == 0)
i++;
v[m++] = v[i];
}
*count = n = m;
if(n < 3)
return v;
stack = (vertex *)malloc(n * sizeof(vertex));
stack[0] = v[0];
stack[1] = v[1];
stack[2] = v[2];
m = 2;
for(i = 3; i < n; i++)
{
while(orientation(stack[m-1], stack[m], v[i]) != 2)
m--;
stack[++m] = v[i];
}
*count = n = ++m;
free(v);
return stack;
}

int main()
{
int t, n, i,count;

vertex *v;


scanf("%d", &n);


v = (vertex *)malloc( n * sizeof(vertex));
count = process_vertices(n, v);

v = Convex_Hull(v, &count);
for(i=0;i<count;i++)
   printf("%d %d\n",v[i].x,v[i].y);

return 0;
}
/* run this code in online c compiler.*/

Add a comment
Know the answer?
Add Answer to:
GIFT WRAPPING ALGORITHM OF JARVIS MARCH In mathematics, the convex hull of a set of points...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A 3-dimensional surface contains a saddle point when the point is a global minimum along a line parallel to eith...

    A 3-dimensional surface contains a saddle point when the point is a global minimum along a line parallel to either the x or the y axis, but a global maximum along the other axis. You'll usually see a "U" shape meeting an upside-down "U" shape. The classic horseback riding saddle has one such saddle point, and, not coincidentally, it is a rider's most stable position The input will be a 2-dimensional array AInIn] of numbers, representing a lattice approximation of...

  • For the convex hull algorithm we have to be able to test whether a point r lies left or right of the directed line through two points p and q. Let = (px, Py), q , and r-(Tx,rv). a. Show that the sign...

    For the convex hull algorithm we have to be able to test whether a point r lies left or right of the directed line through two points p and q. Let = (px, Py), q , and r-(Tx,rv). a. Show that the sign of the determinant 1 rx iy determines whether r lies left or right of the line. For the convex hull algorithm we have to be able to test whether a point r lies left or right of...

  • Use Table 8.1, a computer, or a calculator to answer the following. Suppose a candidate for...

    Use Table 8.1, a computer, or a calculator to answer the following. Suppose a candidate for public office is favored by only 47% of the voters. If a sample survey randomly selects 2,500 voters, the percentage in the sample who favor the candidate can be thought of as a measurement from a normal curve with a mean of 47% and a standard deviation of 1%. Based on this information, how often (as a %) would such a survey show that...

  • The following results were obtained from an undrained shear box test carried out on a set...

    The following results were obtained from an undrained shear box test carried out on a set of undisturbed soil samples. 0.2 0.8 Normal Load (N) Strain (%) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0 21 46 70 89 107 121 131 136 138 138 137 136 0.4 Shearing force (N) 0 33 72 110 139 164 180 192 201 210 217 224 230 234 237 236 0 45...

  • The following results were obtained from an undrained shear box test carried out on a set...

    The following results were obtained from an undrained shear box test carried out on a set of undisturbed soil samples. 0.2 0.8 Normal Load (N) Strain (%) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0 21 46 70 89 107 121 131 136 138 138 137 136 0.4 Shearing force (N) 0 33 72 110 139 164 180 192 201 210 217 224 230 234 237 236 0 45...

  • Questions 1. Given the H NMR spectrum and molec- ular formula for each of the following compounds, deduce the...

    Questions 1. Given the H NMR spectrum and molec- ular formula for each of the following compounds, deduce the structure of the compound, estimate the chemical shifts of all its protons using the parameters in Tables 22.3–22.5, and assign the NMR sig- nals to their respective protons. (a) C.H,,Cl; 1H NMR (CDC12): 8 3.33 (2H, s); 1.10 (9H, s) (b) C-H,,0,; 1H NMR (CDC12): 8 3.88 (1H, s); 2.25 (3H, s); 1.40 (6H, s) (C) CH,,0,; 1H NMR (CDC1,): 8...

  • Suppose 1000 coins are tossed. Use the normal curve approximation to the binomial distribution to find...

    Suppose 1000 coins are tossed. Use the normal curve approximation to the binomial distribution to find the probability of getting the following result. Exactly 495 heads Use the table of areas under the standard normal curve given below. Click here to view page 1. Click here to view page 2. Click here to view page 3. Click here to view page 4. Click here to view page 5. Click here to view page 6. The probability of getting exactly 495...

  • Suppose 16 coins are tossed. Use the normal curve approximation to the binomial distribution to find...

    Suppose 16 coins are tossed. Use the normal curve approximation to the binomial distribution to find the probability of getting the following result. More than 11 tails. Use the table of areas under the standard normal curve given below. Click here to view page 1. Click here to view page 2. Click here to view page 3. Click here to view page 4. Click here to view page 5. Click here to view page 6. Binomial probability = (Round to...

  • Case Study Notes Case Questions 1- Is Disney liquid compared to its peers? 2- Does Disney...

    Case Study Notes Case Questions 1- Is Disney liquid compared to its peers? 2- Does Disney manage its assets effectively compared to its peers? 3- Does Disney’s debt load suggest trouble paying its creditors? 4- Compare Disney’s profitability to its peers. 21,922 36.5% 46.7% 24,701 41.1% 6,095 38.8% PECP Studio Entertainment 10,065 16.7% 19.1% 3,414 5.7% -738 -4.7% -668 -10 Eliminations Total 59,434 HOW DISNEY MAKES MONEY PARKS, EXPERIENCES & CONSUMER PRODUCTS A previous Disney Case used the company's financial...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT