Recent

Author Topic: Finding the center of an irregular shape  (Read 8248 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Finding the center of an irregular shape
« Reply #15 on: June 01, 2021, 04:10:50 pm »
Nice demo. I now understand that the "pole of inaccessibility" is the desired point.
« Last Edit: June 01, 2021, 04:12:50 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Finding the center of an irregular shape
« Reply #16 on: June 01, 2021, 04:59:30 pm »
Nice demo. I now understand that the "pole of inaccessibility" is the desired point.

The article posted by Edson in reply #8 is really nice.

Need to work on that Sqrt(MinDisSq) again. Without the ABS around MinDisSq there is a - value that pops in there if you add some points and move it off the screen or around at different places.

I don't think you need to change it. GetSegDistSq returns positive numbers always:
Code: Pascal  [Select][+][-]
  1.     Result := round(dx * dx + dy * dy);

Here is a comment from the Javascript file:
Quote
// get squared distance from a point to a segment

I believe the error you're seeing is probably a range error from trying to round and fit a double value bigger than MaxInt into an integer variable in the same line:
Code: Pascal  [Select][+][-]
  1.     Result := round(dx * dx + dy * dy);

DomingoGP

  • Jr. Member
  • **
  • Posts: 62
Re: Finding the center of an irregular shape
« Reply #17 on: June 05, 2021, 05:23:06 pm »

Based on the implementation of jamie and engkin I have reimplemented the routine using floating point variables and a Priorityqueue instead of a list.
I have also implemented the routine to work with BGRABitmap using TPointF.

I have allowed myself to include the license (ISC) just like the original project.

jamie, I have put your data as co-author of the routine (I think they are correct, if not, correct me).
engkin, I can give you credit in the file but I need your real data.

P.S. I have tested it in a real map project and it works fine.

The project now depends on BGRABitmap, but the dependency can be removed by commenting out the definition
  {$ define IMPLEMENT_TPOINTF}

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Finding the center of an irregular shape
« Reply #18 on: June 05, 2021, 08:29:06 pm »
engkin, I can give you credit in the file but I need your real data.

Thank you for your efforts. I did not do anything worth to be credited.

DomingoGP

  • Jr. Member
  • **
  • Posts: 62
Re: Finding the center of an irregular shape
« Reply #19 on: June 13, 2021, 04:35:28 pm »

I have used this function in https://github.com/DomingoGP/geopackageViewer and works fine.

but i had to change the test for CellSize because if the polygons are too small i get OutOfMemory error.
const
  MIN_POLYGON_SIZE=10;


  //if CellSize = 0 then      // error out of memory
  //begin
  //  Result.PlotX := MinX;
  //  Result.PlotY := MinY;
  //  Result.Distance := 0;
  //  Exit;
  //end;
  if CellSize < MIN_POLYGON_SIZE then  // poligon to small, return center.
  begin
    Result.PlotX := (MinX+MaxX)/2;
    Result.PlotY := (MinY+MaxY)/2;
    Result.Distance := 0;
    Exit;
  end;

 

TinyPortal © 2005-2018