Solved

Convert C code to C#

Posted on 2009-03-29
6
506 Views
Last Modified: 2012-08-13
I need to convert this C code to C#

My conversion identifies my polygon as ccw. I know it is cw.
Did I make a mistake converting from C to C#?
I noted the assumption that the last point is not repeated and have tried my code with n-1, but still returns ccw.
Also, my polygon is in the Western Hemisphere, meaning that Longitude is expressed as a negative number.
I have tried all the solutions to deal with the negative number I can think of.
Thanks for any help,
Michael



C function by Paul Bourke 
 

/*

   Return the clockwise status of a curve, clockwise or counterclockwise

   n vertices making up curve p

   return 0 for incomputables eg: colinear points

          CLOCKWISE == 1

          COUNTERCLOCKWISE == -1

   It is assumed that

   - the polygon is closed

   - the last point is not repeated.

   - the polygon is simple (does not intersect itself or have holes)

*/

int ClockWise(XY *p,int n)

{

   int i,j,k;

   int count = 0;

   double z;
 

   if (n < 3)

      return(0);
 

   for (i=0;i<n;i++) {

      j = (i + 1) % n;

      k = (i + 2) % n;

      z  = (p[j].x - p[i].x) * (p[k].y - p[j].y);

      z -= (p[j].y - p[i].y) * (p[k].x - p[j].x);

      if (z < 0)

         count--;

      else if (z > 0)

         count++;

   }

   if (count > 0)

      return(COUNTERCLOCKWISE);

   else if (count < 0)

      return(CLOCKWISE);

   else

      return(0);

}

But I don't remember much C

I have tried to rewrite to C#:

private static int ClockWise(double[] y, double[]x, int n)

			

		{

			int i,j,k;

			int count = 0;

			double z;
 

			if (n < 3)//not a polygon

				return(0);
 

			for (i=0;i<n-1;i++) 

			{

				j = (i + 1) % n;

				k = (i + 2) % n;

				z  = (x[j] - x[i]) * (y[k] - y[j]);

				z -= (y[j] - y[i]) * (x[k] - x[j]);

				if (z < 0)

					count--;

				else if (z > 0)

					count++;

			}

			if (count > 0)//counterclockwise

				return(1);

			else if (count < 0)//clockwise

				return(-1);

			else

				return(0);

		}

Open in new window

0
Comment
Question by:MichaelMullin
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24015899
try this one out
what i have tried here is creating a point class and using a generic list
i am assuming that you C program i taking the input of points array which is similar to the List<Point>
class Point

{

	public double x;

	public double y;

}
 

private int ClockWise (List<Point> p, int n)

{

	int i, j, k;

	int count = 0;

	double z;
 

	if (n < 3)

		return (0);
 

	for (i = 0; i < n; i++)

	{

		j = (i + 1) % n;

		k = (i + 2) % n;

		z = (p[j].x - p[i].x) * (p[k].y - p[j].y);

		z -= (p[j].y - p[i].y) * (p[k].x - p[j].x);

		if (z < 0)

			count--;

		else if (z > 0)

			count++;

	}

	if (count > 0)

		return (-1);

	else if (count < 0)

		return (1);

	else

		return (0);

}

Open in new window

0
 

Author Comment

by:MichaelMullin
ID: 24018068
Thank-you,
No, I do not use C. The question is: did I properly translate the C code to C#? My C# translation works for a simple 4-sided polygon but not for a 2500-sided polygon. Either the original C code is incorrect or my translation is incorrect. The simplest solution would be that my translation is incorrect. If you are able to assure me that my translation is either correct or incorrect, that would be the solution to this question.
0
 

Accepted Solution

by:
MichaelMullin earned 0 total points
ID: 24023176
I have solved this problem, and would like to close this question. I did not actually get an answer to my question, but than-you ragi0017: for you input. If someone still wants to answer the basic question, that is: Is my translation of the C code to C# correct or not, I am still interested. If the translation is correct, the C code algorithm is incorrect (or not robust enough for my needs) I did find another alorithm which I was able to adapt to C# and it works.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24025306
no problems you can close the question
0
 

Author Comment

by:MichaelMullin
ID: 24025386
Even though I have solved this problem, I would still like to know if my translation from C to C# is correct or not. I will award the points for a yes or no answer with explanation if incorrect.
0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 30720329
Although it is irrelevant, but for those who too want to check if conversion is good or not, then with my knowledge it is good.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - break the dataset in chunks? 7 39
Error in JQuery 5 39
Device Location 4 13
Coding C# in Linux 8 34
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now