//Returns 1 if the given point is inside the triangle, 0 otherwise
pointInTriangle(var/datum/Point/p)
//Look for being outside bounding rect
//this should speed up this algorithm a lot
if( (p.x>maxx) || (p.x<minx) )
return 0
if( (p.y>maxy) || (p.y<miny) )
return 0
var/A=findArea() //Area of the triangle
//We need to create mini triangles containing our point
var/datum/Triangle
ABP=new(a.x, a.y, b.x, b.y, p.x, p.y)
BCP=new(b.x, b.y, c.x, c.y, p.x, p.y)
CAP=new(c.x, c.y, a.x, a.y, p.x, p.y)
//add the area of all the triangles
//if they're more than the area of this triangle, point is not inside
world<<"ABP: [ABP.findArea()] BCP: [BCP.findArea()] CAP: [CAP.findArea()]"
var/sumA=ABP.findArea() + BCP.findArea() + CAP.findArea()
world<<"Point: ([p.x], [p.y]) SumA: [sumA] A: [A]"
if( sumA > A)
return 0
return 1
Problem description:
I've been scratching my head for hours over this one. For some reason, sometimes sumA and A will equal the exact same thing and yet the if statement resolves to true.
Triangle is (6, 6) (8.12132, 8.12132) (3.87868, 8.12132)
ABP: 0 BCP: 2.37868 CAP: 2.12132
Point: (7, 7) SumA: 4.5 A: 4.5
ABP: 1.06066 BCP: 0.257361 CAP: 3.18198
Point: (7, 8) SumA: 4.5 A: 4.5
The two datasets above are the problematic ones. And the really strange thing is that a dataset such as:
ABP: 2.12132 BCP: 2.37868 CAP: 0
Point: (5, 7) SumA: 4.5 A: 4.5
evaluates correctly. Is this possibly a bug with Byond or am I missing something?
If this isn't clear let me know and I'll explain further or post more code or something.
Results from the same tests you ran.
ABP: 0.928505 BCP: CAP: 2.29129
Point: (7, 7) SumA: 3.21979 A: 4.5
Yeah!
ABP: 1.22023 BCP: CAP: 3.26101
Point: (7, 8) SumA: 4.48124 A: 4.5
Yeah!
ABP: BCP: 2.55347 CAP: 1.00367
Point: (5, 7) SumA: 3.55715 A: 4.5
Yeah!