VEX - Getting the point ( vector ) between an intersecting line

   9338   4   1
User Avatar
Member
2040 posts
Joined: Sept. 2015
Offline
Hello,

I was hoping someone might be able provide some tips/insight into what I would like to do for this “method” solution.

I've set up a simple version of what I would like to be able to do in Vex with the attached hip file.

In my Point Wrangler I have two sets of points coming in that make up two seperate lines.

Each point in the wrangler is moved uniquely to a new position and two new lines are drawn.

What I am needing to know how to do is get the point( vector - x,y,z position ) of where the two lines(new) intersect.

I think I am having a bit of difficulty because the only related math I ever studied is basic trigonometry.

In doing a bit of search on this project it seems I need to do a bit of studying in the area of “dot” math, vector normalization, etc.

Some of the links I've looked at seems to suggest I will have to do a combination of using 3 VEX functions:

intersect()
dot()
normal()

Some of the links I've looked at :

http://www.animated-pixels.com/notes-db/2015/9/20/vex-intersect-function [animated-pixels.com]
http://forums.odforce.net/topic/18365-normal-to-angle-how-to/ [forums.odforce.net]
http://forums.odforce.net/topic/18064-how-to-add-implicit-point-normals-in-vex/ [forums.odforce.net]

Perhaps I'm a bit vague on what I would like help on, so I guess for this post I'll try to ask one specific question for a possible answer to start chewing on.

In the first example link the guy is using the vex function intersect:

int hit_prim = intersect(1, @P, -@N, hit_point, hu, hv);

So after readin the docs on this function and what he is saying, am I right in beleiving, that if I get my arguments “right” for this function the point(vector) answer that I am looking for would be represented by the equivalent of what he has for “hit_point” ?

Also, are the two main inputs in his example to get the intersection represented by @P and -@N ?

If so, I think I would have to find a way to use/convert my two sets of vectors to use as arguments similarly.

It seems he is using one vector (@P) which I guess could be represented by one of my set of points, and he is also using what seems to be a normal (-@N) which I am guessing my other set of points could be converted to a normal? and used as that argument input?

Here I'm pasting the code from from my vex wrangler in case anyone doesn't need to open the attached file and can more quicly glean the set up.


int Line_1, Line_2;

int Pt_0_Int, Pt_1_Int, Pt_2_Int, Pt_3_Int;

vector Pt_0_Vec, Pt_1_Vec, Pt_2_Vec, Pt_3_Vec;

// Bring in the points from add node

Pt_0_Vec = point(@OpInput1, "P", 0);
Pt_1_Vec = point(@OpInput1, "P", 1);
Pt_2_Vec = point(@OpInput1, "P", 2);
Pt_3_Vec = point(@OpInput1, "P", 3);

// "translate" each point uniquely
Pt_0_Vec.x += 0.2;
Pt_0_Vec.y += 0.5;
Pt_1_Vec.x += 1;
Pt_1_Vec.y -= 0.5;
Pt_2_Vec.x += 0.6;
Pt_2_Vec.y += 0.7;
Pt_3_Vec.x -= 0.2;
Pt_3_Vec.y -= 0.8;

// Draw the 2 new lines

Line_1 = addprim(0, "polyline" );
Line_2 = addprim(0, "polyline" );

Pt_0_Int = addpoint( 0, Pt_0_Vec);
Pt_1_Int = addpoint( 0, Pt_1_Vec);
Pt_2_Int = addpoint( 0, Pt_2_Vec);
Pt_3_Int = addpoint( 0, Pt_3_Vec);

addvertex( 0, Line_1, Pt_0_Int );
addvertex( 0, Line_1, Pt_1_Int );

addvertex( 0, Line_2, Pt_2_Int );
addvertex( 0, Line_2, Pt_3_Int );

// Next - find the x,y,z vector of the point intersection of the 2 new lines

// ????

Thank you for any help anyone can provide.

Edited by BabaJ - Aug. 6, 2016 15:24:24

Attachments:
Line Intersect.hiplc (68.5 KB)

User Avatar
Member
1799 posts
Joined: Oct. 2010
Offline
are your lines 2D or 3D? for 2D, google is your friend (I generally do not look on how to do it in a software, but, indeed, on the math I did not learn then adapt it to VEX

zonalandeducation.com/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html
http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ [geeksforgeeks.org]

the intersect function in vex takes an origin point, which, if running in prims, that point is generally the centroid (so it would not work in your case)

you can also sorta of do the “lazy” workaround and use a resample to add a very large number of points to your segment, and check for closest point but I would google for the math first and try to implement that



-G
User Avatar
Member
2040 posts
Joined: Sept. 2015
Offline
Hi grayO,

Yeah…that's what I ended up doing…looks like I'll be able to work something out with this:

http://www.mathopenref.com/coordintersection.html [mathopenref.com]

I'm doing points of the same plane…so yeah it's more like 2d, except I have another function which rotates the set of points to my xy plane where I do the calculations, apply, then rotate them back to their original plane.

The vex rotation function is very helpful for that.

For the intersection I just thought there might a more “simple” way, like some function I wasn't aware of.

And thanks for pointing that out about the intersect function running in prims, I suspected there might be some issue like that.

Maybe if I am able to put together some vex code function from that link I'll post it here.

Maybe somewhere along the line someone else might find it useful.
User Avatar
Member
2040 posts
Joined: Sept. 2015
Offline
Well it turns out to be easier to do than I thought…so if anyone might find this usefull here is a function you can use in vex:

vector Determine_Point_Intercept_Of_Two_Lines(vector Line_1_FirstPoint, Line_1_SecondPoint, Line_2_FirstPoint, Line_2_SecondPoint)
{

// This function calculates the point where two lines intersect. It is for points that lie on an xy plane.
// variables m1, m2, b1, b2 represent their conventional representation.
// m signifies the slope of a line and b signifies the y intercept.

float m1, m2, b1, b2;
vector intercept;

m1 = (Line_1_SecondPoint.y - Line_1_FirstPoint.y) / (Line_1_SecondPoint.x - Line_1_FirstPoint.x);
m2 = (Line_2_SecondPoint.y - Line_2_FirstPoint.y) / (Line_2_SecondPoint.x - Line_2_FirstPoint.x);

b1 = Line_1_FirstPoint.y - (m1 * Line_1_FirstPoint.x);
b2 = Line_2_FirstPoint.y - (m2 * Line_2_FirstPoint.x);

intercept.x = (b2 - b1) / (m1 - m2);
intercept.y = b1 + (m1 * intercept.x);
intercept.z = 0;

return intercept;

}
User Avatar
Member
2040 posts
Joined: Sept. 2015
Offline
Ok, so I neglected to include the possiblity that one or even both of the lines might be vertical.

In either case the slope of a line becomes undefined.

So I've added the code to take into account those possibilites.

In the case when both lines are vertical on the xy plane and they share the same x value this function aribratily uses the first inputs x,y,z as the return.

On a further note this function doesn't deal with cases in which the lines are parallel nor does it check for lengths of the respective lines: meaning it assumes they are of infinite length in determing where they would eventually intercept each other.

Here's the revised code:
vector Determine_Point_Intercept_Of_Two_Lines(vector Line_1_FirstPoint, Line_1_SecondPoint, Line_2_FirstPoint, Line_2_SecondPoint)
{

// This function calculates the point where two lines intersect. It is for points that lie on an xy plane.
// variables m1, m2, b1, b2 represent their conventional representation.
// m signifies the slope of a line and b signifies the y intercept.

float m1, m2, b1, b2;
vector intercept;

if( (Line_1_SecondPoint.x - Line_1_FirstPoint.x == 0) && (Line_2_SecondPoint.x - Line_2_FirstPoint.x != 0) )
{
intercept.x = Line_1_FirstPoint.x;

m2 = (Line_2_SecondPoint.y - Line_2_FirstPoint.y) / (Line_2_SecondPoint.x - Line_2_FirstPoint.x);
b2 = Line_2_FirstPoint.y - (m2 * Line_2_FirstPoint.x);

intercept.y = b2 + (m2 * intercept.x);
intercept.z = 0;

return intercept;
}

if( (Line_1_SecondPoint.x - Line_1_FirstPoint.x != 0) && (Line_2_SecondPoint.x - Line_2_FirstPoint.x == 0) )
{
intercept.x = Line_2_FirstPoint.x;

m1 = (Line_1_SecondPoint.y - Line_1_FirstPoint.y) / (Line_1_SecondPoint.x - Line_1_FirstPoint.x);
b1 = Line_1_FirstPoint.y - (m1 * Line_1_FirstPoint.x);

intercept.y = b1 + (m1 * intercept.x);
intercept.z = 0;

return intercept;
}


// This next if statement is arbitrarily setting the return to the first input argument since ( if both lines share the same x value) they would have in common any y value;
// If both lines have infinite length

if( (Line_1_SecondPoint.x - Line_1_FirstPoint.x == 0) && (Line_2_SecondPoint.x - Line_2_FirstPoint.x == 0) )
{
intercept = Line_1_FirstPoint;
return intercept;
}

m1 = (Line_1_SecondPoint.y - Line_1_FirstPoint.y) / (Line_1_SecondPoint.x - Line_1_FirstPoint.x);
m2 = (Line_2_SecondPoint.y - Line_2_FirstPoint.y) / (Line_2_SecondPoint.x - Line_2_FirstPoint.x);

b1 = Line_1_FirstPoint.y - (m1 * Line_1_FirstPoint.x);
b2 = Line_2_FirstPoint.y - (m2 * Line_2_FirstPoint.x);

intercept.x = (b2 - b1) / (m1 - m2);
intercept.y = b1 + (m1 * intercept.x);
intercept.z = 0;

return intercept;

}
  • Quick Links