<p>There are many methods you could use to go about doing this, here is one using the parametric form of a line which is: <span class="math-container">$L(t) = p+t\vec v$</span> Where <span class="math-container">$p$</span> is a point on the line and would be stored as a 4 component vector <span class="math-container">$p = \{x,y,z,1\}$</span> and <span class="math-container">$\vec v$</span> is a vector that would be stored as a 4 components <span class="math-container">$\vec v = \{x,y,z,0\}$</span></p>
<p>This form translates with a 4x4 matrix just like any point/vector would by simply transforming both the vector and the point. A 1 is put in the 4th component of a point since it has a location in 3D space and the 0 in the 4th component of vector reflects that a vector has magnitude and direction but no position.</p>
<p>The point can be chosen as one of the ends of the line (it doesn't matter which just be consistent), and the vector is computed just like you would expect. IE if the line has the two end points <span class="math-container">$a$</span> and <span class="math-container">$b$</span> and the <span class="math-container">$a$</span> is chosen as the start, then the vector is computed as <span class="math-container">$b-a$</span>.</p>
<p>Do not normalize the vector and just vary <span class="math-container">$t$</span> between 0 and 1 for every line. Though you may want to calculate the magnitude of the vector (which corresponds to the length of the line) to determine how many points on the line you want to compute WRT the resolution of the image.</p>
<p>Edit:
This can be done in 2D and works exactly the same way. The points would just be x,y,1 and the vectors would be x,y,0 and the transformation matrix becomes a 3x3 matrix.</p>

CC BY-SA 4.0
2021-10-11T11:06:36.910Z
2021-10-11T11:06:36.910Z
pmw1234
<p>I'm trying to make a software renderer, but I want to avoid creating extra triangles with clipping. For this I want to transform lines which compose triangles, instead of individual points. However I'm not sure how to do a perspective transformation on a line equation.</p>

CC BY-SA 4.0
2021-10-10T20:58:42.577Z
How is the line represented?
pmw1234
CC BY-SA 4.0
2021-10-11T00:43:41.960Z
Doesn't matter, so long I can easily flatten it to XY and check if a point(pixel) is on its left or right side. That is, the representation should contain the direction of the line(though I could just use an extra bool if needed). Could be a pair of XY coordinates, implicitly at z=0, z=1; or a pair of ax+by=c; anything thats "easy to work" with.
user369070
<transformations><perspective><camera-matrix>
How do I perform a perspective transform on a straight line?
user369070
# How do I perform a perspective transform on a straight line?

(Asked by: username_0 on 2021-10-10)

I'm trying to make a software renderer, but I want to avoid creating extra triangles with clipping. For this I want to transform lines which compose triangles, instead of individual points. However I'm not sure how to do a perspective transformation on a line equation.

**Tags:** transformations, perspective, camera-matrix

> **Comments:**
> **username_1**: How is the line represented?  
> **username_0**: Doesn't matter, so long I can easily flatten it to XY and check if a point(pixel) is on its left or right side. That is, the representation should contain the direction of the line(though I could just use an extra bool if needed). Could be a pair of XY coordinates, implicitly at z=0, z=1; or a pair of ax+by=c; anything thats "easy to work" with.  

---

## 1 answers

### Answer by username_1 on 2021-10-11. Score: 2 | Accepted answer

There are many methods you could use to go about doing this, here is one using the parametric form of a line which is: $L(t) = p+t\vec v$ Where $p$ is a point on the line and would be stored as a 4 component vector $p = \{x,y,z,1\}$ and $\vec v$ is a vector that would be stored as a 4 components $\vec v = \{x,y,z,0\}$

This form translates with a 4x4 matrix just like any point/vector would by simply transforming both the vector and the point. A 1 is put in the 4th component of a point since it has a location in 3D space and the 0 in the 4th component of vector reflects that a vector has magnitude and direction but no position.

The point can be chosen as one of the ends of the line (it doesn't matter which just be consistent), and the vector is computed just like you would expect. IE if the line has the two end points $a$ and $b$ and the $a$ is chosen as the start, then the vector is computed as $b-a$.

Do not normalize the vector and just vary $t$ between 0 and 1 for every line. Though you may want to calculate the magnitude of the vector (which corresponds to the length of the line) to determine how many points on the line you want to compute WRT the resolution of the image.

Edit:
This can be done in 2D and works exactly the same way. The points would just be x,y,1 and the vectors would be x,y,0 and the transformation matrix becomes a 3x3 matrix.


---

