C translation question

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

C translation question

Ryan Joseph-3
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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

C Western-2
On 22/10/16 10:06, Ryan Joseph wrote:
> j := i + 1;
might want this at the end of the loop.

Colin
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Ryan Joseph-3


> On Oct 22, 2016, at 4:45 PM, C Western <[hidden email]> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

José Mejuto
El 22/10/2016 a las 11:59, Ryan Joseph escribió:

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


--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Bernd Oppolzer
In reply to this post by Ryan Joseph-3
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  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

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


Am 23.10.2016 um 22:20 schrieb Bernd Oppolzer:

> 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  -  [hidden email]
>> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
In reply to this post by José Mejuto
On Sat, October 22, 2016 4:44 am, José Mejuto wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
In reply to this post by Bernd Oppolzer
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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

José Mejuto
In reply to this post by noreply
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 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.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
On Mon, October 24, 2016 5:20 am, José Mejuto wrote:

> 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,

...

> 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 looked at it.

There 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
In reply to this post by José Mejuto
I previously wrote:
>
>> Are you saying that nowadways you can actually convert
>> plain C code to fpc,

Found this too:

http://c2pas.sourceforge.net/

It says it can convert a large portion, but not all, obviously
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
On Mon, October 24, 2016 8:00 pm, Lars wrote:

> 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/


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Ryan Joseph-3
In reply to this post by Bernd Oppolzer
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.

> On Oct 24, 2016, at 3:20 AM, Bernd Oppolzer <[hidden email]> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

José Mejuto
In reply to this post by noreply
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).

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.

--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
On Tue, October 25, 2016 3:31 pm, José Mejuto wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Graeme Geldenhuys-6
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.

Regards,
  Graeme

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
On Sun, October 30, 2016 4:40 pm, Graeme Geldenhuys wrote:
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Bo Berglund
On Sun, 30 Oct 2016 21:15:03 -0600, "Lars"
<[hidden email]> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

noreply
On Sun, November 13, 2016 2:50 am, Bo Berglund wrote:
> On Sun, 30 Oct 2016 21:15:03 -0600, "Lars"
> <[hidden email]> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C translation question

Marc Santhoff-2
On Di, 2016-11-15 at 22:38 -0700, Lars wrote:

> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal