3

What is, and is there, a fast way to check where in the plane my line will intersect, if i know the plane is always in the same z-axis (so it cannot be rotated), and its width/height is infinite? Also, my "line" isn't actually a line, but a 3d vector, so the "line" can go to infinite distance.

Here is the code that relies on two points: (p1 and p2 are start and end points of the line. plane_z = where the plane is)

k1 = -p2.z/(p1.z-p2.z-plane_z);
k2 = 1.0f-k1;
ix = k1*p1.x + k2*p2.x;
iy = k1*p1.y + k2*p2.y;
iz = plane_z; // where my plane lays

Another solution which works with a vector (i made it use two points as the first example did too, "p2.x-p1.x" etc. is the vector calculation):

a = (plane_z-p1.z)/(p2.z-p1.z);
ix = p1.x + a*(p2.x-p1.x);
iy = p1.y + a*(p2.y-p1.y);
iz = plane_z;

Edit3: added Orbling's solution which is slightly faster, and doesnt rely on two points necessarily.

Rookie
  • 3,753
  • 5
  • 33
  • 33
  • 3
    That's just the [intersection between a line and a plane](http://en.wikipedia.org/wiki/Line-plane_intersection). – Orbling May 26 '11 at 13:25
  • 1
    @Orbling, that is not the same, since i dont have a line, i only have a direction where the line is going, so im hoping this would make the calculations simpler. Plus, i dont understand those formulaes on the wikipedia... and why would i come here if i was master at maths already...? – Rookie May 26 '11 at 13:38
  • @Rookie: No need for the tone. If you only have a direction and no point. Then what you have is infinitely many lines/planes, all of which have infinite points of intersection with your primary plane. There is no *point* of intersection, unless the line is actually bound to a specific location. – Orbling May 26 '11 at 13:42
  • @Orbling, i only have one plane and one line. i was thinking if this could be calculated somehow without specifying the start and end position of my line, since those are irrelevant: i should always be able to get intersect point (except the 2 cases). And the problem is: i should be able to calculate this without the end point of the line, because how do i really get the endpoint...? i would have to calculate that somehow, and that is useless calculations since it doesnt matter for me how far i am from the plane.. well, i am not a math guy so maybe this is not even possible. thats why i asked. – Rookie May 26 '11 at 14:05
  • @Rookie, you don't need the start and end position of your line. Just any point that's on it. A point it travels through. – Orbling May 26 '11 at 14:14
  • @Orbling, i dont understand... to get intersection point in the plane, my code requires 2 points, start and end, and the plane should be inbetween those two points or else i wont get intersection point. – Rookie May 26 '11 at 14:17
  • @Rookie: If your line is infinite, just a direction vector like you say, that's fine, unless it is parallel to the Z-plane (your plane) then it'll definitely intersect somewhere. But in order to have an actual line, the line must be somewhere, a direction can be anywhere. Imagine holding a pencil up at an angle to your desk. If it is just a direction you have, the pencil could be moved anywhere around your desk retaining that direction, and would intersect with the desk anywhere as a result. If it's in a specific position relative to the desk, a point must be able to be found somewhere. – Orbling May 26 '11 at 14:24
  • @Rookie: Do you have a direction vector instead of `p2` then? – Orbling May 26 '11 at 14:34
  • @Rookie: LOL, *good*... what is it? So we can do the code. ;-) – Orbling May 26 '11 at 14:50
  • @Orbling, what do you mean, i dont use it yet since i cant. but if i knew how, i would convert my p1-p2 into a direction vector easily. – Rookie May 26 '11 at 14:53
  • @Rookie: I meant, what would the variable be called. Shall I assume you have a direction vector called `d` then with `d.x, d.y, d.z` parts, or make it up from `p1` and `p2`? [Sorry about the delay in responses, at work, have to answer you when an opportunity arises.] – Orbling May 26 '11 at 15:18
  • @Orbling, variable names doesnt matter ;) d sounds fine. – Rookie May 26 '11 at 15:26
  • @Rookie: I sorted out an algebraic form of the answer visually at work, but then had to go home, so don't have it available. Basically, once you have the direction of the line `d`, all points on the line are given by `p1 + ad`, where `a` is some scalar, `p1` is the point you know. The point plus any amount of scaling of the direction. All you need to do is find the value for a, where it hits the plane. You can calculate that with the formula `a = ((zp - p1).n) / (d.n)`; where `zp` is any point in the plane (use `[0, 0, z]` for simplicity) and `n` is the normal to the z-plane (`[0, 0, 1]`). – Orbling May 26 '11 at 23:53
  • @Rookie: Those calculations above are vector level, so the subtraction is `[zp.x - p1.x, zp.y - p1.y, zp.z - p1.z]` and the dot-product is each component times it's corresponding component. It all simplifies down a lot as your plane is a z-plane. `a = (z - p1.z) / d.z`, where `z` is the z-plane distance, I think. You can plug that value back in to get the final values `[p1.x + a d.x, p1.y + a d.y, p1.z + a d.z]` see if that works. – Orbling May 26 '11 at 23:58
  • @Orbling, does it look anything like this in code? `a = (plane_z-p.z)/d.z; ix = p.x+a*d.x; iy = p.y+a*d.y; iz = plane_z;` im not sure if i read your formulas right. if its not right and needs more calculations, i dont think its any simpler than the formula i already use. (have you looked at my edits?) – Rookie May 27 '11 at 17:15
  • one question: does the vector have to be normalized? – Rookie May 27 '11 at 17:19
  • @Rookie: The direction vector? Usually it is not a bad idea to normalize such things. But because the direction vector is being used in the creation of the scalar, and then again in the calculations of the intersection, it should not matter. Try it and see, I might well have messed it up as I did that very late at night off my head and I've not done those calculations for about 10 years, lol. – Orbling May 27 '11 at 19:15
  • @Orbling, it worked, but i have few problems: how do i check if the values are "overflowing" when it gets out of the scale, for example i started to get wild numbers like after 464256 coordinate i started to get -45373 suddenly etc. i know only how to check if the line goes paraller to the plane. btw mind you put your solution as an answer so i can accept it ? although, its not much faster than the current one, still, its better, i think. – Rookie May 27 '11 at 19:22
  • @Rookie: If the directional vector comes close to parallel, then the coordinates of intersection will be way off. Because you have a simple z-plane to compare against, you can just check the `d.z` value for being `0` or close to it I imagine and consider those non-intersecting. Don't worry about the answer, the comments suffice. – Orbling May 27 '11 at 20:43
  • @Rookie: It won't be simpler than the formula you used already, that was reasonably optimal. – Orbling May 27 '11 at 20:45
  • Folks - you know there is the chat room feature that you can take these extended discussions over to :) – Kev May 27 '11 at 21:21
  • @Orbling, okay then, ill just accept the only answer here, although it didnt help _me_ – Rookie May 27 '11 at 23:39
  • @Kev: All an extended answer. Just required some discussion to clarify. – Orbling May 28 '11 at 00:01
  • @rookie - if an answer didn't solve the problem described in your question then don't mark as accepted. Upvote if it helped somewhat but if you're still scratching you head then it's not an answer. Others who may find themselves with the same problem then won't waste time trying to make the vrinces answer work for them then discover it's not a solution. Thanks. – Kev May 28 '11 at 00:09
  • @Kev, it might help someone else with higher math knowledge... i dont know, it seems like a good link he gave, but its all gibberish to me.. and since nobody else answered, that was the closest answer i could accept... and btw i cant upvote or downvote since im not logged in (i dont know how to register lol) – Rookie May 28 '11 at 12:51

1 Answers1

3

You can implement a strait-forward solution like there http://paulbourke.net/geometry/planeline/, then apply your simplifications. In the algebraic solution (#2) A and B are zeros in your case (if i understand correctly this statement)

plane is always in the same z-axis (so it cannot be rotated)

Note: your line should be a point and a direction, or two points right?

Thomas Vincent
  • 2,214
  • 4
  • 17
  • 25
  • 1
    He says he doesn't have a point, only a direction - but that is not a line. So perhaps he has like a viewpoint, origin or object centre. :-/ – Orbling May 26 '11 at 13:50
  • 1
    @Rookie, can you give us more about what your are trying to achieve here, context etc ... – Thomas Vincent May 26 '11 at 13:55
  • maybe this is not even possible, seems like i must have two points, but as i mentioned in my question comments, the annoyance is that i must calculate the end position of my line to be able to get the intersection point. i was hoping this could be optimized somehow, i have working code for the intersection point, but it still requires two points. – Rookie May 26 '11 at 14:13
  • @Rookie: It sounds entirely possible, just you need a point not a start, not an end, just any given point the line runs through, otherwise the line could move about anywhere couldn't it? If it was just a direction. How is this direction described, does it come from somewhere? – Orbling May 26 '11 at 14:20
  • @Orbling, oh yeah, you are right, it needs a start position of course. but not end position, as my code requires now. mind seeing my code to see if the end pos can be eliminated? (editing my question post now). – Rookie May 26 '11 at 14:26