check your work later !

This commit is contained in:
whaffman 2025-03-27 16:15:11 +01:00
parent fd15edfe1f
commit 83c053cb09
13 changed files with 489 additions and 24 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/25 11:39:39 by whaffman ######## odam.nl */
/* Updated: 2025/03/27 10:57:00 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -29,7 +29,7 @@ Fixed::Fixed(const Fixed &fixed)
Fixed::Fixed(const float value)
{
std::cout << "Float constructor called" << std::endl;
fixedPointValue = (int)(value * (1 << fractionalBits));
fixedPointValue = (int)roundf(value * (1 << fractionalBits));
}
Fixed::Fixed(const int value)

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
/* Updated: 2025/03/25 11:40:20 by whaffman ######## odam.nl */
/* Updated: 2025/03/27 10:58:38 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@ int main(void)
Fixed const b(10);
Fixed const c(42.42f);
Fixed const d(b);
a = Fixed(1234.6321f);
a = Fixed(1234.4321f);
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;

View File

@ -6,10 +6,14 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/25 15:23:47 by whaffman ######## odam.nl */
/* Updated: 2025/03/27 12:27:16 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#pragma once
#define DEBUG 0
class Fixed {
public:
Fixed();

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/25 15:28:16 by whaffman ######## odam.nl */
/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -14,37 +14,45 @@
#include "Fixed.hpp"
#include <cmath>
Fixed::Fixed()
{
fixedPointValue = 0;
std::cout << "Default constructor called" << std::endl;
if (DEBUG)
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const Fixed &fixed)
{
std::cout << "Copy constructor called" << std::endl;
if (DEBUG)
std::cout << "Copy constructor called" << std::endl;
*this = fixed;
}
Fixed::Fixed(const float value)
{
std::cout << "Float constructor called" << std::endl;
fixedPointValue = (int)(value * (1 << fractionalBits));
if (DEBUG)
std::cout << "Float constructor called" << std::endl;
fixedPointValue = (int)roundf(value * (1 << fractionalBits));
}
Fixed::Fixed(const int value)
{
std::cout << "Int constructor called" << std::endl;
if (DEBUG)
std::cout << "Int constructor called" << std::endl;
fixedPointValue = value * (1 << fractionalBits);
}
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
if (DEBUG)
std::cout << "Destructor called" << std::endl;
}
Fixed &Fixed::operator=(const Fixed &fixed)
{
std::cout << "Copy assignment operator called" << std::endl;
if (DEBUG)
std::cout << "Copy assignment operator called" << std::endl;
fixedPointValue = fixed.fixedPointValue;
return *this;
}
@ -58,25 +66,32 @@ std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
Fixed Fixed::operator+(const Fixed &fixed) const
{
return (Fixed(toFloat() + fixed.toFloat()));
Fixed result;
result.setRawBits(getRawBits() + fixed.getRawBits());
return (result);
}
Fixed Fixed::operator-(const Fixed &fixed) const
{
return (Fixed(toFloat() - fixed.toFloat()));
Fixed result;
result.setRawBits(getRawBits() - fixed.getRawBits());
return (result);
}
Fixed Fixed::operator*(const Fixed &fixed) const
{
return (Fixed(toFloat() * fixed.toFloat()));
Fixed result;
result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits));
return (result);
}
Fixed Fixed::operator/(const Fixed &fixed) const
{
return (Fixed(toFloat() / fixed.toFloat()));
Fixed result;
result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits()));
return (result);
}
Fixed &Fixed::operator++()
{
fixedPointValue++;
@ -133,13 +148,15 @@ bool Fixed::operator!=(const Fixed &fixed) const
int Fixed::getRawBits(void) const
{
std::cout << "getRawBits member function called" << std::endl;
if (DEBUG)
std::cout << "getRawBits member function called" << std::endl;
return fixedPointValue;
}
void Fixed::setRawBits(int const raw)
{
std::cout << "setRawBits member function called" << std::endl;
if (DEBUG)
std::cout << "setRawBits member function called" << std::endl;
fixedPointValue = raw;
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
/* Updated: 2025/03/25 15:26:00 by whaffman ######## odam.nl */
/* Updated: 2025/03/27 12:25:28 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -39,6 +39,7 @@ int main(void)
std::cout << a++ << std::endl;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << Fixed(100) / Fixed(8) << std::endl;
std::cout << Fixed::max(a, b) << std::endl;
return 0;

View File

@ -6,11 +6,11 @@
# By: whaffman <whaffman@student.codam.nl> +#+ #
# +#+ #
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
# Updated: 2025/03/24 15:15:55 by whaffman ######## odam.nl #
# Updated: 2025/03/27 16:10:22 by whaffman ######## odam.nl #
# #
# **************************************************************************** #
NAME=
SRC=
NAME= BSP
SRC= main.cpp Fixed.cpp Point.cpp bsp.cpp
-include ../common.mk

63
ex03/inc/Fixed.hpp Normal file
View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* Fixed.hpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:09:40 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#define DEBUG 0
class Fixed {
public:
Fixed();
Fixed(const int value);
Fixed(const float value);
Fixed(const Fixed &fixed);
~Fixed();
Fixed &operator=(const Fixed &fixed);
Fixed operator+(const Fixed &fixed) const;
Fixed operator-(const Fixed &fixed) const;
Fixed operator*(const Fixed &fixed) const;
Fixed operator/(const Fixed &fixed) const;
Fixed &operator++();
Fixed operator++(int);
Fixed &operator--();
Fixed operator--(int);
bool operator>(const Fixed &fixed) const;
bool operator<(const Fixed &fixed) const;
bool operator>=(const Fixed &fixed) const;
bool operator<=(const Fixed &fixed) const;
bool operator==(const Fixed &fixed) const;
bool operator!=(const Fixed &fixed) const;
static Fixed &min(Fixed &a, Fixed &b);
static const Fixed &min(const Fixed &a, const Fixed &b);
static Fixed &max(Fixed &a, Fixed &b);
static const Fixed &max(const Fixed &a, const Fixed &b);
int getRawBits(void) const;
void setRawBits(int const raw);
float toFloat(void) const;
int toInt(void) const;
private:
int fixedPointValue;
static const int fractionalBits = 8;
};
std::ostream &operator<<(std::ostream &out, const Fixed &fixed);

37
ex03/inc/Point.hpp Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* Point.hpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/27 13:09:47 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:09:07 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#pragma once
#include "Fixed.hpp"
class Point {
public:
Point();
Point(const Point &point);
Point(float x, float y);
Point(int x, int y);
~Point();
Point &operator=(const Point &point);
Fixed x(void) const;
Fixed y(void) const;
void x(Fixed x);
void y(Fixed y);
private:
Fixed _x;
Fixed _y;
};

196
ex03/src/Fixed.cpp Normal file
View File

@ -0,0 +1,196 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* Fixed.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <iostream>
#include "Fixed.hpp"
#include <cmath>
Fixed::Fixed()
{
fixedPointValue = 0;
if (DEBUG)
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const Fixed &fixed)
{
if (DEBUG)
std::cout << "Copy constructor called" << std::endl;
*this = fixed;
}
Fixed::Fixed(const float value)
{
if (DEBUG)
std::cout << "Float constructor called" << std::endl;
fixedPointValue = (int)roundf(value * (1 << fractionalBits));
}
Fixed::Fixed(const int value)
{
if (DEBUG)
std::cout << "Int constructor called" << std::endl;
fixedPointValue = value * (1 << fractionalBits);
}
Fixed::~Fixed()
{
if (DEBUG)
std::cout << "Destructor called" << std::endl;
}
Fixed &Fixed::operator=(const Fixed &fixed)
{
if (DEBUG)
std::cout << "Copy assignment operator called" << std::endl;
fixedPointValue = fixed.fixedPointValue;
return *this;
}
std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
{
out << fixed.toFloat();
return (out);
}
Fixed Fixed::operator+(const Fixed &fixed) const
{
Fixed result;
result.setRawBits(getRawBits() + fixed.getRawBits());
return (result);
}
Fixed Fixed::operator-(const Fixed &fixed) const
{
Fixed result;
result.setRawBits(getRawBits() - fixed.getRawBits());
return (result);
}
Fixed Fixed::operator*(const Fixed &fixed) const
{
Fixed result;
result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits));
return (result);
}
Fixed Fixed::operator/(const Fixed &fixed) const
{
Fixed result;
result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits()));
return (result);
}
Fixed &Fixed::operator++()
{
fixedPointValue++;
return *this;
}
Fixed Fixed::operator++(int)
{
fixedPointValue++;
return Fixed(*this);
}
Fixed &Fixed::operator--()
{
fixedPointValue--;
return *this;
}
Fixed Fixed::operator--(int)
{
fixedPointValue--;
return Fixed(*this);
}
bool Fixed::operator>(const Fixed &fixed) const
{
return (fixedPointValue > fixed.fixedPointValue);
}
bool Fixed::operator<(const Fixed &fixed) const
{
return (fixedPointValue < fixed.fixedPointValue);
}
bool Fixed::operator>=(const Fixed &fixed) const
{
return (fixedPointValue >= fixed.fixedPointValue);
}
bool Fixed::operator<=(const Fixed &fixed) const
{
return (fixedPointValue <= fixed.fixedPointValue);
}
bool Fixed::operator==(const Fixed &fixed) const
{
return (fixedPointValue == fixed.fixedPointValue);
}
bool Fixed::operator!=(const Fixed &fixed) const
{
return (fixedPointValue != fixed.fixedPointValue);
}
int Fixed::getRawBits(void) const
{
if (DEBUG)
std::cout << "getRawBits member function called" << std::endl;
return fixedPointValue;
}
void Fixed::setRawBits(int const raw)
{
if (DEBUG)
std::cout << "setRawBits member function called" << std::endl;
fixedPointValue = raw;
}
float Fixed::toFloat(void) const
{
return (float)fixedPointValue / (1 << fractionalBits);
}
int Fixed::toInt(void) const
{
return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits));
}
Fixed &Fixed::min(Fixed &a, Fixed &b)
{
if (a < b)
return a;
return b;
}
const Fixed &Fixed::min(const Fixed &a, const Fixed &b)
{
if (a < b)
return a;
return b;
}
Fixed &Fixed::max(Fixed &a, Fixed &b)
{
if (a > b)
return a;
return b;
}
const Fixed &Fixed::max(const Fixed &a, const Fixed &b)
{
if (a > b)
return a;
return b;
}

65
ex03/src/Point.cpp Normal file
View File

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* Point.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/27 15:45:12 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:01:14 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "Point.hpp"
#include "Fixed.hpp"
Point::Point()
{
}
Point::Point(const Point &point)
{
*this = point;
}
Point::Point(float x, float y)
{
_x = Fixed(x);
_y = Fixed(y);
}
Point::Point(int x, int y)
{
_x = Fixed(x);
_y = Fixed(y);
}
Point::~Point()
{
}
Point &Point::operator=(const Point &point)
{
_x = point.x();
_y = point.y();
return *this;
}
Fixed Point::x(void) const
{
return _x;
}
Fixed Point::y(void) const
{
return _y;
}
void Point::x(Fixed x)
{
_x = x;
}
void Point::y(Fixed y)
{
_y = y;
}

33
ex03/src/bsp.cpp Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* bsp.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/27 16:01:44 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:07:02 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "Point.hpp"
#include "Fixed.hpp"
#include "bsp.h"
Fixed crossproduct(Point a, Point b, Point c)
{
Fixed x1 = b.x() - a.x();
Fixed y1 = b.y() - a.y();
Fixed x2 = c.x() - a.x();
Fixed y2 = c.y() - a.y();
return (x1 * y2 - x2 * y1);
}
bool bsp(Point a, Point b, Point c, Point point)
{
Fixed res1 = crossproduct(a, b, point);
Fixed res2 = crossproduct(b, c, point);
Fixed res3 = crossproduct(c, a, point);
if ((res1 >= 0 && res2 >= 0 && res3 >= 0) || (res1 <= 0 && res2 <= 0 && res3 <= 0))
return true;
return false;
}

19
ex03/src/bsp.h Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* bsp.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/27 16:07:27 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:07:31 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#pragma once
#include "Point.hpp"
#include "Fixed.hpp"
Fixed crossproduct(Point a, Point b, Point c);
bool bsp(Point a, Point b, Point c, Point point);

30
ex03/src/main.cpp Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
/* Updated: 2025/03/27 16:14:32 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <iostream>
#include "Fixed.hpp"
#include "Point.hpp"
#include "bsp.h"
int main(void)
{
Point a(0, 0);
Point b(0, 4);
Point c(4, 0);
Point point(2.002f,2.0f);
if (bsp(a, b, c, point))
std::cout << "Point is inside the triangle" << std::endl;
else
std::cout << "Point is outside the triangle" << std::endl;
return 0;
}