Find if a point is within a triangle: Difference between revisions
Find if a point is within a triangle (view source)
Revision as of 22:59, 29 August 2023
, 9 months ago→{{header|Evaldraw}}: fix bug where point in tri wouldnt work if point in negative quadrant
(add RPL) |
(→{{header|Evaldraw}}: fix bug where point in tri wouldnt work if point in negative quadrant) |
||
Line 1,171:
<syntaxhighlight lang="c">struct vec2{x,y;};
struct line_t{a,b,c;};
struct triangle_calc_t{
vec2 origin;
vec2 vertices[3];
double area2;
};▼
static vec2 dat[3] = {-3,7, -6,-5, 2,2};
static triangle_calc_t tri;
enum{TRI_OUT=0, TRI_EDGE=1, TRI_INSIDE=2}
(x,y,t,&r,&g,&b)
{
if (numframes==0)
{▼
precalc_tri( tri, dat);
▲ vec2 dat[3] = {0,-2, -2,2, 4,0};
for(i=0; i<3; i++) {▼
vertices[i].x = dat[i].x + 2;▼
vertices[i].y = dat[i].y + 2;▼
}▼
makeLine(line0, vertices[0], vertices[1]);▼
makeLine(line1, vertices[1], vertices[2]);▼
makeLine(line2, vertices[2], vertices[0]);▼
area2 = areaTriangleX2(vertices[0], vertices[1], vertices[2]);▼
winding_dir = sgn(area2);▼
}
d0 = d1 = d2 = 0;
r=255; g=255; b=0;
return 1;
else if (side == TRI_INSIDE) {
if (tri.winding_dir == -1) {
swap(d0,d1);
}
factor = 255;
g = factor*( d2 / div);
b = factor*( d0 / div);
return 1;
}
Line 1,211 ⟶ 1,209:
}
precalc_tri(triangle_calc_t t, vec2 verts[3]) {
t.origin = verts[0];
d0 = lineDist( line0, p.x, p.y);▼
▲ if ( winding_dir*sgn(d0) <= 0 ) return 0;
}
}
areaTriangleX2(vec2 a, vec2 b, vec2 c) { // Same as the determinant, but dont div by 2▼
}
isPointInsideTriangle(x,y, triangle_calc_t t, &d0,&d1,&d2) {
vec2 p = {x + t.origin.x, y + t.origin.y };
if (d0==0) { return TRI_EDGE; }else if ( sgn(d0) < 0 ) return TRI_OUT;
d1 = t.winding_dir * lineDist(
if (d1==0) { return TRI_EDGE; } else if (
d2 = t.winding_dir * lineDist(
if (d2==0) { return TRI_EDGE; } else if (
return
}
Line 1,233 ⟶ 1,245:
lineDist(line_t line, x,y) {
x*line.a + y*line.b + line.c;
▲}
▲areaTriangleX2(vec2 a, vec2 b, vec2 c) { // Same as the determinant, but dont div by 2
▲ ( a.x*(b.y - c.y) + b.x*(c.y - b.y) + c.x*(a.y - b.y) );
}
swap(&a,&b) {tmp = a; a=b; b=tmp; }</syntaxhighlight>
|