Perhaps there is a trade-off between speed and accuracy, or another way to achieve your objective.

]]>I've given up adding and substracting areas as it's now clear this isn't the way to go...

I'm going to try and come up with a function of all of the tip coordinates of 2 triangles that returns the amount of common surface area, without requiring ANY if clauses. This was my goal in the first place.

But first, to avoid and possible needless hassle, I'm going to search for just such a function on the internet... If I fail to find one, I'm going to try and come up with one. Do you know of such a function? No ifs required? Please post if you do. 10x

]]>Mathematically, it's easy. Say you have two lines in point-slope form,

y = y1 + m(x - x1)

y = y2 + n(x - x2)

...that you know intersect. The intersection happens when both equations produce the same coordinates, so you can set the two variables equal to each other and solve the system by substitution:

y2 + nx - nx2 = y1 + mx - mx1

nx - mx = y1 - y2 + nx2 - mx1

x(n - m) = (y1 - y2) + (nx2 - mx1)

x = [ (y1 - y2) + (nx2 - mx1) ] / (n - m)

This is a general formula for the x coordinate of intersection. Sub back to get a formula for the y:

y = y1 - mx1 + {[ m(y1 - y2) + m(nx2 - mx1) ] / (n - m)}

There are many cases, and you'll have to define them all. In all cases, triangles allow you to find the area of the intersection. I suggest you sketch them all in order to work it out.

Good luck...

]]>The only way I can think of is to calculate the intersection points, and then work out the area of the polygon so created. There will be different cases:

* no intersection at all

* just touching (various types: points touching, point touching line, etc)

* various intersections

* one fully inside another

Have a look at this page, there is a java applet where you can draw and intersect shapes: http://www.cs.mcgill.ca/~gstamm/P507/bool_op.html

Another idea: you can calculate the area of a shape by calculating the area of each line segment down to the x-axis. If you always proceed clockwise (or counter-clockwise) and assign plus or minus area based on whether the line has increasing or decreasing x value, you get the net area (or possibly the negative of the net area). This little trick may help somehow.

Perhaps by defining a polygon made up of the two triangles? In other words triangle ABC and DEF become polygon ABCADEFD, then use my area method above ... worth an experiment.

]]>I know I can't do it by just adding the areas of the 2 triangles, but that is useful too... I have also tried defining a shape that contains the 2 triangles as well as some outside space left over, I have tried a lot of things... Can you please help?

I really don't know how i come up with these crazy problems... I'm not even sure it can be solved. Please try for me!!!

Oh, and it must work for any 2 triangles...

]]>You can't do it by just adding or subtracting the areas of your 2 triangles because they could be completely overlapped, completely separate or somewhere in between, and that would affect the result.

]]>