# Rendering a triangle in 3D

**URL:** https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622
**Category:** Support
**Created:** [April 22, 2021, 9:57pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622 "2021-04-22T21:57:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [April 22, 2021, 9:57pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/1 "2021-04-22T21:57:08Z")

</div>

Dear All,

Generally this forum have persons with great expertise.

See the following link in Section " Intersection of a Ray/Segment with a Triangle"

[http://geomalgorithms.com/a06-\_intersect-2.html](http://geomalgorithms.com/a06-_intersect-2.html)

I am having problems putting work the rendering of a triangle with the algorithm written (with similar code to the one provided at the end). I test the points of a cuboid (that includes the triangle) if are from the triangle. Testing that s\>=0, t \>=0, s+t \<= 1 for all points of the cuboid.

Then I think I understood that:

**u** =V1-V0 must be unitary  
**v** =V2-V0 must be unitary  
Not certain  
**u** and **v** must be normal (???)

How can be generalized the algorithm for general size of **u** and **v**? And not normal? (???)

Thanks,

Luís Gonçalves

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 22, 2021, 11:04pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/2 "2021-04-22T23:04:22Z")

</div>

> [@Luis\_Carlos\_Carneiro](#):
>
> **u** =V1-V0 must be unitary  
> **v** =V2-V0 must be unitary  
> **u** and **v** must be normal (???)

The two vectors don’t have to be unitary or normal. It’s a general algorithm, and a very nice one. But really, this is not a VTK question.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [April 22, 2021, 11:05pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/3 "2021-04-22T23:05:44Z")

</div>

Thanks for the reply. Then I have to see what is happening.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [April 25, 2021, 2:28pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/5 "2021-04-25T14:28:07Z")

</div>

Solved. The algorithm not works alone. Must be tested before if the point is in the plane of the three vertices. And then apply the algorithm to those points that are in the plane.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 25, 2021, 3:06pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/6 "2021-04-25T15:06:48Z")

</div>

The algorithm is designed to project a ray onto the triangle. If you add a check that the point is already in the plane, then you can simplify it by removing the “projection” part, which is the first half of the algorithm.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [April 25, 2021, 3:21pm UTC](https://discourse.vtk.org/t/rendering-a-triangle-in-3d/5622/7 "2021-04-25T15:21:18Z")

</div>

Yes, my error was that at first I omitted the first part of the algorithm (I now saw better the code in the bottom of the page. At first I only saw the text). I only computed u,v,s,t and tested.  
But I found this solution. As I test all the points of a cuboid, I find only one plane for the triangle and test if the points are in the plane is faster than the original algorithm.
