t
Loading...
Searching...
No Matches
algorithms.hpp
Go to the documentation of this file.
1#include "math/Vector3.hpp"
2#include <vector>
3
9#ifndef ALGORITHMS_HPP
10#define ALGORITHMS_HPP
11
12namespace t {
13
17std::vector<std::pair<int, int>> bresenhamLow(int x0, int y0, int x1, int y1) {
18 int dx = x1 - x0;
19 int dy = y1 - y0;
20 int yIncrement = 1;
21 if (dy < 0) {
22 yIncrement = -1;
23 dy *= -1;
24 }
25 int difference = 2 * dy - dx;
26 int y = y0;
27
28 std::vector<std::pair<int, int>> points;
29
30 for (int x = x0; x <= x1; ++x) {
31 points.push_back(std::make_pair(x, y));
32
33 if (difference > 0) {
34 y += yIncrement;
35 difference += 2 * (dy - dx);
36 } else {
37 difference += 2 * dy;
38 }
39 }
40
41 return points;
42}
43
47std::vector<std::pair<int, int>> bresenhamHigh(int x0, int y0, int x1, int y1) {
48 int dx = x1 - x0;
49 int dy = y1 - y0;
50 int xIncrement = 1;
51 if (dx < 0) {
52 xIncrement = -1;
53 dx *= -1;
54 }
55 int difference = 2 * dx - dy;
56 int x = x0;
57
58 std::vector<std::pair<int, int>> points;
59
60 for (int y = y0; y <= y1; ++y) {
61 points.push_back(std::make_pair(x, y));
62
63 if (difference > 0) {
64 x += xIncrement;
65 difference += 2 * (dx - dy);
66 } else {
67 difference += 2 * dx;
68 }
69 }
70
71 return points;
72}
73
87std::vector<std::pair<int, int>> bresenham(int x0, int y0, int x1, int y1) {
88 if (std::abs(y1 - y0) < std::abs(x1 - x0)) {
89 if (x0 > x1) {
90 return bresenhamLow(x1, y1, x0, y0);
91 } else {
92 return bresenhamLow(x0, y0, x1, y1);
93 }
94 } else {
95 if (y0 > y1) {
96 return bresenhamHigh(x1, y1, x0, y0);
97 } else {
98 return bresenhamHigh(x0, y0, x1, y1);
99 }
100 }
101}
102
112Vector3 barycentric(Vector3 point, Vector3 vertexA, Vector3 vertexB,
113 Vector3 vertexC) {
114 Vector3 edgeAB = vertexB - vertexA;
115 Vector3 edgeAC = vertexC - vertexA;
116 Vector3 ap = point - vertexA;
117
118 double dotABAB = Vector3::dot(edgeAB, edgeAB);
119 double dotABAC = Vector3::dot(edgeAB, edgeAC);
120 double dotACAC = Vector3::dot(edgeAC, edgeAC);
121 double dotAPAB = Vector3::dot(ap, edgeAB);
122 double dotAPAC = Vector3::dot(ap, edgeAC);
123 double denominator = dotABAB * dotACAC - dotABAC * dotABAC;
124
125 double v = (dotACAC * dotAPAB - dotABAC * dotAPAC) / denominator;
126 double w = (dotABAB * dotAPAC - dotABAC * dotAPAB) / denominator;
127 double u = 1.0 - v - w;
128
129 return Vector3(u, v, w);
130}
131
132} // namespace t
133
134#endif // ALGORITHMS_HPP
The 3D vector class.
Definition Vector3.hpp:20
static double dot(const Vector3 &a, const Vector3 &b)
Returns the dot product of two vectors.
Definition Vector3.hpp:67
The t software 3D graphics library namespace.
Definition algorithms.hpp:12
std::vector< std::pair< int, int > > bresenhamHigh(int x0, int y0, int x1, int y1)
Used internally by bresenham.
Definition algorithms.hpp:47
std::vector< std::pair< int, int > > bresenhamLow(int x0, int y0, int x1, int y1)
Used internally by bresenham.
Definition algorithms.hpp:17
std::vector< std::pair< int, int > > bresenham(int x0, int y0, int x1, int y1)
Computes and returns the points of a line using the Bresenham's line algorithm.
Definition algorithms.hpp:87
Vector3 barycentric(Vector3 point, Vector3 vertexA, Vector3 vertexB, Vector3 vertexC)
Computes and returns the barycentric coordinates of a point in a triangle.
Definition algorithms.hpp:112