Jarvis March Algorithm problem

closed account (3UCfGNh0)
Hi There

Im new to this site. Im currently implementing jarvis march algorithm with breezenhams line drawing. Im currently having problems with this as the line drawing does not work with the convex hull. Im not sure why.

Jarvis March Code:
// There must be at least 3 points
if (n < 3)
return;

// Initialize Result
int *next = new int[n];
for (int i = 0; i < n; i++)
next[i] = -1;
int *order = new int[n];
// Find the leftmost point
int l = 0;
for (int i = 1; i < n; i++)
if (flies[i].X1 < flies [l].X2)
l = i;

// Start from leftmost point, keep moving counterclockwise
// until reach the start point again
int p = l, q;
int count = 0;
do
{
order[count] = p;
count++;
// Search for a point 'q' such that orientation(p, i, q) is
// counterclockwise for all points 'i'
q = (p + 1) % n;
for (int i = 0; i < n; i++)
if (orientation(flies[p], flies[i], flies[q]) < 0)
{
q = i;
}


next[p] = q; // Add q to result as a next point of p
p = q; // Set p as q for next iteration
}
while (p != l);

// Print Result
for (int i = 0; i < n; i++)
{
if (next[i] != -1)
{
graphics.breezenhams(flies[order[i]].X1, flies[order[i]].Y1, flies[order[i]].X2, flies[order[i]].Y2); //call the breezenhams algorithm passing in the points
cout << "(" << flies[i].X1 << ", " << flies[i].Y1 << ")\n";
}

else
{
graphics.breezenhams(flies[order[i]].X1, flies[order[i]].Y1, flies[order[i]].X2, flies[order[i]].Y2); //call the breezenhams algorithm passing in the points
}
}

Breezenhams:


float dx = abs(X1 - X2);
float dy = abs(Y1 - Y2);

float x, y;


//create delta x and delta y
float twoDy = 2 * dy; //create two delta y
float twoDYDX = 2 * (dy - dx); // create variable that multiplies the result of subtracting dy from dx

float xEnd; //variable to store the end of the line

//parameter variable
float p = 2 * dy - dx;

//if the position of x0 is greater than x1
if (X1 > X2)
{
//move x to the x1 variable
x = X2;
//move y to the y1 variable
y = Y2;
//xend variable will store x0
xEnd = X1;
}
else
//if x0 is less than x1
{
//move the value of x0 to x
x = X1;
//move the value of y0 to y
y = Y1;
//xEnd variable will hold the value of x1
xEnd = X2;
}
//use the draw pixel to draw the line
drawPixel(X2, Y2);
//create while loop, argument is that if x is less than the end value of x
while (x < xEnd)
{
//increment x
x++;
//if the parameter is less than 0
if (p < 0)

p += twoDy;
else
{
//increment the y variable
y++;
p += twoDYDX;
}
//use the draw pixel
drawPixel(X2, Y2);
}

Can anyone please help me with this as it is frustrating.
Thanks
Topic archived. No new replies allowed.