V
V
Voldemar_pauk2021-07-02 14:40:31
Delphi
Voldemar_pauk, 2021-07-02 14:40:31

Is the cycle correct for points in an area?

60defb24ec380359662468.png

x:=StrToFloat(Edit1.Text);

y:=StrToFloat(Edit2.Text);
  if  ((x=0) and (y=-1)) or ((x=1) and (y=-1))
  then
  Label1.Caption:=' точка лежит в заштрихованной области'
  else if ((x=-1) or (y=-2)) or ((x=2) or (y=-2)) or ((x-y)=0) or ((x+y)=4)
  or ((x=2) and (y=-1))
   then
  Label1.Caption:=' точка лежит на границе заштрихованной области '
  else Label1.Caption:=' точка за пределами заштрихованной области '

A cycle was made to check the presence / absence of points in the figure. BUT I don't know if it works for all cases and what can be done to make it better?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hemul GM, 2021-07-02
@Voldemar_pauk

Check first whether the point is in the triangle. Then into a rectangle.
Or you can try using Windows features

function PointInPolygon(Point: TPoint; const Polygon: array of TPoint): Boolean;
var
  rgn: HRGN;
begin
  rgn := CreatePolygonRgn(Polygon[0], Length(Polygon), WINDING);
  Result := PtInRegion(rgn, Point.X, Point.Y);
  DeleteObject(rgn);
end;

Использование
var Points: array of TPoint;
Points := [TPoint.Create(-1, 0), TPoint.Create(0, 0), ...];
if PointInPolygon(TPoint.Create(0, 0), Points) then <точка входит в область>

Or a separate implementation
https://wiki.freepascal.org/Geometry_in_Pascal

K
kalapanga, 2021-07-02
@kalapanga

First, there is no cycle here.
Secondly, the check of finding a point inside the area is implemented incorrectly. Even from general considerations, it is clear that checking whether a point falls into an area cannot be performed by checking whether the coordinates of the point are equal to anything. There should be a check for more or less.
I did not check the long condition for checking if it hit the border, laziness. But something in it is suspiciously few logical AND (and). It is most likely incorrect.

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question