CS488 PA6
Functions
Geometry.hpp File Reference
#include <cmath>
#include "../pa5/Matrix.hpp"
#include "../pa5/Vector.hpp"

Go to the source code of this file.

Functions

template<int N>
double area (const Matrix< 2, 1 > &a, const Matrix< 2, 1 > &b, const Matrix< 2, 1 > &c)
 
template<int N>
Vector< N > barycentricCombination (const Matrix< N, 1 > &q, const Matrix< N, 1 > &r, double alpha)
 
template<int R, int C>
Matrix< R, 2 > getBounds (const Matrix< R, C > &p)
 
template<int N>
bool inside (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &r, const Matrix< 2, 1 > &s, const Matrix< 2, 1 > &t)
 
template<int N>
bool intersect (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &q, const Matrix< 2, 1 > &r, const Matrix< 2, 1 > &s, double *alpha, double *beta)
 
template<int N>
Vector< 2 > perp (const Matrix< 2, 1 > &a)
 
template<int N>
double toImplicit (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &q, Matrix< 2, 1 > *n)
 

Detailed Description

A template for a utility class that can perform various calculations required by 2-D and 3-D rasterizers.

Note: Since we don't need to transform 2D points, they are in Cartesian (rather than homogeneous) coordinates and stored in a Matrix<2,1> (i.e., a 2-element column vector which is the same as a Vector<2>). This also makes it a little easier to calculate the area and determine whether a point is inside of a triangle.

Note: area(), inside(), and toImplicit() are templated so that they are one-to-one with Matrix.

Function Documentation

◆ area()

template<int N>
double area ( const Matrix< 2, 1 > &  a,
const Matrix< 2, 1 > &  b,
const Matrix< 2, 1 > &  c 
)

Compute the area of a triangle from its three vertices.

The vertices can be (i.e., are) assumed to be ordered correctly (i.e., obey the right-hand rule).

Note: This is a function template to avoid problems of duplicate definitions when linking.

Template Parameters
NThe dimensionality (always 2)
Parameters
aThe first vertex
bThe second vertex
cThe third vertex
Returns
The ares

◆ barycentricCombination()

template<int N>
Vector< N > barycentricCombination ( const Matrix< N, 1 > &  q,
const Matrix< N, 1 > &  r,
double  alpha 
)

Returns the point \( \alpha \mathbf{q} + (1-\alpha) \mathbf{r} \).

Template Parameters
NThe dimensionality
Parameters
qOne point
rThe other points
alphaThe weight
Returns
The point

◆ getBounds()

template<int R, int C>
Matrix< R, 2 > getBounds ( const Matrix< R, C > &  p)

Returns the bounds of the given line, triangle, or polygon.

This method returns a matrix with two columns. Column 0 contains the minimums value and column 1 contains the maximum values. (The rows correspond to the rows of the points.)

Template Parameters
RThe number of rows (i.e., the dimensionality of each point)
CThe number of columns (i.e., the number of points)
Parameters
pThe line, triangle, or polygon
Returns
The bounding rectangle

◆ inside()

template<int N>
bool inside ( const Matrix< 2, 1 > &  p,
const Matrix< 2, 1 > &  r,
const Matrix< 2, 1 > &  s,
const Matrix< 2, 1 > &  t 
)

Determine whether the point p is inside the triangle formed by the vertices r, s, t.

Note: This is a function template to avoid problems of duplicate definitions when linking

Template Parameters
NThe dimensionality (always 2)
Parameters
pThe test point
rThe first vertex of the triangle
sThe second vertex of the triangle
tThe third vertex of the triangle
Returns
true if p is in the triangle (r,s,t); false otherwise

◆ intersect()

template<int N>
bool intersect ( const Matrix< 2, 1 > &  p,
const Matrix< 2, 1 > &  q,
const Matrix< 2, 1 > &  r,
const Matrix< 2, 1 > &  s,
double *  alpha,
double *  beta 
)

Find the intersection of two lines (NOT line segments).

This method returns false if the lines are parallel and true if the lines intersect.

If the lines intersect it calculates the (convex combination) weights that define the intersection point. Letting alpha denote the weight for line 0 and beta denote the weight for line 1, the intersection point is given by:

\( [ \alpha p_0 + (1-\alpha) q_0 , \alpha p_1 + (1-\alpha) q_1 ] \)

and or:

\( [ \beta r_0 + (1-\beta) s_0 , \beta r_1 + (1-\beta) s_1 ] \)

If alpha is in [0, 1] then the intersection point is within the line segment from p to q. Similarly, if beta is in [0, 1] then the intersection point is within the line segment from r to s.

Template Parameters
NThe dimensionality (always 2)
Parameters
pOne endpoint of the line from p to q
qThe other endpoint of the line from p to q
rOne endpoint of the line from r to s
sThe other endpoint of the line from r to s
alphaOne of the weights (outbound)
betaThe other weight (outbound)
Returns
true if the lines intersect; false otherwise

◆ perp()

template<int N>
Vector< 2 > perp ( const Matrix< 2, 1 > &  a)

Find a perpendicular to the given 2-vector.

Template Parameters
NThe dimensionality (always 2)
Parameters
aThe 2-vector (which is a Matrix<2, 1> to increase flexibility)
Returns
The perpendicular (i.e., [-a[1], a[0]])

◆ toImplicit()

template<int N>
double toImplicit ( const Matrix< 2, 1 > &  p,
const Matrix< 2, 1 > &  q,
Matrix< 2, 1 > *  n 
)

Compute the implicit form of a line defined by two points.

The implicit form is the set of points \( \mathbf{r} \) that satisfy \( \mathbf{n} \cdot \mathbf{r} = \mathbf{b} \)

n is passed into this method empty and is filled. b is returned.

Note: This is a function template to avoid problems of duplicate definitions when linking

Template Parameters
NThe dimensionality (always 2)
Parameters
pOne endpoint
qThe other endpoint
nThe vector parameter of the implict form (returned)
Returns
The scalar parameter of the implicit form