easy vpython way to compute the intersection of a line with a plane (triangle) ?

90 views
Skip to first unread message

Horst JENS

unread,
Jun 25, 2021, 4:04:28 PM6/25/21
to VPython-users

Hello, i struggle a bit with an simple geometric problem and i guess it is already solved many times by vpython users but i would love to see an example.

my problem is:
i have an triangle defined by 3 vertex poins ABC
i have an vertical line between points p0 and p1
i know that the line intersects the plane of the triangle because the x and z coordinates
of p0 are inside the x and z coordinates of the triangle

i search an easy/obvious way to compute the missing y coordinate of the intersection point of the line and the triangle.
something of a function like that:

def compute_y(triangle, x, y):
    # ....
    return y

i played around with interpolating the y values of AB, AC and BC and i tried to solve the line / plane equations using my very rusty high-shool math but i wonder if there does exist a ready-to use function  for that case or some vpyhton examples that i can look at and study.

i created some code to visualize the problem:




Screenshot at 2021-06-25 21-59-39.png

Kevin Karplus

unread,
Jun 25, 2021, 6:50:19 PM6/25/21
to vpytho...@googlegroups.com
I don't know if there is an easy solution already available.  Here is how I'd go about it if there isn't:
  • Subtract P0 from everything, getting A',B',C', 0, and P1'
  • The line is now  (x,y,z)= k P1'  for some k.
  • The plane is the solution to  ((x,y,z)-A')•((B'-A')×(C'-A'))=0
  • The intersection (if it exists) is k P1', where k is the solution to (k P1' -A')•((B'-A')×(C'-A'))=0
  • That is a simple linear equation that can be easily solved for k numerically (as long as there is no division by zero).  (Setting up this solution is probably the most likely place to make programming errors.)
  • Add P0 back to get the intersection in the original coordinates.
Undergraduate Director for Biomolecular Engineering and Bioinformatics
Professor of Biomolecular Engineering, University of California, Santa Cruz
Affiliations for identification only.



--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/c4d1c25c-f2bd-49b1-9efd-9bf47e432bb2n%40googlegroups.com.

John

unread,
Jun 29, 2021, 2:04:27 AM6/29/21
to VPython-users
IMG_20210628_225703.jpg

Horst JENS

unread,
Jun 29, 2021, 3:43:37 AM6/29/21
to VPython-users
thank you! What is the title of this book?
-Horst 

Horst JENS

unread,
Jun 29, 2021, 4:02:55 AM6/29/21
to VPython-users
I made an updated program using the solution of Kevin Karplus

John

unread,
Jun 29, 2021, 7:18:45 AM6/29/21
to VPython-users
The tile of the book is "Mathematics for 3D Game Programming and Computer Graphics".

Kevin Karplus

unread,
Jun 29, 2021, 12:43:06 PM6/29/21
to vpytho...@googlegroups.com
Horst, 

I'm glad my description helped you solve the problem.

I've not looked at your code, but I assume you put in checks for the degenerate cases (P0=P1, A=B, A=C, B=C, ABC colinear, P0-P1 coplanar with ABC).  Of course, most of the degenerate cases will fail with a divide-by-zero error at some point, so you don't really have to check for them in Python, but adding some of the simpler checks and raising exceptions can give you better error messages for debugging.

The solution from the book is a little more elegant using 4D vectors, but I think is essentially the same.

Undergraduate Director for Biomolecular Engineering and Bioinformatics
Professor of Biomolecular Engineering, University of California, Santa Cruz
Affiliations for identification only.


Horst JENS

unread,
Jun 29, 2021, 2:18:57 PM6/29/21
to vpytho...@googlegroups.com
yes, i am aware that my code currently does not check for special cases, i will work on it.

One question is still in my mind:

if i just "look" at the 3d-ouput of vpython of a line going through a plane, i can very good "see" where the plane intersects the line, because the web-gl output of vpython hides those Objects that are behind other objects correctly. So it seems to me that the intersection point of plane with line is already calculated by web-gl / the grapic card.
Is there a method to simply access the coordinates of the intersection point from there ? It strikes me as odd that i have to re-calculate the intersection point in vpython when the renderer already can "paint" the intersection point correctly.

You received this message because you are subscribed to a topic in the Google Groups "VPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vpython-users/XIE5koUptdM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/CAJF5WLW2sQpN3UPKyd5zv_6VXxudvrTdOYxV9NfZtQx5fkYHJg%40mail.gmail.com.


--
First name:   Horst     tel:+43-660-52.65.377
Last  name:   JENS      
Email:   horstje...@gmail.com
Web:     http://spielend-programmieren.at

Bruce Sherwood

unread,
Jun 29, 2021, 5:08:36 PM6/29/21
to VPython-users
The way that graphics cards make hidden objects be hidden is in hardware, not software. Multiple processors on the card may simultaneously, in parallel, attempt to store different colors into a particular pixel corresponding to several overlapping objects. The hardware keeps in memory for every pixel the smallest distance from the camera ("z depth") encountered so far in the rendering of the scene and ignores an attempt by one of the processors to store a color into a pixel with a larger z depth. So in an important sense the graphics card's software does NOT know how to answer your question.

There are some echoes here of the quite difficult implementation of transparency and mouse picking:


I find it difficult to visualize what a function would look like that would address your need. There may not be an alternative to doing vector calculations of the kind that have been discussed here.

Bruce

Reply all
Reply to author
Forward
0 new messages