Inside polygon

About advanced scenes, and the Thyme scripting language used in Algodoo.

Inside polygon

Postby Kilinich » Tue Dec 11, 2012 2:54 pm

Techdemo for insidePolygon function:
Rating: rated 5
Filesize: 5.09 kB
Comments: 2
Ratings: 1
download


Here some interesting code for detection is point inside surface (true) or outside (false):

Code: Select all
scene.my.insideSurface := (p, surf) => {
  l := string.length(surf);
  c := 0;
  crossRay := (p1,p2) => {(((p1(1) > 0) && (p2(1) < 0)) || ((p2(1) > 0) && (p1(1) < 0))) && ((p2(0)*p1(1) - p1(0)*p2(1))/(p1(1)-p2(1)) > 0)};
  xFor := (n1, n2, code)=>{n2 > n1 ? {m := (n1 + n2) / 2; xFor(n1, m, code); xFor(m + 1, n2, code)} : {code(n1)}};
  xFor (1, l, (i)=>{c = c + math.toInt(crossRay(surf(i-1)-p,surf(i%l)-p))});
  c % 2 == 1
};


If you want to analyse polygon with holes (multi-surface), use this additional function:

Code: Select all
scene.my.insideSurfaces := (p, msurf) => {
  c := 0;
  For (string.length(msurf), (i)=>{c = c + math.toInt(scene.my.insideSurface(p,msurf(i)))});
  c % 2 == 1
};


To work with real polygon you also need to transform coordinates (according to polygon position, angle):

Code: Select all
Scene.my.trans := (geom, c)=>{
    nc := c - geom.pos;
    cos_a := math.cos(geom.angle);
    sin_a := math.sin(geom.angle);
    [nc(0) * cos_a + nc(1) * sin_a, nc(1) * cos_a - nc(0) * sin_a]
};


Finally, code examples for polygon color and mouse position:

Code: Select all
// Multisurface
color = {scene.my.insideSurfaces(scene.my.trans(owner, app.mousepos), (readable(owner)).surfaces) ? [1,1,1,1] : [0,0,0,1]};

//Single surface
color = {scene.my.insideSurface(scene.my.trans(owner, app.mousepos), (readable(owner)).surfaces(0)) ? [1,1,1,1] : [0,0,0,1]};
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Inside polygon

Postby guyboy » Wed Dec 12, 2012 12:19 am

Nice algorithm. :clap:

Sorry if this has been asked before, but how do you add functions to your scene?
ImageImage
Human horse pasture expansion OpenGL Book Hall
Kwon owned Version (C) 2002-2008
User avatar
guyboy
 
Posts: 80
Joined: Mon Aug 31, 2009 6:19 pm
Location: Ḻ̘̬͕͍͚̥͈͚͖̝̜̲͕̭̩ͅ

Re: Inside polygon

Postby electronicboy » Wed Dec 12, 2012 2:14 am

Paste the thyme code into the console!

All functions are, is "special variables"!
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Inside polygon

Postby Xray » Tue Dec 18, 2012 7:14 am

That code is rather esoteric, and I doubt that more than a couple of people besides Kilinich would understand it. I have some programming experience as a professional engineer, and I tried to follow it and understand it, but now I have a major headache! :crazy:
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Inside polygon

Postby Xray » Sat Mar 01, 2014 7:31 pm

When I click on the script for the color property, the scene no longer works! The only way that I can make the scene work again is if I enter in the script box: color = {
scene.my.insideSurfaces(scene.my.trans(owner, app.mousepos), (readable(owner)).surfaces) ? [1, 1, 1, 1] : [0, 0, 0, 1]
}

Why does clicking on the script make it stop working? :crazy:
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Inside polygon

Postby Kilinich » Sat Mar 01, 2014 11:27 pm

Xray wrote:When I click on the script for the color property, the scene no longer works! The only way that I can make the scene work again is if I enter in the script box: color = {
scene.my.insideSurfaces(scene.my.trans(owner, app.mousepos), (readable(owner)).surfaces) ? [1, 1, 1, 1] : [0, 0, 0, 1]
}
Why does clicking on the script make it stop working? :crazy:

It's a bug. Thyme still experimental and not supported remember? ;-)

Try replace owner with entity. That should do the trick!

Code: Select all
{
    scene.my.insideSurfaces(scene.my.trans(entity, app.mousepos), (readable(entity)).surfaces) ? [1, 1, 1, 1] : [0, 0, 0, 1]
}
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Inside polygon

Postby Xray » Sun Mar 02, 2014 12:41 am

Yup, that fixed it! Thanks again! :clap:
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Inside polygon

Postby lololoer16_17 » Sun Mar 02, 2014 4:54 pm

As happens to Xray I don't understand that code, kilinich could you please explain the sense of each script component of all functions? :problem:
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)

Re: Inside polygon

Postby Kilinich » Sun Mar 02, 2014 8:03 pm

lololoer16_17 wrote:As happens to Xray I don't understand that code, kilinich could you please explain the sense of each script component of all functions? :problem:

What excatcly you want to know?
Start from something like this: http://mathonweb.com/entrtain/inout/t_inout.htm
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Inside polygon

Postby lololoer16_17 » Mon Mar 03, 2014 6:28 pm

Is everything :mrgreen: .
I suggest you to explain in each script line (ended by ";") using //
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 3 guests