This repository has been archived on 2024-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
ecg-prog-filtered/u02/include/util.h

46 lines
2.1 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include <cstdint>
#include <tuple>
typedef uint8_t Transformation;
const Transformation TRANSFORM_MIRROR_X = 1 << 0;
const Transformation TRANSFORM_MIRROR_Y = 1 << 1;
const Transformation TRANSFORM_ROTATE_CW = 1 << 2;
const Transformation TRANSFORM_ROTATE_CCW = 1 << 3;
// Applies the provided transformation to the point, mutating it in place.
// Rotation is done before mirroring.
void transform_mut(Transformation transformation, int &x, int &y);
// Applies the provided transformation to the point,
// returning the transformed point.
// Rotation is done before mirroring.
std::pair<int, int> transform(Transformation transformation, int x, int y);
// Applies the inverse transformation to the point, mutating it in place.
// Composition of this with the transformation is the identity function.
void transform_inv_mut(Transformation transformation, int &x, int &y);
// Applies the inverse transformation to the point,
// returning the transformed point.
// Composition of this with the transformation is the identity function.
std::pair<int, int> transform_inv(Transformation transformation, int x, int y);
// Returns the transformation required
// to make the given endpoints of a line conform
// to the standard case for rasterization.
Transformation transformation_to_standard_case(int x0, int y0, int x1, int y1);
// Returns the barycentric coordinates of the point given by (xp, yp)
// in the triangle given by the three points (x0, y0), (x1, y1), (x2, y2).
std::tuple<float, float, float> barycentric_coordinates(int x0, int y0, int x1,
int y1, int x2, int y2,
int xp, int yp);
// Sorts the points of a triangle to be in ascending order (y0 < y1 < y2).
void sort_triangle_points(int &x0, int &y0, int &x1, int &y1, int &x2, int &y2);
// This calculates the slope of a line from (x0, y0) to (x1, y1).
// It handles special cases to ensure the return value will be real.
float slope(int x0, int y0, int x1, int y1);