Inside polygon
10 posts • Page 1 of 1
Inside polygon
Techdemo for insidePolygon function:
Here some interesting code for detection is point inside surface (true) or outside (false):
If you want to analyse polygon with holes (multi-surface), use this additional function:
To work with real polygon you also need to transform coordinates (according to polygon position, angle):
Finally, code examples for polygon color and mouse position:
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...
-

Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Inside polygon
Nice algorithm. 
Sorry if this has been asked before, but how do you add functions to your scene?
Sorry if this has been asked before, but how do you add functions to your scene?
Human horse pasture expansion OpenGL Book Hall
Kwon owned Version (C) 2002-2008
-

guyboy - Posts: 80
- Joined: Mon Aug 31, 2009 6:19 pm
- Location: Ḻ̘̬͕͍͚̥͈͚͖̝̜̲͕̭̩ͅ
Re: Inside polygon
Paste the thyme code into the console!
All functions are, is "special variables"!
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
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! 
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Inside polygon
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?
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?
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Inside polygon
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?
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...
-

Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Inside polygon
Yup, that fixed it! Thanks again! 
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Inside polygon
As happens to Xray I don't understand that code, kilinich could you please explain the sense of each script component of all functions? 
- lololoer16_17
- Posts: 18
- Joined: Tue Jan 21, 2014 8:55 pm
- Location: Spain (South)
Re: Inside polygon
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?
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...
-

Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Inside polygon
Is everything
.
I suggest you to explain in each script line (ended by ";") using //
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)
10 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 3 guests




