### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Solved] Checking if the point is within the area !  (Read 5305 times)

• Sr. Member
• Posts: 253
##### [Solved] Checking if the point is within the area !
« on: July 17, 2021, 10:06:55 am »
Hi All,
The code in the https://forum.lazarus.freepascal.org/index.php?topic=42826.0 link that checks whether the point is within the field;
Code: Pascal  [Select][+][-]
1. function PointInPolygon(Point: TPoint; const Polygon: array of TPoint): Boolean;
2. var
3.   rgn: HRGN;
4. begin
5.   rgn := CreatePolygonRgn(Polygon[0], Length(Polygon), WINDING);
6.   Result := PtInRegion(rgn, Point.X, Point.Y);
7.   DeleteObject(rgn);
8. end;
It works on windows, but on android, it gives an error on the CreatePolygonRgn line.

The code in the https://wiki.freepascal.org/Geometry_in_Pascal link that I can use as an alternative is;

Android works but gives wrong result for some situations. Is there an alternative code that I can use instead?
Or will I have to roll up my sleeves and write it myself ?
I would be glad if you help.

« Last Edit: July 17, 2021, 01:53:16 pm by loaded »
Installing the spelling, google translate is used

#### winni

• Hero Member
• Posts: 2660
##### Re: Checking if the point is within the area !
« Reply #1 on: July 17, 2021, 12:06:20 pm »
Hi!

Much faster as the example from the link "Geometry in Pascal" is this:

Code: Pascal  [Select][+][-]
1. function PointInPoly(p : TPointF; const poly :  array of TPointF) : Boolean;
2. var i,k : integer;
3.
4. begin
5.  result := false;
6.  k := High(poly);
7.  For i := 0 to high(poly) do begin
8.   if (
9.      ( ((poly[i].y <= p.y) and (p.y < poly[k].y)) or ((poly[k].y <= p.y)
10.            and (p.y < poly[i].y)) ) and
11.           (p.x < ((poly[k].x - poly[i].x) * (p.y - poly[i].y) /
12.              (poly[k].y - poly[i].y) + poly[i].x) )
13.      ) then result := not result;
14.   k := i
15.  end;
16. end;
17.

As you need it for Points with integer coordinates change

* TPointF to TPoint
* array of TPointF to array of TPoint

As the fpc still does not accept arrays for inlining this function cannot be inlined.

Winni

• Sr. Member
• Posts: 253
##### Re: Checking if the point is within the area !
« Reply #2 on: July 17, 2021, 01:52:36 pm »
winnie, thank you very much. The codes worked fine on Android. When you find an expert like you in graphics; There is another issue I would like to ask in line with the phrase "Since,then, the One Who creates knows, surely the One Who knows will speak.";
I have geometric problems such as the closest point of the point to the line and the lines that cut the area, and I have very unstable solutions for them.
Do you have a clear resource recommendation on the subject?
Installing the spelling, google translate is used

#### marcov

• Hero Member
• Posts: 9521
• FPC developer.
##### Re: Checking if the point is within the area !
« Reply #3 on: July 17, 2021, 03:21:30 pm »
As the fpc still does not accept arrays for inlining this function cannot be inlined.

(such a complex routine wouldn't improve much, performance wise when inlining anyway)

#### Seenkao

• Full Member
• Posts: 215
##### Re: [Solved] Checking if the point is within the area !
« Reply #4 on: August 03, 2021, 12:20:32 am »
Думаю вам нужно посмотреть в сторону игр. Вам нужны математические решения. Многие из них уже давно сделаны. Для вашего решения (как я понимаю) сильно не надо работать с графикой в данный момент времени.

Все решения сделаны очень давно, но ими надо уметь пользоваться. Думаю сложно рекомендовать даже что-то. Посмотрите решения в других программах, других модулях. Желательно в игровых или тесно связанных с играми.

I think you need to look towards games. You need mathematical solutions. Many of them have been made long ago. For your solution (as I understand it), you don't really need to work with graphics at this point in time.

All decisions were made a long time ago, but you need to know how to use them. I think it's hard to recommend even something. Look for solutions in other programs, other modules. Preferably in gaming or closely related to them.

• Sr. Member
• Posts: 253
##### Re: [Solved] Checking if the point is within the area !
« Reply #5 on: August 03, 2021, 07:22:11 am »
Thank you Seenkao for the reply.
Thanks to winni, his solution works number ten.
Installing the spelling, google translate is used

#### avra

• Hero Member
• Posts: 2226
##### Re: [Solved] Checking if the point is within the area !
« Reply #6 on: August 03, 2021, 10:25:53 am »
You need mathematical solutions. Many of them have been made long ago.
One of the oldest ways to determine if a point is inside of an irregular polygon, was to draw a line through that point and count intersection points between line and polygon (starting from the point towards outside of the polygon). If number of intersetion points was odd - then point is inside the polygon. If even - point is outside.
« Last Edit: August 03, 2021, 10:28:40 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

#### winni

• Hero Member
• Posts: 2660
##### Re: [Solved] Checking if the point is within the area !
« Reply #7 on: August 03, 2021, 11:04:49 am »
@avra

That is exactly what my shown code does.

Winni

#### avra

• Hero Member
• Posts: 2226
##### Re: [Solved] Checking if the point is within the area !
« Reply #8 on: August 03, 2021, 03:56:23 pm »
That is exactly what my shown code does.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib