Author Topic: Vector math - find a location on a triangular face  (Read 703 times)

I have three points that are all the same distance from each other on the XY plane (grid of points).
Each one has a different height. The heights can be negative and can have extreme differences.
I also have an X position and a Y position on this plane. I need to find the height of this point, interpolated from the other three.
How do?

Cluttered diagram:

Hm. I have no idea how to do this, so I'll think about it and get back to you with an answer that makes sense in an hour or so.

Code: [Select]
$one = "10 10 -2";
$two = "20 10 50";
$three = "10 20 -50";

function calculateHeight(%x, %y)
{
    %two = vectorSub($two, $one);
    %three = vectorSub($three, $one);
    %xp = %x / getWord(%two, 0);
    %yp = %y / getWord(%three, 1);
    %xp *= getWord(%two, 2);
    %yp *= getWord(%three, 2);
    %height = %xp + %yp;
    %height += getWord($one, 2);
    return %height;
}

That's my first attempt, not sure if it'll work but I can't test it because I can't access Blockland right now.
« Last Edit: January 13, 2013, 03:26:50 PM by !Trinick »

Wait, is this supposed to represent a 3d triangle, and you're trying to find the height of a single point on the triangle?

3 points all the same distance from eachother -> equilateral triangle?
not quite sure what you're trying to figure out though

3 points making a triangle, and then a 4th you're trying to figure out the height of?

MSPaint to the rescue!



The random point is on the same plane as the three triangle vertices

Never mind, got it myself.
For every square defined by two triangles:
 gridone = 0, 0 (upper left corner)
 gridtwo = 1, 0 (x axis)
 gridthree = 0, 1 (y axis)
 gridfour = 1, 1 (lower right corner)

Code: [Select]
function getGridHeight(%x, %y, %gridone, %gridtwo, %gridthree, %gridfour) {
 %xp = lerp(%gridone, %gridtwo, %x);
 %xb = lerp(%gridthree, %gridfour, %x);
 return lerp(%xp, %xb, %y);
}

function lerp(%a, %b, %x) {
 return %a + %x * (%b - %a);
}