Author Topic: Point in polygon  (Read 1074 times)

0 Members and 1 Guest are viewing this topic.

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 772
  • User-Rate: +31/-1
    • Charles Pegge
Re: Point in polygon
« Reply #15 on: May 10, 2022, 11:01:31 PM »
Yes, there is progress, though this 2d vector stuff is surprisingly tricky. I am mor accustomed to 3d graphics :)

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 772
  • User-Rate: +31/-1
    • Charles Pegge
Re: Point in polygon
« Reply #16 on: May 11, 2022, 01:02:26 PM »
This is the code for InsideOoutsidePoints, your original quest. It should still work with the original Geoplanar.inc.

Code: [Select]
  % Title "Intersections Demo"
 '% Animated
 '% ScaleUp
 '% PlaceCentral
 '% AnchorCentral
  uses consoleG
  uses GLO2\GeoPlanar

  sub boundaries()
  ================
  indexbase 0

  string tab="    "
  point  p
  int    a,i,j,k

  seed=0x123567

  line  d[8]
  float f[16]
  f={-1,-1, 1,-1,  1,1, -1,1}
  for i=0 to 7
    f[i]+=rnd()*0.4 'turbulate
  next
  '
  d[0]={ f[0],f[1],f[2],f[3]}
  d[1]={ f[2],f[3],f[4],f[5]}
  d[2]={ f[4],f[5],f[6],f[7]}
  d[3]={ f[6],f[7],f[0],f[1]}
  '
  for i=0 to 7
    f[i]=f[i]*0.5 'smaller inner form
  next
  '
  d[4]={ f[0],f[1],f[2],f[3]}
  d[5]={ f[2],f[3],f[4],f[5]}
  d[6]={ f[4],f[5],f[6],f[7]}
  d[7]={ f[6],f[7],f[0],f[1]}



  'shading
  flat 'default
  '
  color 0,1,1
  'printl "Inside " tab p.x tab p.y

  scale 10,10

  move 1.5,-1
  '
  color 0,1,1
  thickness 3
  '
  for i=0 to 7
    drawline @d[i]
  next
  '
  int inside
  point pio
  line dd
  '
  float x,y
  for j=1 to 1000
    x=2*rnd
    y=2*rnd
    pio={x,y}
    gosub checkInside
  next
  exit sub
  '
  CheckInside:
  ============
  color 1,1,0
  pointsize 4
  dd={-1000,pio.y,1000,pio.y}
  inside=0
  for i=0 to 7
   a=intersected d[i],dd,p
   if a
     'drawline @dd
     'drawpoint @p
     if p.x<pio.x
       inside=1-inside 'toggle
     endif
   endif
  next
  '
  if inside
    color 1,1,0
  else
    color 0.8,0,0
  endif
  pointsize 4
  drawpoint @pio
  ret 
 



  end sub


  sub main()
  ==========
  string s
  cls 0,0,0
  pushstate
  scale 2,2
  'typeface=1
  printl "Inside / Outside Points"
  popstate
  '
  move 0,-2
  '
  PushState
  move 1,-1
  boundaries()
  PopState
  '
 
  end sub

  EndScript

Offline Nicola_Piano

  • Jr. Member
  • **
  • Posts: 88
  • User-Rate: +0/-0
Re: Point in polygon
« Reply #17 on: May 12, 2022, 04:55:51 PM »
Hi Charles,
did not come out the same result as yours ... :-(
« Last Edit: May 12, 2022, 05:01:00 PM by Nicola_Piano »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 772
  • User-Rate: +31/-1
    • Charles Pegge
Re: Point in polygon
« Reply #18 on: May 12, 2022, 05:23:37 PM »
.Ok, Sorry about that. Here is my current Geoplanar.inc which belongs in /inc/GLO2

I think it will be quite open-ended for some time.

You can resize ConsoleG apps and take a jpeg-snapshot with Ctrl-P.
« Last Edit: May 12, 2022, 05:40:46 PM by Charles Pegge »

Offline Nicola_Piano

  • Jr. Member
  • **
  • Posts: 88
  • User-Rate: +0/-0
Re: Point in polygon
« Reply #19 on: May 13, 2022, 03:52:21 PM »
Hi Charles,
OK now.
I am attaching what I had done which also seems to work quite well, even with the old geoplanar.inc

Code: [Select]
' % Title "Console Demo"
 '% Animated
 '% ScaleUp
 '% PlaceCentral
 '% AnchorCentral
  uses consoleG
  uses console
  uses GLO2\GeoPlanar

  sub drawintersections()
  =======================
  string tab="    "
  line   d1,d2
  point  p
  sys    a
  int n,j,i,k,np,xt
  n=10 'number of poly
  np=9 'number of point to evaluate

 'points of poly
float px={41.84076349611504,41.837170604812826,41.83233185912741,41.82782814339409,41.82006567695157,41.81968232089131,41.81450678946148,41.809282912371984,41.81431509508004,41.820880300959615}
float py={12.466150927636479,12.480682808848268,12.48042560741089,12.478946699145974,12.481390112801055,12.468530040932215,12.461135499607634,12.45496266511059,12.44788962558273,12.438437472759132}

 'points to prove
float fx={41.834844,41.8300796,41.829849,41.832556,41.8284089,41.828539,41.8320845,41.8393343,41.820684}
float fy={12.471219,12.4668936,12.467641,12.463344,12.4608684,12.471123,12.4649883,12.4637847,12.479901}

for j=1 to np
xt=0 'conta il numero di intersenzioni
for i=1 to n
k=i+1
if i=n then k=1
d1={px[i],py[i],px[k],py[k]}
'd2={41.834844,12.471219,41.834844,0}
d2={fx[j],fy[j],fx[j],0}
a=intersected d1,d2,p
if a then
printl "CROSS" tab j "," i ")" tab a tab p.x tab p.y
xt++
end if
p.x=0
p.y=0
next i
if frac(xt/2)=0 then printl fx[j] "," fy[j] tab "> OUT"
if frac(xt/2)<>0 then printl fx[j] "," fy[j] tab "> IN"
next j
printl

end sub


sub main
=============================
  string s
  printl "Intersection Points"
  DrawIntersections
  pause
  exit
end sub

  EndScript


Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 999
  • User-Rate: +30/-4
    • it-berater
Re: Point in polygon
« Reply #20 on: May 15, 2022, 07:26:13 PM »
This stuff reminds me about neural networks where you can train the network to make classification of elements :-).
The advantage is that you do not need to find the formula, because the network will find the formula.
Neural network optimizations would be an interesting topic for oxygen possibly.
Because the basic underlying formulas are simple and can easily be optimzed in ASM.


Neural Networks for Classification
« Last Edit: May 15, 2022, 07:28:13 PM by Theo Gottwald »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 772
  • User-Rate: +31/-1
    • Charles Pegge
Re: Point in polygon
« Reply #21 on: May 16, 2022, 03:53:09 AM »
Hi Theo,

Delaunay triangles connect points so that no triangle may intersect another triangle. It can be solved by creating all possible lines between the points, then sorting into ascending order of length and testing each for intersections, giving priority to the shorter lines.

This uses random points, but it could be more interesting with other arrangements .

Offline Nicola_Piano

  • Jr. Member
  • **
  • Posts: 88
  • User-Rate: +0/-0
Re: Point in polygon
« Reply #22 on: May 16, 2022, 12:57:46 PM »
Charles,
this topic is interesting. I have to see to deepen it ...
Cheers

Offline Johan Klassen

  • Newbie
  • *
  • Posts: 9
  • User-Rate: +0/-0
Re: Point in polygon
« Reply #23 on: May 16, 2022, 05:03:16 PM »
Good day everyone :)
this is a very deep and interesting subject, you find a lot of info in the web like https://www.cs.cmu.edu/~quake/triangle.html