Advertisement

Point in triangle Colour interpolation

Started by May 21, 2012 02:17 PM
1 comment, last by r_bewick 12 years, 9 months ago
Hi, I am trying to find the colour of a point(x,y) inside a triangle. I have used this code taken from
here: http://content.gpwik...le#Other_Method


a:=-(R[1]*Q[2]-P[1]*Q[2]-R[1]*P[2]+P[2]*Q[1]+R[2]*P[1]-Q[1]*R[2]);
b:= (P[1]*R[0]+Q[1]*P[0]+R[1]*Q[0]-Q[1]*R[0]-P[1]*Q[0]-R[1]*P[0]);
c:= (Q[2]*R[0]+P[2]*Q[0]+R[2]*P[0]-P[2]*R[0]-Q[2]*P[0]-Q[0]*R[2]);
d:=-a*P[0]-b*P[2]-c*P[1];

result:= -(a*X+c*Y+d)/b; //z of point


I call this method 4 times to find the colour of a point inside a triangle. Once for red, green, blue and alpha.

I know this method works perfectly on right angled triangles, but I am using scalene triangles.
This should still work on any triangle right?

Most of the time this works, but sometimes the colour that is returned is way off, for example if
the three points of the triangle are close shades of brown(or even the same), the returned point is blue.

What happens when the point I am trying to find is outside the triangle? I am 99% sure that I am always
using a point inside the triangle. If it was slightly outside, would this make the problem I am having? or
could it be something else?

Thanks,
r_bewick
Can you post an example with specific values for P, Q, R, X and Y? You only need to look at one of the color channels (one in which you are sure the result is wrong).
Advertisement

Can you post an example with specific values for P, Q, R, X and Y? You only need to look at one of the color channels (one in which you are sure the result is wrong).


Thanks for the reply alvaro!
This sounds really stupid but there was actually a blue triangle behind the other one. I don't know how I didn't see it.
Thanks for asking me to find the values which I knew to be wrong - I searched for the colour that must be wrong and there the triangle was. I was looking in the completely wrong place before.
I'm glad that this code works and it was just me. biggrin.png

This topic is closed to new replies.

Advertisement