views:

28

answers:

2

Hi I have a need to create a Ternary Plot graph from 3 different variables.

http://en.wikipedia.org/wiki/Ternary_plot

I basically need to come up with the x/y coordinates of the point within the triangle, from the ratio of the 3 variables.

Is there any function / algorithm that anyone is using to create this? The language I am using by the way is ActionScript 3

Thanks

+2  A: 

Wikipedia says that a ternary plot is:

a barycentric plot on three variables which sum to a constant

A barycentric plot takes three variables, which sum to 1, as parameters. So first things first divide your three inputs by their sum.

Now that you have three numbers which sum to one, you can plot a barycentric point. To do this simply multiply the position of one of the points on the triangle by the first numbers, multiply the second point on the triangle by the second number, and the third by the third. Then sum then all together, this is the position you should plot on the graph.

public Vector2 CalculateCoordinate(float a, float b, float c)
{
    float sum = a + b + c;
    a /= sum;
    b /= sum;
    c /= sum;

    return Triangle.Corner1Position * a
         + Triangle.Corner2Position * b
         + Triangle.Corner3Position * c;
}
Martin
+1. right solution, although AS3 doesn't allow operator overloading (and syntax is a bit off :P).
back2dos
Well, to be honest I've never used AS3 in my life. That's C# code. But you get the idea ;)
Martin
This logic worked great thanks
MR-RON
You're welcome :)
Martin
+1  A: 

Martin provided the right solution. In AS3, you'd probably want to use the flash.geom.Point class.

public function calculateCoordinate(a:Number, b:Number, c:Number):Point {
    var sum:Number = a + b + c;
    a /= sum;
    b /= sum;
    c /= sum;

    return scale(Triangle.Corner1Position, a)
         + scale(Triangle.Corner2Position, b)
         + scale(Triangle.Corner3Position, c);
}
public function scale(p:Point, factor:Number):Point {
    return new Point(p.x * factor, p.y * factor);
}
back2dos