Koos
Koos

Reputation: 33

Calculating a position within a triangle

I'm trying to visualise some data following the design in this picture:

enter image description here

Each subject holds a score (1-100). I want the middle dot to indicate which subject the student (middle dot) excels at.

For example: If the student scores 50% for both subject 1 and subject 2, and 100% for subject 3, I want the dot to be slightly towards the subject 3 corner and perfectly centered between subject 1 and 2, as in this picture:

enter image description here

Any advice on how to do this, or even a point in the right direction, would be greatly appreciated!

Upvotes: 3

Views: 224

Answers (1)

samgak
samgak

Reputation: 24417

Sum the scores for all subjects, and then divide each subject score by the total to get a co-efficient for each. Then multiply all the subject points by their respective co-efficients and sum together to get your central point.

e.g (not code):

subject1: 50%
subject2: 50%
subject3: 100%

total: 200

subject1 co-eff: 50 / 200 = 0.25
subject2 co-eff: 50 / 200 = 0.25
subject3 co-eff: 100 / 200 = 0.5

centralpoint.x = (point1.x * 0.25) + (point2.x * 0.25) + (point3.x * 0.5)
centralpoint.y = (point1.y * 0.25) + (point2.y * 0.25) + (point3.y * 0.5)

What you are doing is computing a weight for each subject where the weights sum to 1 and then finding a weighted average of the three points. This construction is called a Convex combination (thanks to @MattTimmermans for providing the link in the comments).

One special case is where the scores sum to zero, in which case there is no valid point (since the calculations would involve a divide by zero). In this case you could put the point in the exact center, or just display no point, up to you.

Upvotes: 6

Related Questions