Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-10

C translation question

20 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

I’m trying to translate a function from C (taken from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html) and although I think I got it correctly it’s not behaving 100% accurately so I’m not sure if it was my translation or not. The “for” statement is pretty confusing to my eyes despite the author of the function giving a description of his code.

Does anyone spot any errors with my translation?

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) {

  int i, j, c = 0;
  for (i = 0, j = nvert-1; i < nvert; j = i++) {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
       c = !c;
  }
  return c;

}

function TPolygonHelper.ContainsPoint (point: TPoint): boolean;
var
	i, j, c: integer;
begin
	i := 0;
	j := high(self);
	c := 0;
	for i := 0 to high(self) do
		begin
			j := i + 1;
			if ((self[i].y > point.y)  (self[j].y > point.y))
					and (point.x < (self[j].x - self[i].x) * (point.y - self[i].y) / (self[j].y - self[i].y) + self[i].x) then
				c := not c;
		end;
	result := c  0;
end;

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to C Western
On Oct 22, 2016, at 4:45 PM, C Western wrote: > j := i + 1; might want this at the end of the loop.

Doing that defiantly broke it. I think “j” is being set inside the for() construct and before each pass of the loop. The author explains the for loop even but the fact it requires an explanation makes my worry about how easy it is to mess up. :)

Regards,
Ryan Joseph

Reply by email

José Mejuto
to Ryan Joseph
Ryan Joseph wrote
>> j := i + 1; > might want this at the end of the loop. Doing that defiantly broke it. I think “j” is being set inside the for() construct and before each pass of the loop. The author explains the for loop even but the fact it requires an explanation makes my worry about how easy it is to mess up. :)

Hello,

It is at the end of the loop for sure, it points to the "previous" point in the polygon and in the case of the first testing point the "previous" one is the last one.

So the correct code is:

   j := i;

This is my automated C code conversion for that function:

function pnpoly(nvert: Integer; vertx: PointerTo_Single; verty:
PointerTo_Single; testx: Single; testy: Single): Integer;
var
   c: Integer = 0;
   j: Integer = 0;
   i: Integer = 0;
begin
   {INITCODE} c := 0;
   {INITCODE} j := 0;
   {INITCODE} i := 0;
   i := 0;
   j := nvert - 1;
   while i < nvert do
   begin
     if IsTrue(IsTrue(((verty[i] > testy)  (verty[j] > testy)))
       AND
       IsTrue((testx < (vertx[j] - vertx[i]) * (testy - verty[i])
              div  (verty[j] - verty[i]) + vertx[i]))) then
     begin
       c := BooleanNot (c);
     end;
     j := PostInc(i);
   end;
   exit (c);
end;

Reply by email

Bernd Oppolzer
to Ryan Joseph: I’m trying to translate a function from C (taken from…

it might help if I translate the C for loop into an equivalent while loop for you, (and eliminating the ++ construct), simply mechanically, without knowing anything about the application.

This results in:

int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) {

   int i, j, c = 0;
   i = 0, j = nvert-1;
   while (i < nvert)
   {
     if (((verty [i] > testy) != (verty [j] > testy)) &&
          (testx < (vertx[j] - vertx[i]) * (testy - verty [i])
           / (verty [j] - verty [i]) + vertx [i]))
        c = !c;
     j = i;
     i = i + 1;
   }
   return c;
}

HTH,

kind regards

Bernd

Ryan Joseph wrote 39 lines
I’m trying to translate a function from C (taken from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html) and although I think I got it correctly it’s not behaving 100% accurately so I’m not sure if it was my translation or not. The “for” statement is pretty confusing to my eyes despite the author of the function giving a description of his code. Does anyone spot any errors with my translation? int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, c = 0; for (i = 0, j = nvert-1; i < nvert; j = i++) { if ( ((verty[i]>testy) != (verty[j]>testy)) && (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) ) c = !c; } return c; } function TPolygonHelper.ContainsPoint (point: TPoint): boolean; var i, j, c: integer; begin i := 0; j := high(self); c := 0; for i := 0 to high(self) do begin j := i + 1; if ((self[i].y > point.y) (self[j].y > point.y)) and (point.x < (self[j].x - self[i].x) * (point.y - self[i].y) / (self[j].y - self[i].y) + self[i].x) then c := not c; end; result := c 0; end; Regards, Ryan Joseph _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Bernd Oppolzer
to Bernd Oppolzer

What I wanted to say:

when translating the for loop to Pascal, you are in danger of introducing logic errors, because the Pascal for loop is kind of restricted compared to the C for loop (which is in fact simply a while loop with another notation).

But if you translate the C for loop to a while loop first, then you can translate this loop in a straightforward manner to Pascal, and the probability
of inserting logic errors is much lower.

Kind regards

Bernd

Bernd Oppolzer wrote 84 lines
it might help if I translate the C for loop into an equivalent while loop for you, (and eliminating the ++ construct), simply mechanically, without knowing anything about the application. This results in: int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, c = 0; i = 0, j = nvert-1; while (i < nvert) { if (((verty [i] > testy) != (verty [j] > testy)) && (testx < (vertx[j] - vertx[i]) * (testy - verty [i]) / (verty [j] - verty [i]) + vertx [i])) c = !c; j = i; i = i + 1; } return c; } HTH, kind regards Bernd Am 22.10.2016 um 11:06 schrieb Ryan Joseph: > I’m trying to translate a function from C (taken from > http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html) > and although I think I got it correctly it’s not behaving 100% > accurately so I’m not sure if it was my translation or not. The “for” > statement is pretty confusing to my eyes despite the author of the > function giving a description of his code. > > Does anyone spot any errors with my translation? > > int pnpoly(int nvert, float *vertx, float *verty, float testx, float > testy) > { > int i, j, c = 0; > for (i = 0, j = nvert-1; i < nvert; j = i++) { > if ( ((verty[i]>testy) != (verty[j]>testy)) && > (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / > (verty[j]-verty[i]) + vertx[i]) ) > c = !c; > } > return c; > } > > > function TPolygonHelper.ContainsPoint (point: TPoint): boolean; > var > i, j, c: integer; > begin > i := 0; > j := high(self); > c := 0; > for i := 0 to high(self) do > begin > j := i + 1; > if ((self[i].y > point.y) (self[j].y > point.y)) > and (point.x < (self[j].x - self[i].x) * (point.y > - self[i].y) / (self[j].y - self[i].y) + self[i].x) then > c := not c; > end; > result := c 0; > end; > > Regards, > Ryan Joseph > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Lars
to José Mejuto: It is at the end of the loop for sure, it points to the "previous" point in the polygon…
José Mejuto wrote 13 lines
Hello, It is at the end of the loop for sure, it points to the "previous" point in the polygon and in the case of the first testing point the "previous" one is the last one. So the correct code is: j := i; This is my automated C code conversion for that function:

..

Hi, what is the status of automated conversion tools? Last I remember reading about them was when I read a delphi page on how to convert header files. Are you saying that nowadways you can actually convert plain C code to fpc, not just header files but all C code? Just certain types of C code? Are there any competing tools or everyone uses just one main tool?

Thanks! Interested

Reply by email

Lars
to Bernd Oppolzer: What I wanted to say: when translating the for loop to Pascal, you are in danger of…

On Sun, October 23, 2016 2:31 pm, Bernd Oppolzer wrote

But if you translate the C for loop to a while loop first,

Agree! For loops, are not nearly as flexible and powerful as while loops. For loops can get you stuck in a certain mode of thinking and you try to develop hacks to manipulate the for loop to do what you want to do, when in fact just using a while loop with more custom control is the way to go.

I love for loops for their simplicity, but difficult problems cannot be solved via for loops.

Reply by email

José Mejuto
to Lars: .. Hi, what is the status of automated conversion tools? Last I remember reading about…
Lars wrote 5 lines
Hi, what is the status of automated conversion tools? Last I remember reading about them was when I read a delphi page on how to convert header files. Are you saying that nowadways you can actually convert plain C code to fpc, not just header files but all C code? Just certain types of C code? Are there any competing tools or everyone uses just one main tool?

Hello,

I don't know, wrote this tool to help me in the past to translate Zint barcode source from C to pascal, in special the maths involving "++", "--" and other funny "C" expressions.

It is far away from a real code translator, and it is dummy as it does not analyze the type of each variable, instead it uses them as a variable of any type and it simply try to put it in the same context as the "C" one. When it finds things like:

if (a) { ...

It will convert it to

if IsTrue(a) then ...

So you must provide an IsTrue function for that case (integer, char, byte...) and many times you need to manually adjust some lines.

Or if it finds:

a++;

It will output:

PostInc(a);

Where PostInc is:

function PostInc(var aValue: integer): integer;
begin
   Result:=aValue;
   aValue:=aValue + 1;
end;

It uses GoldParser so maybe it only works in Windows :-?, only very basic "C" is handled and is mostly function by function oriented.

If you want to test it I can provide .exe or source code, but do not expect it to translate anything far than some trivial functions.

Reply by email

Lars
to José Mejuto
José Mejuto wrote 15 lines
El 24/10/2016 a las 1:40, Lars escribió: > Hi, what is the status of automated conversion tools? Last I remember > reading about them was when I read a delphi page on how to convert > header files. Are you saying that nowadways you can actually convert > plain C code to fpc, not just header files but all C code? Just certain > types of C code? Are there any competing tools or everyone uses just > one main tool? Hello, I don't know, wrote this tool to help me in the past to translate Zint barcode source

...

It is far away from a real code translator,

...

Show quoted text 6 lines
It uses GoldParser so maybe it only works in Windows :-?, only very basic "C" is handled and is mostly function by function oriented. If you want to test it I can provide .exe or source code, but do not expect it to translate anything far than some trivial functions.

Do you have github account... or sourceforge, I think a few other people may find it useful...

My dream is some day to convert this:
http://duro.sourceforge.net/

to freepascal or GoLang, however I don't really feel like doing it all manually, would rather automate a lot of it.

AFAIK it's plain C, been a while since I is more C code I'd like to convert too, and I'm sure others would be interested in your source... But it is up to you, I don't know if you just don't feel like making it public on github or sourceforge

Reply by email

Lars
to Lars
Lars wrote 11 lines
I previously wrote: > >> Are you saying that nowadways you can actually convert >> plain C code to fpc, Found this too: http://c2pas.sourceforge.net/

And there is also this interesting find:

http://cc.embarcadero.com/item/26951

(C to Pascal Converter 2.1.7.2015)

Looks as though there are multiple efforts, too bad these people didn't combine forces and create a github account and work together on these tools.

There is also this:
http://www.astonshell.com/freeware/c2pas32/

Reply by email

Ryan Joseph
to Bernd Oppolzer: it might help if I translate the C for loop into an equivalent while loop for you, (and…

I just got back to this problem now. Yes, changing it to a while loop fixed the little bug and cleans things up in the translation for sure. Thanks for the tip.

Show quoted text 21 lines
On Oct 24, 2016, at 3:20 AM, Bernd Oppolzer wrote: int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, c = 0; i = 0, j = nvert-1; while (i < nvert) { if (((verty [i] > testy) != (verty [j] > testy)) && (testx < (vertx[j] - vertx[i]) * (testy - verty [i]) / (verty [j] - verty [i]) + vertx [i])) c = !c; j = i; i = i + 1; } return c; }

Regards,
Ryan Joseph

Reply by email

José Mejuto
to Lars: ... ... Do you have github account... or sourceforge, I think a few other people may find…
Lars wrote
> If you want to test it I can provide .exe or source code, but do not > expect it to translate anything far than some trivial functions. Do you have github account... or sourceforge, I think a few other people may find it useful...

Hello,

No sorry, no github, sourceforge (maybe yes, I can not recall).

Anyway I had sent you off the list a link to download my current implementation, test it and if you think it could be useful for anybody you can publish it or maybe I should open a github account :-)

Let me know if you was able to compile it. Thank you.

Reply by email

Lars
to José Mejuto
José Mejuto wrote 13 lines
El 25/10/2016 a las 3:56, Lars escribió: >> If you want to test it I can provide .exe or source code, but do not >> expect it to translate anything far than some trivial functions. > Do you have github account... or sourceforge, I think a few other > people may find it useful... Hello, No sorry, no github, sourceforge (maybe yes, I can not recall).

...

maybe I should open a github account :-)

Code that is locked away on someone's hard drive is much less useful than if publicly published :-)
In fact I have a lot of code on my drive that should be uploaded. Just that I'm tired of learning new revision control systems, I was already getting used to SVN when they pulled the plug on that and no one uses it much any more as github is hot with the new kids on the block these days.

Reply by email

Graeme Geldenhuys
to Lars
Lars wrote
as github is hot with the new kids on the block these days.

Yes, for good reason! Though I would correct your statement as "Git is hot...". You don't have to use GitHub or any other public Git repository hosting environment.

Regards,
Graeme

Reply by email

Lars
to Graeme Geldenhuys
Graeme Geldenhuys wrote 8 lines
On 2016-10-30 21:12, Lars wrote: > as github is hot with the new kids on the block these days. Yes, for good reason! Though I would correct your statement as "Git is hot...". You don't have to use GitHub or any other public Git repository hosting environment.

I considered just using an SVN account elsewhere since I know SVN already.. however the issue is, no one uses svn any more (I know people use it) so for sharing code you pretty much have to use github and follow the other sheep. I don't mind the github GUI, in fact it's web based portal is extremely good. I'm just skeptical of github being replaced by yet another tool in 3 years just as CVS was replaced by SVN, and SVN replaced by CVS.

I think the reason github is successful is because it has a wicked GUI interface... github.com. Compared to the crap you see on source forge with their broken ugly CVS viewers in html, or svn viewers.

The issue with github is that it encourages a lot of forking rather than working together as CVS did.

Actually that begs the question, does freepascal still use SVN as their development revision system? I'm completely out of touch with what's happening with fpc dev

Reply by email

Bo Berglund
to Lars
"Lars" wrote
The issue with github is that it encourages a lot of forking rather than working together as CVS did.

We still use CVS and its ability to handle multiple developers on the same code files is one important thing.
As is the fact that we have used it for such a long time (since 2000) and literally everything is in our CVS repository...

Bo Berglund Developer in Sweden

Reply by email

Lars
to Bo Berglund
Bo Berglund wrote 8 lines
On Sun, 30 Oct 2016 21:15:03 -0600, "Lars" wrote: > The issue with github is that it encourages a lot of forking rather > than working together as CVS did. We still use CVS

I just wish CVS and SVN had as nice of a GUI front end (like github). That's the one thing I really like about git is the nice GUI access, simple readme system for projects etc. With CVS and SVN gui's and website front ends I never found anything that pleasing, although I haven't looked at third party front ends.

Reply by email

Marc Santhoff
to Lars
Lars wrote 5 lines
I just wish CVS and SVN had as nice of a GUI front end (like github). That's the one thing I really like about git is the nice GUI access, simple readme system for projects etc. With CVS and SVN gui's and website front ends I never found anything that pleasing, although I haven't looked at third party front ends.

http://www.twobarleycorns.net/tkcvs.html

I'm using TkCVS for many years now. Really easy to handle und understand, and it is capacle of doing the same for SVN. It does run on Windows, too. On the downside you have to edit a file and insert directory and project name in a file for TkCVS to know what it is doing. Only needed once for a new source tree.

I like it.

HTH,
Marc

Reply by email