Files
CPU-Raytracer/helper.c
T

35 lines
914 B
C

//
// Created by ary on 29.07.2026.
//
#include "helper.h"
#include <stdlib.h>
#include <math.h>
#include "structs.h"
#include "vectorOp.h"
#define EPSILON 1e-8
vec3 randUnitVector() {
double x1,x2,lengthSquared;
do {
x1 = (rand() / (double) RAND_MAX) * 2 - 1; //random zahl zwischen -1 und 1
x2 = (rand() / (double) RAND_MAX) * 2 - 1;
lengthSquared = x1 * x1 + x2 * x2;
}while (lengthSquared >= 1);
double weirdFactor = 2.0 * sqrt(1.0 - lengthSquared);
return (vec3){
x1 * weirdFactor,
x2 * weirdFactor,
1.0 - 2.0 * lengthSquared};
}
int epsilonCheck(vec3 p1, vec3 p2) { //if distance is to small, the test fails (return 0)
vec3 distanceVector = vec3Subtract(p1, p2);
double sumVector = fabs(distanceVector.x) + fabs(distanceVector.y) + fabs(distanceVector.z);
if (sumVector <= EPSILON) {
return 0;
}
return 1;
}